If you have something similar and each type of data is handled (in the example only double and enum because its an example) and
you want to turn the value to text automatically , i.e. new value , instant format to text ,why is it not allowed to use the function for enumerations (EnumToString) when you create a double type instance of the class .
Can't it assume i'm handling it by types ?
Thanks
Edit , the opposite is allowed :
You can do that. Then just call the function in any class, struct, anywhere. Of course, just for enum and double. If you need other types like int or bool, will need add new functions for them.
As the above, but prints with a nicer and more accurate formatting:
//--- print an expression or variable along with its name#define TOSTR(A) (#A + " = " + ToString(A) + " ")
#define PRINT(A) Print(#A + " = ", ToString(A))
template <typename T>
string ToString(const T val) { return(EnumToString((T)val)); }
string ToString(constdouble val) { return(string(val)); } // fixed (string)dbl bug in recent versionsstring ToString(constfloat val) { return(Repr(val) + "f"); } // because (string)float bug is still NOT fixed!string ToString(constchar val) { return(string(val)); }
string ToString(constuchar val) { return(string(val)); }
string ToString(constshort val) { return(string(val)); }
string ToString(constushort val) { return(string(val)); }
string ToString(constint val) { return(string(val)); }
string ToString(constuint val) { return(string(val)); }
string ToString(constlong val) { return(string(val)); }
string ToString(constulong val) { return(string(val)); }
string ToString(constbool val) { return(string(val)); }
string ToString(constcolor val) { return(string(val)); }
string ToString(constdatetime val) { return("D'" + string(val)) + "'"; }
string ToString(conststring val) { return("\"" + string(val)) + "\""; }
//+------------------------------------------------------------------+//| Converting float value into the shortest string representation |//| that round-trips into the same numeric value. This ensures that |//| a float value converted to a string is always parsed back into |//| the same numeric value. i.e., (float)(Repr(f)) == f. |//+------------------------------------------------------------------+string Repr(float value)
{
string str;
for(int prec = 6; prec < 10; prec++)
if(value == float(str = StringFormat("%.*g", prec, value)))
return str;
//--- never reached!return(NULL);
}
Demo
voidOnStart()
{
double d = 1.1234567890123;
float f = (float)d;
int i = 101;
bool b = false;
color c = clrGreen;
datetime t = D'2038.01.01';
string s = "Hello World";
ENUM_TIMEFRAMES period = PERIOD_D1;
//--- print variables
PRINT(d);
PRINT(f); // Print(f);
PRINT(i);
PRINT(b);
PRINT(c);
PRINT(t);
PRINT(s);
PRINT(period);
Print(TOSTR(d), TOSTR(f), TOSTR(i), TOSTR(b), TOSTR(c), TOSTR(t), TOSTR(s), TOSTR(period));
//--- print expressions
PRINT(Symbol());
Print(TOSTR(SymbolInfoDouble(Symbol(), SYMBOL_ASK)), TOSTR((ENUM_SYMBOL_SWAP_MODE)SymbolInfoInteger(Symbol(),SYMBOL_SWAP_MODE)));
}
/*
d = 1.1234567890123
f = 1.1234568f
i = 101
b = false
c = clrGreen
t = D'2038.01.01 00:00:00'
s = "Hello World"
period = PERIOD_D1
d = 1.1234567890123 f = 1.1234568f i = 101 b = false c = clrGreen t = D'2038.01.01 00:00:00' s = "Hello World" period = PERIOD_D1
Symbol() = "CHFJPY"
SymbolInfoDouble(Symbol(),SYMBOL_ASK) = 138.373 (ENUM_SYMBOL_SWAP_MODE)SymbolInfoInteger(Symbol(),SYMBOL_SWAP_MODE) = SYMBOL_SWAP_MODE_POINTS
*/
For printing enumeration values:
template<typename T>
void PrintEnum(T dummy = -1)
{
Print(typename(T));
Print(" {");
for(int i = -2; i < USHORT_MAX + 1000; i++)
if(StringFind(EnumToString((T)i),"::")<0)
printf(" %s = %d,",EnumToString((T)i),i);
Print(" };");
}
voidOnStart()
{
PrintEnum<ENUM_TIMEFRAMES>();
PrintEnum<ENUM_ACCOUNT_INFO_DOUBLE>();
PrintEnum<ENUM_FP_CLASS>(); // the lowest enum value
PrintEnum<ENUM_CHART_EVENT>(); // the highest enum value
}
You can do that. Then just call the function in any class, struct, anywhere. Of course, just for enum and double. If you need other types like int or bool, will need add new functions for them.
For this case I have created a class "cToString". It's basically the same as what Samuel described. See the attached script for usage.
this are the solutions
It appears if you place the double (to text) before the template (to text) the editor is okay with it because it comes first .(for these 2 types , i will test will all others)
Enumerations - Matrix and Vector Types - Matrix and Vector Methods - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Data Type Identifiers - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Client Terminal Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
here are all the possible enum item values , excluding some ENUM_DX_FORMAT values and ENUM_GEMM .
long allPossibleEnumItemValues[]={37,38,39,40,41,42,43,45,46,48,49,50,51,52,0,32,33,34,35,44,47,53,54,55,56,1,2,3,36,4,5,6,7,8,9,10,11,12,13,14,15,16,1000,66534,108,109,17,18,19,21,22,23,24,25,26,27,28,29,30,31,100,101,102,103,104,105,106,107,110,111,112,115,116,118,20,113,114,64,128,65536,131072,262144,524288,1048576,2097152,4096,96,97,98,99,255,-1,57,58,59,60,61,62,63,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,130,131,132,-2,32767,65535,204,1006,1007,1010,200,201,202,203,207,208,1002,1003,1004,1005,1008,1011,1012,1013,1014,1015,1016,1018,1019,1020,1022,1023,1024,1025,1026,1027,1028,1029,1030,1031,1032,1033,1034,1035,1036,205,206,1001,1017,1021,4094,4095,4097,4098,4099,4100,4101,4108,4127,4131,4146,4354,4528,4530,4532,2304,2305,2306,2307,2308,4093,4139,4140,4141,4142,4143,4144,4157,4159,117,119,120,121,122,123,151,152,153,154,155,156,157,158,159,160,161,162,251,252,253,254,256,257,258,259,260,261,262,263,264,265,266,267,268,269,301,302,303,304,305,306,307,308,309,310,311,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,401,402,403,404,405,406,407,408,409,410,411,412,451,452,453,454,455,456,457,458,459,460,461,462,501,502,503,504,505,506,551,552,553,554,1009,1037,1038,1039,1040,1045,1046,1144,1145,16385,16386,16387,16388,16390,16392,16396,16408,32769,49153};
If you have something similar and each type of data is handled (in the example only double and enum because its an example) and
you want to turn the value to text automatically , i.e. new value , instant format to text ,why is it not allowed to use the function for enumerations (EnumToString) when you create a double type instance of the class .
Can't it assume i'm handling it by types ?
Thanks
Edit , the opposite is allowed :
You can do that. Then just call the function in any class, struct, anywhere. Of course, just for enum and double. If you need other types like int or bool, will need add new functions for them.
For this case I have created a class "cToString". It's basically the same as what Samuel described. See the attached script for usage.
For debugging, I have collected some nice macros, and I'd like to share them with you:
To print an expression or variable along with its name
As the above, but prints with a nicer and more accurate formatting:
Demo
For printing enumeration values:
For debugging, I have collected some nice macros, and I'd like to share them with you:
To print an expression or variable along with its name
As the above, but prints with a nicer and more accurate formatting:
Demo
For printing enumeration values:
Nice , thank you @amrali
You can do that. Then just call the function in any class, struct, anywhere. Of course, just for enum and double. If you need other types like int or bool, will need add new functions for them.
this
and
For this case I have created a class "cToString". It's basically the same as what Samuel described. See the attached script for usage.
this are the solutions
It appears if you place the double (to text) before the template (to text) the editor is okay with it because it comes first .(for these 2 types , i will test will all others)
@Samuel Manoel De Souza
@Petr Nosek
Thank you very much .
I will go through with catching all the possible enum ints , for the fun of it , but your suggestions seem to be the solution.
[concerning the enumapper - the original issue is solved]
There are some undocumented enumerations among the misscounts of course
for instance in ENUM_ACTIVATION_FUNCTION there is no AF_PRELU in the docs
or the ENUM_DATATYPE
or ENUM_TERMINAL_INFO_STRING
here are all the possible enum item values , excluding some ENUM_DX_FORMAT values and ENUM_GEMM .