Skip to content

Commit

Permalink
Issue #218: Used refenrces instead of pointers in C++ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemydiuk committed Nov 1, 2020
1 parent f6659ed commit 1a1b0cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
47 changes: 24 additions & 23 deletions MT5Connector/MT5Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ template <typename T> T Execute(std::function<T()> func, wchar_t* err, T default
catch (Exception^ e)
{
convertSystemString(err, e->Message);
MtAdapter::GetInstance()->LogError(e->Message);
}
return result;
}
Expand Down Expand Up @@ -220,67 +221,67 @@ _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<int>([&expertHandle, res]() {
*res = MtAdapter::GetInstance()->GetCommandType(expertHandle);
return Execute<int>([&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<int>([&expertHandle, &paramIndex, res]() {
*res = (int)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return Execute<int>([&expertHandle, &paramIndex, &res]() {
res = MtAdapter::GetInstance()->GetCommandParameter<int>(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<int>([&expertHandle, &paramIndex, res]() {
*res = (double)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return Execute<int>([&expertHandle, &paramIndex, &res]() {
res = MtAdapter::GetInstance()->GetCommandParameter<double>(expertHandle, paramIndex);
return 1;
}, err, 0);
}

_DLLAPI int _stdcall getStringValue(int expertHandle, int paramIndex, wchar_t* res, wchar_t* err)
{
return Execute<int>([&expertHandle, &paramIndex, res]() {
convertSystemString(res, (String^)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex));
convertSystemString(res, MtAdapter::GetInstance()->GetCommandParameter<String^>(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<int>([&expertHandle, &paramIndex, res]() {
*res = (unsigned __int64)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return Execute<int>([&expertHandle, &paramIndex, &res]() {
res = MtAdapter::GetInstance()->GetCommandParameter<unsigned __int64>(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<int>([&expertHandle, &paramIndex, res]() {
*res = (__int64)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return Execute<int>([&expertHandle, &paramIndex, &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<int>([&expertHandle, &paramIndex, res]() {
bool val = (bool)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
*res = val == true ? 1 : 0;
return Execute<int>([&expertHandle, &paramIndex, &res]() {
bool val = MtAdapter::GetInstance()->GetCommandParameter<bool>(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<int>([&expertHandle, &paramIndex, res]() {
*res = (unsigned int)MtAdapter::GetInstance()->GetCommandParameter(expertHandle, paramIndex);
return Execute<int>([&expertHandle, &paramIndex, &res]() {
res = MtAdapter::GetInstance()->GetCommandParameter<unsigned int>(expertHandle, paramIndex);
return 1;
}, err, 0);
}
9 changes: 7 additions & 2 deletions MTApiService/MtAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public int GetCommandType(int expertHandle)
return retval;
}

public object GetCommandParameter(int expertHandle, int index)
public T GetCommandParameter<T>(int expertHandle, int index)
{
Log.DebugFormat("GetCommandParameter: begin. expertHandle = {0}, index = {1}", expertHandle, index);

Expand All @@ -204,7 +204,7 @@ public object GetCommandParameter(int expertHandle, int index)

Log.DebugFormat("GetCommandParameter: end. retval = {0}", retval);

return retval;
return (T)retval;
}

public object GetNamedParameter(int expertHandle, string name)
Expand Down Expand Up @@ -250,6 +250,11 @@ public bool ContainsNamedParameter(int expertHandle, string name)

return retval;
}

public void LogError(string message)
{
Log.Error(message);
}
#endregion

#region Private Methods
Expand Down

0 comments on commit 1a1b0cd

Please sign in to comment.