From 3b755e9f54cfb64fca8b03262b0c03f86ffb263b Mon Sep 17 00:00:00 2001 From: kenorb Date: Tue, 3 Sep 2024 17:34:37 +0100 Subject: [PATCH] Some fixes with const --- Exchange/Account/Account.h | 2 +- Exchange/Account/AccountBase.h | 18 +++++----- Exchange/Account/AccountForex.h | 14 ++++---- Exchange/Account/AccountMt.h | 62 ++++++++++++++++---------------- Exchange/Exchange.h | 4 +-- Exchange/tests/Exchange.test.mq5 | 40 +++++++++++++++------ Platform/Platform.h | 2 +- Platform/Platform.struct.h | 2 +- 8 files changed, 82 insertions(+), 62 deletions(-) diff --git a/Exchange/Account/Account.h b/Exchange/Account/Account.h index cedbf79e8..00d1f7923 100644 --- a/Exchange/Account/Account.h +++ b/Exchange/Account/Account.h @@ -77,7 +77,7 @@ class Account : public AccountBase { /** * Returns serialized representation of the object instance. */ - virtual SerializerNodeType Serialize(Serializer &_s) { + virtual SerializerNodeType Serialize(Serializer &_s) const { _s.PassStruct(THIS_REF, "state", state); return SerializerNodeObject; } diff --git a/Exchange/Account/AccountBase.h b/Exchange/Account/AccountBase.h index 6f2eeef4f..4307eb2aa 100644 --- a/Exchange/Account/AccountBase.h +++ b/Exchange/Account/AccountBase.h @@ -71,47 +71,47 @@ class AccountBase : public Dynamic { /** * Returns serialized representation of the object instance. */ - virtual SerializerNodeType Serialize(Serializer &_s) = 0; + virtual SerializerNodeType Serialize(Serializer &_s) const = 0; /* Virtual methods */ /** * Returns balance value of the current account. */ - virtual datetime GetDateTime() { return TimeCurrent(); }; + virtual datetime GetDateTime() const { return TimeCurrent(); }; /** * Returns balance value of the current account. */ - virtual float GetBalance() = 0; + virtual float GetBalance() const = 0; /** * Returns credit value of the current account. */ - virtual float GetCredit() = 0; + virtual float GetCredit() const = 0; /** * Returns profit value of the current account. */ - virtual float GetProfit() = 0; + virtual float GetProfit() const = 0; /** * Returns equity value of the current account. */ - virtual float GetEquity() = 0; + virtual float GetEquity() const = 0; /** * Returns margin value of the current account. */ - virtual float GetMarginUsed() = 0; + virtual float GetMarginUsed() const = 0; /** * Returns free margin value of the current account. */ - virtual float GetMarginFree() = 0; + virtual float GetMarginFree() const = 0; /** * Get account available margin. */ - virtual float GetMarginAvail() = 0; + virtual float GetMarginAvail() const = 0; }; diff --git a/Exchange/Account/AccountForex.h b/Exchange/Account/AccountForex.h index 66420b76a..3a9001e54 100644 --- a/Exchange/Account/AccountForex.h +++ b/Exchange/Account/AccountForex.h @@ -64,49 +64,49 @@ class AccountForex : public Account { /** * Returns balance value of the current account. */ - float GetBalance() { + float GetBalance() const { return 0.0f; } /** * Returns credit value of the current account. */ - float GetCredit() { + float GetCredit() const { return 0.0f; } /** * Returns profit value of the current account. */ - float GetProfit() { + float GetProfit() const { return 0.0f; } /** * Returns equity value of the current account. */ - float GetEquity() { + float GetEquity() const { return 0.0f; } /** * Returns margin value of the current account. */ - float GetMarginUsed() { + float GetMarginUsed() const { return 0.0f; } /** * Returns free margin value of the current account. */ - float GetMarginFree() { + float GetMarginFree() const { return 0.0f; } /** * Get account available margin. */ - float GetMarginAvail() { + float GetMarginAvail() const { return 0.0f; } }; diff --git a/Exchange/Account/AccountMt.h b/Exchange/Account/AccountMt.h index 7b71ebf3c..62f782f35 100644 --- a/Exchange/Account/AccountMt.h +++ b/Exchange/Account/AccountMt.h @@ -84,16 +84,16 @@ class AccountMt : public AccountBase { * @return * Returns account entry. */ - AccountEntry GetEntry() { + AccountEntry GetEntry() const { AccountEntry _entry; - _entry.dtime = TimeCurrent(); - _entry.balance = GetBalance(); - _entry.credit = GetCredit(); - _entry.equity = GetEquity(); - _entry.profit = GetProfit(); - _entry.margin_used = GetMarginUsed(); - _entry.margin_free = GetMarginFree(); - _entry.margin_avail = GetMarginAvail(); + _entry.dtime = AccountMt::GetDateTime(); + _entry.balance = AccountMt::GetBalance(); + _entry.credit = AccountMt::GetCredit(); + _entry.equity = AccountMt::GetEquity(); + _entry.profit = AccountMt::GetProfit(); + _entry.margin_used = AccountMt::GetMarginUsed(); + _entry.margin_free = AccountMt::GetMarginFree(); + _entry.margin_avail = AccountMt::GetMarginAvail(); return _entry; } @@ -112,7 +112,7 @@ class AccountMt : public AccountBase { * Returns the current account name. */ static string AccountName() { return AccountInfoString(ACCOUNT_NAME); } - string GetAccountName() { return AccountName(); } + string GetAccountName() const { return AccountName(); } /** * Returns the connected server name. @@ -124,13 +124,13 @@ class AccountMt : public AccountBase { * Returns currency name of the current account. */ static string AccountCurrency() { return AccountInfoString(ACCOUNT_CURRENCY); } - string GetCurrency() { return AccountCurrency(); } + string GetCurrency() const { return AccountCurrency(); } /** * Returns the brokerage company name where the current account was registered. */ static string AccountCompany() { return AccountInfoString(ACCOUNT_COMPANY); } - string GetCompanyName() { return AccountCompany(); } + string GetCompanyName() const { return AccountCompany(); } /* Double getters */ @@ -138,7 +138,7 @@ class AccountMt : public AccountBase { * Returns balance value of the current account. */ static double AccountBalance() { return AccountInfoDouble(ACCOUNT_BALANCE); } - float GetBalance() override { + float GetBalance() const override { // @todo: Adds caching. // return UpdateStats(ACC_BALANCE, AccountBalance()); return (float)AccountMt::AccountBalance(); @@ -148,7 +148,7 @@ class AccountMt : public AccountBase { * Returns credit value of the current account. */ static double AccountCredit() { return AccountInfoDouble(ACCOUNT_CREDIT); } - float GetCredit() override { + float GetCredit() const override { // @todo: Adds caching. // return UpdateStats(ACC_CREDIT, AccountCredit()); return (float)AccountMt::AccountCredit(); @@ -158,7 +158,7 @@ class AccountMt : public AccountBase { * Returns profit value of the current account. */ static double AccountProfit() { return AccountInfoDouble(ACCOUNT_PROFIT); } - float GetProfit() override { + float GetProfit() const override { // @todo: Adds caching. // return UpdateStats(ACC_PROFIT, AccountProfit()); return (float)AccountMt::AccountProfit(); @@ -168,7 +168,7 @@ class AccountMt : public AccountBase { * Returns equity value of the current account. */ static double AccountEquity() { return AccountInfoDouble(ACCOUNT_EQUITY); } - float GetEquity() override { + float GetEquity() const override { // @todo: Adds caching. // return UpdateStats(ACC_EQUITY, AccountEquity()); return (float)AccountMt::AccountEquity(); @@ -178,7 +178,7 @@ class AccountMt : public AccountBase { * Returns margin value of the current account. */ static double AccountMargin() { return AccountInfoDouble(ACCOUNT_MARGIN); } - float GetMarginUsed() { + float GetMarginUsed() const { // @todo: Adds caching. // return UpdateStats(ACC_MARGIN_USED, AccountMargin()); return (float)AccountMt::AccountMargin(); @@ -200,7 +200,7 @@ class AccountMt : public AccountBase { * Returns free margin value of the current account. */ static double AccountFreeMargin() { return AccountInfoDouble(ACCOUNT_MARGIN_FREE); } - float GetMarginFree() override { + float GetMarginFree() const override { // @todo: Adds caching. // return UpdateStats(ACC_MARGIN_FREE, AccountFreeMargin()); return (float)AccountMt::AccountFreeMargin(); @@ -212,7 +212,7 @@ class AccountMt : public AccountBase { * @return * Account's free margin in percentage. */ - double GetMarginFreeInPct() { + double GetMarginFreeInPct() const { double _margin_free = GetMarginFree(); double _margin_avail = GetMarginAvail(); return _margin_avail > 0 ? 100 / _margin_avail * _margin_free : 0; @@ -222,19 +222,19 @@ class AccountMt : public AccountBase { * Returns the current account number. */ static int64 AccountNumber() { return AccountInfoInteger(ACCOUNT_LOGIN); } - int64 GetLogin() { return AccountNumber(); } + int64 GetLogin() const { return AccountNumber(); } /** * Returns leverage of the current account. */ static int64 AccountLeverage() { return AccountInfoInteger(ACCOUNT_LEVERAGE); } - int64 GetLeverage() { return AccountLeverage(); } + int64 GetLeverage() const { return AccountLeverage(); } /** * Returns the calculation mode for the Stop Out level. */ static int AccountStopoutMode() { return (int)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE); } - int GetStopoutMode() { return AccountStopoutMode(); } + int GetStopoutMode() const { return AccountStopoutMode(); } /** * Returns the value of the Stop Out level. @@ -243,7 +243,7 @@ class AccountMt : public AccountBase { * is expressed in percents or in the deposit currency. */ static double AccountStopoutLevel() { return AccountInfoDouble(ACCOUNT_MARGIN_SO_SO); } - double GetStopoutLevel() { return AccountStopoutLevel(); } + double GetStopoutLevel() const { return AccountStopoutLevel(); } /** * Get a maximum allowed number of active pending orders set by broker. @@ -252,7 +252,7 @@ class AccountMt : public AccountBase { * Returns the limit orders (0 for unlimited). */ static int64 AccountLimitOrders() { return AccountInfoInteger(ACCOUNT_LIMIT_ORDERS); } - int64 GetLimitOrders(unsigned int _max = 999) { + int64 GetLimitOrders(unsigned int _max = 999) const { int64 _limit = AccountLimitOrders(); return _limit > 0 ? _limit : _max; } @@ -263,13 +263,13 @@ class AccountMt : public AccountBase { * Get account total balance (including credit). */ static double AccountTotalBalance() { return AccountBalance() + AccountCredit(); } - float GetTotalBalance() { return (float)(GetBalance() + GetCredit()); } + float GetTotalBalance() const { return (float)(GetBalance() + GetCredit()); } /** * Get account available margin. */ static double AccountAvailMargin() { return fmin(AccountFreeMargin(), AccountTotalBalance()); } - float GetMarginAvail() override { return (float)AccountAvailMargin(); } + float GetMarginAvail() const override { return (float)AccountAvailMargin(); } /** * Returns the calculation mode of free margin allowed to open orders on the current account. @@ -324,17 +324,17 @@ class AccountMt : public AccountBase { /** * Get account init balance. */ - double GetInitBalance() { return init_balance; } + double GetInitBalance() const { return init_balance; } /** * Get account start balance. */ - double GetStartBalance() { return start_balance; } + double GetStartBalance() const { return start_balance; } /** * Get account init credit. */ - double GetStartCredit() { return start_credit; } + double GetStartCredit() const { return start_credit; } /* Calculation methods */ @@ -388,7 +388,7 @@ class AccountMt : public AccountBase { : -1); #endif } - double GetAccountFreeMarginCheck(ENUM_ORDER_TYPE _cmd, double _volume) { + double GetAccountFreeMarginCheck(ENUM_ORDER_TYPE _cmd, double _volume) const { return AccountFreeMarginCheck(_Symbol, _cmd, _volume); } @@ -639,7 +639,7 @@ class AccountMt : public AccountBase { /** * Returns serialized representation of the object instance. */ - SerializerNodeType Serialize(Serializer &_s) { + SerializerNodeType Serialize(Serializer &_s) const { AccountEntry _entry = GetEntry(); _s.PassStruct(THIS_REF, "account-entry", _entry, SERIALIZER_FIELD_FLAG_DYNAMIC); return SerializerNodeObject; diff --git a/Exchange/Exchange.h b/Exchange/Exchange.h index d58f88fc8..1c843a209 100644 --- a/Exchange/Exchange.h +++ b/Exchange/Exchange.h @@ -75,7 +75,7 @@ class Exchange : public Taskable { * Adds account instance to the list. */ void AccountAdd(AccountParams &_aparams) { - AccountBase *_account = new AccountForex(/*_aparams*/); + AccountBase *_account = new AccountForex(_aparams); AccountAdd(_account); } @@ -191,7 +191,7 @@ class Exchange : public Taskable { /** * Returns serialized representation of the object instance. */ - SerializerNodeType Serialize(Serializer &_s) { + SerializerNodeType Serialize(Serializer &_s) const { _s.PassStruct(THIS_REF, "eparams", eparams); _s.PassStruct(THIS_REF, "accounts", accounts); //_s.PassStruct(THIS_REF, "symbols", symbols); diff --git a/Exchange/tests/Exchange.test.mq5 b/Exchange/tests/Exchange.test.mq5 index d9b5205ee..92a89573b 100644 --- a/Exchange/tests/Exchange.test.mq5 +++ b/Exchange/tests/Exchange.test.mq5 @@ -37,40 +37,60 @@ class AccountDummy : public AccountBase { /** * Returns balance value of the current account. */ - float GetBalance() { return 0; } + float GetBalance() const { return 0; } /** * Returns credit value of the current account. */ - float GetCredit() { return 0; } + float GetCredit() const { return 0; } /** * Returns profit value of the current account. */ - float GetProfit() { return 0; } + float GetProfit() const { return 0; } /** * Returns equity value of the current account. */ - float GetEquity() { return 0; } + float GetEquity() const { return 0; } /** * Returns margin value of the current account. */ - float GetMarginUsed() { return 0; } + float GetMarginUsed() const { return 0; } /** * Returns free margin value of the current account. */ - float GetMarginFree() { return 0; } + float GetMarginFree() const { return 0; } /** * Get account available margin. */ - float GetMarginAvail() { return 0; } + float GetMarginAvail() const { return 0; } + + /* Serializers */ + + /** + * Returns serialized representation of the object instance. + */ + virtual SerializerNodeType Serialize(Serializer &_s) const { + return SerializerNodeObject; + } }; -class ExchangeDummy : public Exchange {}; +class ExchangeDummy : public Exchange { + public: + /** + * Class constructor without parameters. + */ + ExchangeDummy(){}; + + /** + * Class constructor with parameters. + */ + ExchangeDummy(ExchangeParams &_eparams) { eparams = _eparams; }; +}; class SymbolDummy : public SymbolInfo {}; class TradeDummy : public Trade { public: @@ -89,8 +109,8 @@ bool TestExchange01() { // Attach instances of dummy accounts. Ref account01 = new AccountDummy(); Ref account02 = new AccountDummy(); - exchange REF_DEREF AccountAdd(account01.Ptr(), "Account01"); - exchange REF_DEREF AccountAdd(account02.Ptr(), "Account02"); + exchange REF_DEREF AccountAdd(account01.Ptr(), 0); + exchange REF_DEREF AccountAdd(account02.Ptr(), 1); // Attach instances of dummy symbols. Ref symbol01 = new SymbolDummy(); diff --git a/Platform/Platform.h b/Platform/Platform.h index ab37ad0e0..688cc32a1 100644 --- a/Platform/Platform.h +++ b/Platform/Platform.h @@ -621,7 +621,7 @@ class Platform : public Taskable { /** * Returns serialized representation of the object instance. */ - SerializerNodeType Serialize(Serializer &_s) { + SerializerNodeType Serialize(Serializer &_s) const { _s.PassStruct(THIS_REF, "params", pparams); //_s.PassStruct(THIS_REF, "exchanges", exchanges); return SerializerNodeObject; diff --git a/Platform/Platform.struct.h b/Platform/Platform.struct.h index 182be7476..d67260b6b 100644 --- a/Platform/Platform.struct.h +++ b/Platform/Platform.struct.h @@ -82,7 +82,7 @@ struct PlatformParams { SetUserError(ERR_INVALID_PARAMETER); } // Serializers. - SerializerNodeType Serialize(Serializer &s) { + SerializerNodeType Serialize(Serializer &s) const { s.Pass(THIS_REF, "id", id); s.Pass(THIS_REF, "name", name); return SerializerNodeObject;