EnumToString and turning values to string in a template class - page 2

 
Lorentzos Roussos:

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.

template <typename T>
string ToString(T value) {return EnumToString((T)value);}
string ToString(double value) {return DoubleToString(value, 2);}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print("using double: ", ToString(1.53));
   Print("using ENUM_POSITION_TYPE: ", ToString(POSITION_TYPE_BUY));
//---
   return(INIT_SUCCEEDED);
  }
 
You can use the above functions on your set method, instead of check the typename.
 

For this case I have created a class "cToString". It's basically the same as what Samuel described. See the attached script for usage.

#define TOSTR(x) #x + "=" + (string) (x)

class cToString
  {
public:
   template <typename T>
   string   ToString(const T val)         { return(EnumToString((T)val));                       }
   string   ToString(const double val)    { return(DoubleToString(val));                        }
   string   ToString(const float val)     { return(DoubleToString(val));                        }
   string   ToString(const char val)      { return(IntegerToString(val));                       }
   string   ToString(const uchar val)     { return(IntegerToString(val));                       }
   string   ToString(const short val)     { return(IntegerToString(val));                       }
   string   ToString(const ushort val)    { return(IntegerToString(val));                       }
   string   ToString(const int val)       { return(IntegerToString(val));                       }
   string   ToString(const uint val)      { return(IntegerToString(val));                       }
   string   ToString(const long val)      { return(IntegerToString(val));                       }
   string   ToString(const ulong val)     { return(IntegerToString(val));                       }
   string   ToString(const color val)     { return(ColorToString(val));                         }
   string   ToString(const datetime val)  { return(TimeToString(val,TIME_DATE|TIME_SECONDS));   }
   string   ToString(const bool val)      { return(string(val));                                }
   string   ToString(const string val)    { return(string(val));                                }
  };


template <typename X>
class cAnything
  {
private:
   X           value;
   string      asText;
   cToString   toStr;
public:
   cAnything(void)                        { asText=NULL;                                        }
   void     Set(const X newValue)         { value=newValue;asText=toStr.ToString(value);        }
   string   Text(void)                    { return(asText);                                     }
   void     Print(void)                   { Print(typename(X)+" - "+TOSTR(asText));             }
  };



void OnStart()
  {
//---
  
  cAnything <double>f1;
  double v1=1.23;
  f1.Set(v1);
  f1.Print();
  
  cAnything <ENUM_APPLIED_PRICE>f2;
  ENUM_APPLIED_PRICE v2=PRICE_TYPICAL;
  f2.Set(v2);
  f2.Print();
  
  cAnything <bool>f3;
  bool v3=true;
  f3.Set(v3);
  f3.Print();
  
  cAnything <string>f4;
  string v4="Text";
  f4.Set(v4);
  f4.Print();
  
  cAnything <color>f5;
  color v5=clrAliceBlue;
  f5.Set(v5);
  f5.Print();
  
  cAnything <ulong>f6;
  ulong v6=456;
  f6.Set(v6);
  f6.Print();
  
  cAnything <datetime>f7;
  datetime v7=TimeCurrent();
  f7.Set(v7);
  f7.Print();
  
  }
 

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

//--- simple versions
#define TOSTR(A) (#A + "=" + (string)(A) + " ")
#define PRINT(A) Print(#A + " = ", (A))

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(const double val)     { return(string(val));               }  // fixed (string)dbl bug in recent versions
string   ToString(const float val)      { return(Repr(val) + "f");           }  // because (string)float bug is still NOT fixed!
string   ToString(const char val)       { return(string(val));               }
string   ToString(const uchar val)      { return(string(val));               }
string   ToString(const short val)      { return(string(val));               }
string   ToString(const ushort val)     { return(string(val));               }
string   ToString(const int val)        { return(string(val));               }
string   ToString(const uint val)       { return(string(val));               }
string   ToString(const long val)       { return(string(val));               }
string   ToString(const ulong val)      { return(string(val));               }
string   ToString(const bool val)       { return(string(val));               }
string   ToString(const color val)      { return(string(val));               }
string   ToString(const datetime val)   { return("D'" + string(val)) + "'";  }
string   ToString(const string 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

void OnStart()
  {
   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("  };");
  }

void OnStart()
  {
   PrintEnum<ENUM_TIMEFRAMES>();
   PrintEnum<ENUM_ACCOUNT_INFO_DOUBLE>();
   PrintEnum<ENUM_FP_CLASS>();      // the lowest enum value
   PrintEnum<ENUM_CHART_EVENT>();   // the highest enum value
  }
 
amrali #:

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

 
Samuel Manoel De Souza #:

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 

Petr Nosek #:

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.

template <typename X>
class anything{
X value;
string as_text;
  public:
  anything(void){as_text=NULL;}
  void set(X _new_value){
  value=_new_value;
  text_me(_new_value);
  }
  string text(){return(as_text);}
  private:
  void text_me(double _value){as_text=DoubleToString(_value,2);}
  template <typename F>
  void text_me(F _value){as_text=EnumToString(_value);}
};
int OnInit()
  {
//---
  
  anything <double>f1;
  double v1=1.23;
  f1.set(v1);
  Print(f1.text());
  
  
  anything <ENUM_APPLIED_PRICE>f2;
  ENUM_APPLIED_PRICE v2=PRICE_TYPICAL;
  f2.set(v2);
  Print(f2.text());
  
//---
   return(INIT_SUCCEEDED);
  }
 

[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


Documentation on MQL5: Matrix and Vector Methods / Matrix and Vector Types / Enumerations
Documentation on MQL5: Matrix and Vector Methods / Matrix and Vector Types / Enumerations
  • www.mql5.com
Enumerations - Matrix and Vector Types - Matrix and Vector Methods - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Data Type Identifiers
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Data Type Identifiers
  • www.mql5.com
Data Type Identifiers - Indicator Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Client Terminal Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Client Terminal Properties
  • www.mql5.com
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};