From 8af93af0ec7ef27ae678b4c8951b863de48248b1 Mon Sep 17 00:00:00 2001 From: Adrian Kierzkowski Date: Sat, 31 Aug 2024 16:08:53 +0200 Subject: [PATCH] C++ compatibility fixes. Applied clang-format formatting. --- Array.mqh | 158 ++++++++++++------------ Log.mqh | 4 +- Terminal.mqh | 274 +++++++++++++++++++++--------------------- tests/AccountTest.cpp | 6 +- 4 files changed, 223 insertions(+), 219 deletions(-) diff --git a/Array.mqh b/Array.mqh index a0eeeac1e..b362fa304 100644 --- a/Array.mqh +++ b/Array.mqh @@ -21,8 +21,8 @@ */ #ifndef __MQL__ -// Allows the preprocessor to include a header file when it is needed. -#pragma once + // Allows the preprocessor to include a header file when it is needed. + #pragma once #endif // Includes. @@ -35,16 +35,16 @@ // Defines. #ifndef MODE_ASCEND -#define MODE_ASCEND 0 + #define MODE_ASCEND 0 #endif #ifndef MODE_DESCEND -#define MODE_DESCEND 1 + #define MODE_DESCEND 1 #endif // Other constants. // @see https://docs.mql4.com/constants/namedconstants/otherconstants #ifndef WHOLE_ARRAY -// Indicates that all array elements will be processed. -#define WHOLE_ARRAY 0 + // Indicates that all array elements will be processed. + #define WHOLE_ARRAY 0 #endif /* @@ -119,7 +119,7 @@ class Array { int i; string result = ""; for (i = 0; i < ArraySize(arr); i++) { - result += StringFormat("%d:%d%s", i, arr[i], sep); + result += StringFormat("%d:%d%s", i, arr[i], cstring_from(sep)); } // Return text without last separator. return StringSubstr(result, 0, StringLen(result) - StringLen(sep)); @@ -136,7 +136,7 @@ class Array { int i; string result = ""; for (i = 0; i < ArraySize(arr); i++) { - result += StringFormat("%d:%g%s", i, arr[i], sep); + result += StringFormat("%d:%g%s", i, arr[i], cstring_from(sep)); } // Return text without last separator. return StringSubstr(result, 0, StringLen(result) - StringLen(sep)); @@ -404,7 +404,7 @@ static int GetLowestArrDoubleValue(double& arr[][], int key) { int i; string res = ""; for (i = 0; i < ArraySize(arr); i++) { - res += StringFormat("%g%s", NormalizeDouble(arr[i], digits), dlm); + res += StringFormat("%g%s", NormalizeDouble(arr[i], digits), cstring_from(dlm)); } res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm)); return res; @@ -610,16 +610,16 @@ static int GetLowestArrDoubleValue(double& arr[][], int key) { #ifdef __MQL4__ template static bool ArraySort2D(T& arr[][], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) { -#ifdef __MQL4__ + #ifdef __MQL4__ return (bool)::ArraySort(arr, count, start, direction); -#else + #else if (direction == MODE_DESCEND) { return ::ArrayReverse(arr, start, count); } else { // @fixme: Add support for _count amd _start. return ::ArraySort(arr); } -#endif + #endif } #endif @@ -655,42 +655,42 @@ static int GetLowestArrDoubleValue(double& arr[][], int key) { return result; } -/** - * Initializes a numeric array by a preset value. - * - * @param array[] - * Numeric array that should be initialized. - * @param char value - * New value that should be set to all array elements. - * @return int - * Number of initialized elements. - * - * @docs - * - https://docs.mql4.com/array/arrayinitialize - * - https://www.mql5.com/en/docs/array/arrayinitialize - */ -template -static int ArrayInitialize(ARRAY_REF(X, array), char value) { - return ::ArrayInitialize(array, value); -} + /** + * Initializes a numeric array by a preset value. + * + * @param array[] + * Numeric array that should be initialized. + * @param char value + * New value that should be set to all array elements. + * @return int + * Number of initialized elements. + * + * @docs + * - https://docs.mql4.com/array/arrayinitialize + * - https://www.mql5.com/en/docs/array/arrayinitialize + */ + template + static int ArrayInitialize(ARRAY_REF(X, array), char value) { + return ::ArrayInitialize(array, value); + } -/** - * Searches for the lowest element in the first dimension of a multidimensional numeric array. - * - * @param void &array[] - * A numeric array, in which search is made. - * @param int start - * Index to start checking with. - * @param int count - * Number of elements for search. By default, searches in the entire array. - * @return int - * The function returns an index of a found element. - * - * @docs - * - https://docs.mql4.com/array/arraymaximum - * - https://www.mql5.com/en/docs/array/arraymaximum - */ -template + /** + * Searches for the lowest element in the first dimension of a multidimensional numeric array. + * + * @param void &array[] + * A numeric array, in which search is made. + * @param int start + * Index to start checking with. + * @param int count + * Number of elements for search. By default, searches in the entire array. + * @return int + * The function returns an index of a found element. + * + * @docs + * - https://docs.mql4.com/array/arraymaximum + * - https://www.mql5.com/en/docs/array/arraymaximum + */ + template static int ArrayMinimum(ARRAY_REF(X, _array), int _start = 0, int _count = WHOLE_ARRAY) { #ifdef __MQL__ return ::ArrayMinimum(_array); @@ -707,23 +707,23 @@ template #endif } -/** - * Searches for the largest element in the first dimension of a multidimensional numeric array. - * - * @param void &array[] - * A numeric array, in which search is made. - * @param int start - * Index to start checking with. - * @param int count - * Number of elements for search. By default, searches in the entire array. - * @return int - * The function returns an index of a found element. - * - * @docs - * - https://docs.mql4.com/array/arraymaximum - * - https://www.mql5.com/en/docs/array/arraymaximum - */ -template + /** + * Searches for the largest element in the first dimension of a multidimensional numeric array. + * + * @param void &array[] + * A numeric array, in which search is made. + * @param int start + * Index to start checking with. + * @param int count + * Number of elements for search. By default, searches in the entire array. + * @return int + * The function returns an index of a found element. + * + * @docs + * - https://docs.mql4.com/array/arraymaximum + * - https://www.mql5.com/en/docs/array/arraymaximum + */ + template static int ArrayMaximum(ARRAY_REF(X, _array), int start = 0, int count = WHOLE_ARRAY) { #ifdef __MQL__ return ::ArrayMaximum(_array); @@ -740,22 +740,22 @@ template #endif } -/** - * Returns the number of elements of a selected array. - * - * @param void &array[] - * Array of any type. - * @return int - * Value of int type. - * - * @docs - * - https://docs.mql4.com/array/arraysize - * - https://www.mql5.com/en/docs/array/arraysize - */ -template + /** + * Returns the number of elements of a selected array. + * + * @param void &array[] + * Array of any type. + * @return int + * Value of int type. + * + * @docs + * - https://docs.mql4.com/array/arraysize + * - https://www.mql5.com/en/docs/array/arraysize + */ + template static int ArraySize(ARRAY_REF(X, array)) { - return ::ArraySize(array); -} + return ::ArraySize(array); + } template static void ArrayStore(ARRAY_REF(X, array), int _idx, X value, int reserve_size = 0) { diff --git a/Log.mqh b/Log.mqh index 13bc13fd4..262c93076 100644 --- a/Log.mqh +++ b/Log.mqh @@ -76,7 +76,7 @@ class Log : public Object { /** * Class copy constructor. */ - Log(const Log &_log) : filename(_log.filename), last_entry(_log.last_entry), log_level(_log.log_level) {} + Log(const Log &_log) : last_entry(_log.last_entry), log_level(_log.log_level), filename(_log.filename) {} /** * Class deconstructor. @@ -149,7 +149,7 @@ class Log : public Object { * Adds a log entry. */ bool Add(string msg, string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) { - return Add(prefix, msg, suffix, entry_log_level); + return Add(entry_log_level, prefix, msg, suffix); } bool Add(ARRAY_REF(double, arr), string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) { return Add(prefix, Array::ArrToString(arr), suffix, entry_log_level); diff --git a/Terminal.mqh b/Terminal.mqh index 6015a1da7..f99053523 100644 --- a/Terminal.mqh +++ b/Terminal.mqh @@ -51,8 +51,8 @@ class Terminal; // Provide backward compatibility for MQL4 in MQL5. //#include "MQL4.mqh" #else -// Provides forward compatibility for MQL5 in MQL4. -#include "MQL5.mqh" + // Provides forward compatibility for MQL5 in MQL4. + #include "MQL5.mqh" #endif /** @@ -736,128 +736,129 @@ class Terminal : public Object { case 4206: text = "No specified subwindow."; break; - /* Return Codes of the Trade Server */ - // @docs: https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes - case 10004: // TRADE_RETCODE_REQUOTE - text = "Requote"; - break; - case 10006: // TRADE_RETCODE_REJECT - text = "Request rejected"; - break; - case 10007: // TRADE_RETCODE_CANCEL - text = "Request canceled by trader"; - break; - case 10008: // TRADE_RETCODE_PLACED - text = "Order placed"; - break; - case 10009: // TRADE_RETCODE_DONE - text = "Request completed"; - break; - case 10010: // TRADE_RETCODE_DONE_PARTIAL - text = "Only part of the request was completed"; - break; - case 10011: // TRADE_RETCODE_ERROR - text = "Request processing error"; - break; - case 10012: // TRADE_RETCODE_TIMEOUT - text = "Request canceled by timeout"; - break; - case 10013: // TRADE_RETCODE_INVALID - text = "Invalid request"; - break; - case 10014: // TRADE_RETCODE_INVALID_VOLUME - text = "Invalid volume in the request"; - break; - case 10015: // TRADE_RETCODE_INVALID_PRICE - text = "Invalid price in the request"; - break; - case 10016: // TRADE_RETCODE_INVALID_STOPS - text = "Invalid stops in the request"; - break; - case 10017: // TRADE_RETCODE_TRADE_DISABLED - text = "Trade is disabled"; - break; - case 10018: // TRADE_RETCODE_MARKET_CLOSED - text = "Market is closed"; - break; - case 10019: // TRADE_RETCODE_NO_MONEY - text = "There is not enough money to complete the request"; - break; - case 10020: // TRADE_RETCODE_PRICE_CHANGED - text = "Prices changed"; - break; - case 10021: // TRADE_RETCODE_PRICE_OFF - text = "There are no quotes to process the request"; - break; - case 10022: // TRADE_RETCODE_INVALID_EXPIRATION - text = "Invalid order expiration date in the request"; - break; - case 10023: // TRADE_RETCODE_ORDER_CHANGED - text = "Order state changed"; - break; - case 10024: // TRADE_RETCODE_TOO_MANY_REQUESTS - text = "Too frequent requests"; - break; - case 10025: // TRADE_RETCODE_NO_CHANGES - text = "No changes in request"; - break; - case 10026: // TRADE_RETCODE_SERVER_DISABLES_AT - text = "Autotrading disabled by server"; - break; - case 10027: // TRADE_RETCODE_CLIENT_DISABLES_AT - text = "Autotrading disabled by client terminal"; - break; - case 10028: // TRADE_RETCODE_LOCKED - text = "Request locked for processing"; - break; - case 10029: // TRADE_RETCODE_FROZEN - text = "Order or position frozen"; - break; - case 10030: // TRADE_RETCODE_INVALID_FILL - text = "Invalid order filling type"; - break; - case 10031: // TRADE_RETCODE_CONNECTION - text = "No connection with the trade server"; - break; - case 10032: // TRADE_RETCODE_ONLY_REAL - text = "Operation is allowed only for live accounts"; - break; - case 10033: // TRADE_RETCODE_LIMIT_ORDERS - text = "The number of pending orders has reached the limit"; - break; - case 10034: // TRADE_RETCODE_LIMIT_VOLUME - text = "The volume of orders and positions for the symbol has reached the limit"; - break; - case 10035: // TRADE_RETCODE_INVALID_ORDER - text = "Incorrect or prohibited order type"; - break; - case 10036: // TRADE_RETCODE_POSITION_CLOSED - text = "Position with the specified POSITION_IDENTIFIER has already been closed"; - break; - case 10039: // TRADE_RETCODE_CLOSE_ORDER_EXIST - text = "A close order already exists for a specified position"; - break; - case 10040: // TRADE_RETCODE_LIMIT_POSITIONS - text = "The number of open positions simultaneously present on an account can be limited by the server settings"; - break; - case 10041: // TRADE_RETCODE_REJECT_CANCEL - text = "The pending order activation request is rejected, the order is canceled"; - break; - case 10042: // TRADE_RETCODE_LONG_ONLY - text = "The request is rejected because only long positions are allowed"; - break; - case 10043: // TRADE_RETCODE_SHORT_ONLY - text = "The request is rejected because only short positions are allowed"; - break; - case 10044: // TRADE_RETCODE_CLOSE_ONLY - text = "The request is rejected because only position closing is allowed"; - break; - case 10045: // TRADE_RETCODE_FIFO_CLOSE - text = "The request is rejected because position closing is allowed only by FIFO rule"; - break; - case 10046: // TRADE_RETCODE_HEDGE_PROHIBITED - text = "The request is rejected because opposite positions on a single symbol are disabled"; - break; + /* Return Codes of the Trade Server */ + // @docs: https://www.mql5.com/en/docs/constants/errorswarnings/enum_trade_return_codes + case 10004: // TRADE_RETCODE_REQUOTE + text = "Requote"; + break; + case 10006: // TRADE_RETCODE_REJECT + text = "Request rejected"; + break; + case 10007: // TRADE_RETCODE_CANCEL + text = "Request canceled by trader"; + break; + case 10008: // TRADE_RETCODE_PLACED + text = "Order placed"; + break; + case 10009: // TRADE_RETCODE_DONE + text = "Request completed"; + break; + case 10010: // TRADE_RETCODE_DONE_PARTIAL + text = "Only part of the request was completed"; + break; + case 10011: // TRADE_RETCODE_ERROR + text = "Request processing error"; + break; + case 10012: // TRADE_RETCODE_TIMEOUT + text = "Request canceled by timeout"; + break; + case 10013: // TRADE_RETCODE_INVALID + text = "Invalid request"; + break; + case 10014: // TRADE_RETCODE_INVALID_VOLUME + text = "Invalid volume in the request"; + break; + case 10015: // TRADE_RETCODE_INVALID_PRICE + text = "Invalid price in the request"; + break; + case 10016: // TRADE_RETCODE_INVALID_STOPS + text = "Invalid stops in the request"; + break; + case 10017: // TRADE_RETCODE_TRADE_DISABLED + text = "Trade is disabled"; + break; + case 10018: // TRADE_RETCODE_MARKET_CLOSED + text = "Market is closed"; + break; + case 10019: // TRADE_RETCODE_NO_MONEY + text = "There is not enough money to complete the request"; + break; + case 10020: // TRADE_RETCODE_PRICE_CHANGED + text = "Prices changed"; + break; + case 10021: // TRADE_RETCODE_PRICE_OFF + text = "There are no quotes to process the request"; + break; + case 10022: // TRADE_RETCODE_INVALID_EXPIRATION + text = "Invalid order expiration date in the request"; + break; + case 10023: // TRADE_RETCODE_ORDER_CHANGED + text = "Order state changed"; + break; + case 10024: // TRADE_RETCODE_TOO_MANY_REQUESTS + text = "Too frequent requests"; + break; + case 10025: // TRADE_RETCODE_NO_CHANGES + text = "No changes in request"; + break; + case 10026: // TRADE_RETCODE_SERVER_DISABLES_AT + text = "Autotrading disabled by server"; + break; + case 10027: // TRADE_RETCODE_CLIENT_DISABLES_AT + text = "Autotrading disabled by client terminal"; + break; + case 10028: // TRADE_RETCODE_LOCKED + text = "Request locked for processing"; + break; + case 10029: // TRADE_RETCODE_FROZEN + text = "Order or position frozen"; + break; + case 10030: // TRADE_RETCODE_INVALID_FILL + text = "Invalid order filling type"; + break; + case 10031: // TRADE_RETCODE_CONNECTION + text = "No connection with the trade server"; + break; + case 10032: // TRADE_RETCODE_ONLY_REAL + text = "Operation is allowed only for live accounts"; + break; + case 10033: // TRADE_RETCODE_LIMIT_ORDERS + text = "The number of pending orders has reached the limit"; + break; + case 10034: // TRADE_RETCODE_LIMIT_VOLUME + text = "The volume of orders and positions for the symbol has reached the limit"; + break; + case 10035: // TRADE_RETCODE_INVALID_ORDER + text = "Incorrect or prohibited order type"; + break; + case 10036: // TRADE_RETCODE_POSITION_CLOSED + text = "Position with the specified POSITION_IDENTIFIER has already been closed"; + break; + case 10039: // TRADE_RETCODE_CLOSE_ORDER_EXIST + text = "A close order already exists for a specified position"; + break; + case 10040: // TRADE_RETCODE_LIMIT_POSITIONS + text = + "The number of open positions simultaneously present on an account can be limited by the server settings"; + break; + case 10041: // TRADE_RETCODE_REJECT_CANCEL + text = "The pending order activation request is rejected, the order is canceled"; + break; + case 10042: // TRADE_RETCODE_LONG_ONLY + text = "The request is rejected because only long positions are allowed"; + break; + case 10043: // TRADE_RETCODE_SHORT_ONLY + text = "The request is rejected because only short positions are allowed"; + break; + case 10044: // TRADE_RETCODE_CLOSE_ONLY + text = "The request is rejected because only position closing is allowed"; + break; + case 10045: // TRADE_RETCODE_FIFO_CLOSE + text = "The request is rejected because position closing is allowed only by FIFO rule"; + break; + case 10046: // TRADE_RETCODE_HEDGE_PROHIBITED + text = "The request is rejected because opposite positions on a single symbol are disabled"; + break; default: text = "Unknown error."; } @@ -1002,7 +1003,7 @@ class Terminal : public Object { case TERMINAL_COND_IS_CONNECTED: return !IsConnected(); default: - Print(StringFormat("Invalid terminal condition: %s!", EnumToString(_cond), __FUNCTION__)); + Print(StringFormat("Invalid terminal condition: %s!", cstring_from(EnumToString(_cond)), __FUNCTION__)); return false; } } @@ -1037,7 +1038,7 @@ class Terminal : public Object { case TERMINAL_ACTION_CRASH: delete THIS_PTR; default: - Print(StringFormat("Invalid terminal action: %s!", EnumToString(_action), __FUNCTION__)); + Print(StringFormat("Invalid terminal action: %s!", cstring_from(EnumToString(_action)), __FUNCTION__)); return false; } } @@ -1051,7 +1052,8 @@ class Terminal : public Object { /** * Returns textual representation of the Terminal class. */ - string ToString(string _sep = "; ") { + string const ToString() override { + string _sep = "; "; return StringFormat("Allow DLL: %s", IsDllsAllowed() ? "Yes" : "No") + _sep + StringFormat("Allow Libraries: %s", IsLibrariesAllowed() ? "Yes" : "No") + _sep + StringFormat("CPUs: %d", GetCpuCores()) + _sep + @@ -1071,16 +1073,18 @@ class Terminal : public Object { StringFormat("Memory (physical): %d", GetPhysicalMemory()) + _sep + StringFormat("Memory (total): %d", GetTotalMemory()) + _sep + StringFormat("Memory (used): %d", GetUsedMemory()) + _sep + - StringFormat("Path (Common): %s", GetCommonPath()) + _sep + StringFormat("Path (Data): %s", GetDataPath()) + - _sep + StringFormat("Path (Expert): %s", GetExpertPath()) + _sep + - StringFormat("Path (Terminal): %s", GetTerminalPath()) + _sep + - StringFormat("Program name: %s", WindowExpertName()) + _sep + + StringFormat("Path (Common): %s", cstring_from(GetCommonPath())) + _sep + + StringFormat("Path (Data): %s", cstring_from(GetDataPath())) + _sep + + StringFormat("Path (Expert): %s", cstring_from(GetExpertPath())) + _sep + + StringFormat("Path (Terminal): %s", cstring_from(GetTerminalPath())) + _sep + + StringFormat("Program name: %s", cstring_from(WindowExpertName())) + _sep + StringFormat("Screen DPI: %d", GetScreenDpi()) + _sep + StringFormat("Terminal build: %d", GetBuild()) + - _sep + StringFormat("Terminal code page: %d", IntegerToString(GetCodePage())) + _sep + - StringFormat("Terminal company: %s", GetCompany()) + _sep + + _sep + StringFormat("Terminal code page: %d", cstring_from(IntegerToString(GetCodePage()))) + _sep + + StringFormat("Terminal company: %s", cstring_from(GetCompany())) + _sep + StringFormat("Terminal connected: %s", IsConnected() ? "Yes" : "No") + _sep + - StringFormat("Terminal language: %s", GetLanguage()) + _sep + StringFormat("Terminal name: %s", GetName()) + - _sep + StringFormat("Terminal max bars: %d", GetMaxBars()) + _sep + + StringFormat("Terminal language: %s", cstring_from(GetLanguage())) + _sep + + StringFormat("Terminal name: %s", cstring_from(GetName())) + _sep + + StringFormat("Terminal max bars: %d", GetMaxBars()) + _sep + StringFormat("Trade allowed: %s", IsTradeAllowed() ? "Yes" : "No") + _sep + StringFormat("Trade context busy: %s", IsTradeContextBusy() ? "Yes" : "No") + _sep + StringFormat("Trade perm: %s", CheckPermissionToTrade() ? "Yes" : "No") + _sep + diff --git a/tests/AccountTest.cpp b/tests/AccountTest.cpp index ee19d8cd7..4fb5082c8 100644 --- a/tests/AccountTest.cpp +++ b/tests/AccountTest.cpp @@ -40,9 +40,9 @@ typedef unsigned short ushort; int main(int argc, char **argv) { // Initialize class. // Defines variables. - double _balance = AccountInfoDouble(ACCOUNT_BALANCE); - double _credit = AccountInfoDouble(ACCOUNT_CREDIT); - double _equity = AccountInfoDouble(ACCOUNT_EQUITY); + // double _balance = AccountInfoDouble(ACCOUNT_BALANCE); + // double _credit = AccountInfoDouble(ACCOUNT_CREDIT); + // double _equity = AccountInfoDouble(ACCOUNT_EQUITY); // Dummy calls. // acc.GetAccountName();