From 1a1b0cd822352bf3d32df2c977d1cb601202179c Mon Sep 17 00:00:00 2001 From: Viacheslav Demydiuk Date: Sun, 1 Nov 2020 15:59:11 +0200 Subject: [PATCH] Issue #218: Used refenrces instead of pointers in C++ functions --- MT5Connector/MT5Connector.cpp | 47 ++++++++++++++++++----------------- MTApiService/MtAdapter.cs | 9 +++++-- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/MT5Connector/MT5Connector.cpp b/MT5Connector/MT5Connector.cpp index 1b5acd4b..ebbbf234 100755 --- a/MT5Connector/MT5Connector.cpp +++ b/MT5Connector/MT5Connector.cpp @@ -58,6 +58,7 @@ template T Execute(std::function func, wchar_t* err, T default catch (Exception^ e) { convertSystemString(err, e->Message); + MtAdapter::GetInstance()->LogError(e->Message); } return result; } @@ -220,26 +221,26 @@ _DLLAPI int _stdcall sendErrorResponse(int expertHandle, int code, wchar_t* mess //----------- get values ------------------------------- -_DLLAPI int _stdcall getCommandType(int expertHandle, int* res, wchar_t* err) +_DLLAPI int _stdcall getCommandType(int expertHandle, int& res, wchar_t* err) { - return Execute([&expertHandle, res]() { - *res = MtAdapter::GetInstance()->GetCommandType(expertHandle); + return Execute([&expertHandle, &res]() { + res = MtAdapter::GetInstance()->GetCommandType(expertHandle); return 1; }, err, 0); } -_DLLAPI int _stdcall getIntValue(int expertHandle, int paramIndex, int* res, wchar_t* err) +_DLLAPI int _stdcall getIntValue(int expertHandle, int paramIndex, int& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - *res = (int)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + return Execute([&expertHandle, ¶mIndex, &res]() { + res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); return 1; }, err, 0); } -_DLLAPI int _stdcall getDoubleValue(int expertHandle, int paramIndex, double* res, wchar_t* err) +_DLLAPI int _stdcall getDoubleValue(int expertHandle, int paramIndex, double& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - *res = (double)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + return Execute([&expertHandle, ¶mIndex, &res]() { + res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); return 1; }, err, 0); } @@ -247,40 +248,40 @@ _DLLAPI int _stdcall getDoubleValue(int expertHandle, int paramIndex, double* re _DLLAPI int _stdcall getStringValue(int expertHandle, int paramIndex, wchar_t* res, wchar_t* err) { return Execute([&expertHandle, ¶mIndex, res]() { - convertSystemString(res, (String^)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex)); + convertSystemString(res, MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex)); return 1; }, err, 0); } -_DLLAPI int _stdcall getULongValue(int expertHandle, int paramIndex, unsigned __int64* res, wchar_t* err) +_DLLAPI int _stdcall getULongValue(int expertHandle, int paramIndex, unsigned __int64& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - *res = (unsigned __int64)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + return Execute([&expertHandle, ¶mIndex, &res]() { + res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); return 1; }, err, 0); } -_DLLAPI int _stdcall getLongValue(int expertHandle, int paramIndex, __int64* res, wchar_t* err) +_DLLAPI int _stdcall getLongValue(int expertHandle, int paramIndex, __int64& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - *res = (__int64)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + return Execute([&expertHandle, ¶mIndex, &res]() { + res = MtAdapter::GetInstance()->GetCommandParameter<__int64>(expertHandle, paramIndex); return 1; }, err, 0); } -_DLLAPI int _stdcall getBooleanValue(int expertHandle, int paramIndex, int* res, wchar_t* err) +_DLLAPI int _stdcall getBooleanValue(int expertHandle, int paramIndex, int& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - bool val = (bool)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); - *res = val == true ? 1 : 0; + return Execute([&expertHandle, ¶mIndex, &res]() { + bool val = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + res = val == true ? 1 : 0; return 1; }, err, 0); } -_DLLAPI int _stdcall getUIntValue(int expertHandle, int paramIndex, unsigned int* res, wchar_t* err) +_DLLAPI int _stdcall getUIntValue(int expertHandle, int paramIndex, unsigned int& res, wchar_t* err) { - return Execute([&expertHandle, ¶mIndex, res]() { - *res = (unsigned int)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); + return Execute([&expertHandle, ¶mIndex, &res]() { + res = MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex); return 1; }, err, 0); } diff --git a/MTApiService/MtAdapter.cs b/MTApiService/MtAdapter.cs index e5c075a5..b7d43e33 100644 --- a/MTApiService/MtAdapter.cs +++ b/MTApiService/MtAdapter.cs @@ -185,7 +185,7 @@ namespace MTApiService return retval; } - public object GetCommandParameter(int expertHandle, int index) + public T GetCommandParameter(int expertHandle, int index) { Log.DebugFormat("GetCommandParameter: begin. expertHandle = {0}, index = {1}", expertHandle, index); @@ -204,7 +204,7 @@ namespace MTApiService Log.DebugFormat("GetCommandParameter: end. retval = {0}", retval); - return retval; + return (T)retval; } public object GetNamedParameter(int expertHandle, string name) @@ -250,6 +250,11 @@ namespace MTApiService return retval; } + + public void LogError(string message) + { + Log.Error(message); + } #endregion #region Private Methods