OOP, templates and macros in mql5, subtleties and uses - page 22

 
fxsaber:

Thank you!

But haven't figured out how to override the macro

Here's the code:

#define  MACROS_PREFIX(A) FAST_##A

bool Connect(string host = "localhost", int port = 6379)
   {
   return(MACROS_PREFIX(connect)(host, port));
   }

   
   
bool FAST_connect(string host = "localhost", int port = 6379)
{
return true;
}

bool connect(string host = "localhost", int port = 6379)
{
return true;
}

it works now, but with the prefixFAST_ , I want to overrideMACROS_PREFIX , but so thatconnect(string host = "localhost",int port = 6379) wascalled

 
Igor Makanu:

I want to overrideMACROS_PREFIX but callconnect(string host = "localhost", int port = 6379)

#undef  MACROS_PREFIX
#define  MACROS_PREFIX(A) A
 
Igor Makanu:

Thank you!

But haven't figured out how to override the macro

Here's the code:

it works now, but with the prefixFAST_ , I want to overrideMACROS_PREFIX , but so thatconnect(string host = "localhost", int port = 6379) wascalled

#define  F_CALL(dPrefix,dFunc) dPrefix##_##dFunc

void Foo()  {Alert("Null");}
void _Foo() {Foo();}
void SomePref_Foo()  {Alert("SomeFunc");}

void OnStart(){
   F_CALL(,Foo)();
   F_CALL(SomePref,Foo)();
}
 

It is strange that it works, but it does not.

F_CALL()
 
fxsaber:

It's strange that it works, but it doesn't.

My macro has two parameters, but you pass one, so the compiler swears.

 

thanks... but I still haven't figured it out.... science from hell!!!!

I can't explain it in words. I'll try it in note language.

This code works as intended:

bool USE_SAVE_MODE = true;                      // проверка пинга
bool ping() { return(true); }                   // заглушка 
bool connect(string h,int p) { return(true);}   // заглушка 
//+------------------------------------------------------------------+
bool ExPing()
   {
   if(USE_SAVE_MODE)
      {
      Print("safe mode on");
      return(ping());
      }
   else
      {
      Print("safe mode off");
      return(true);
      }

   }
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return ExPing() && connect(host, port);
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
      USE_SAVE_MODE = false; // отключили проверку пинга
      Connect();
   }
//+------------------------------------------------------------------+

But I want to make a macro substitution in MQL to minimize the code text itself, so I have to:

#define   USE_FAST                               // тут нужно что-нибудь сделать!
bool connect(string h,int p)      {    }        // код 1
bool fast_connect(string h,int p) {    }        // код 2
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return connect(host, port);                  // вот тут нужен префикс и будет или connect(host, port);  или fast_connect(host, port); 
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+
 
Vladimir Simakov:

My macro has two parameters and you pass one, which is why the compiler is fighting

It doesn't work.

   F_CALL(,)
 
fxsaber:

It doesn't work.

Of course it does. You have it expanding in _
 
Igor Makanu:

thanks... but I still haven't figured it out.... science from hell!!!!

I can't explain it in words. I'll try it in note language.

This code works as intended:

but i want to do a macro substitution in MQL to minimize the code text itself, so i need it:

#define   USE_FAST                               // тут нужно что-нибудь сделать!
#ifdef  USE_FAST
   #define  CONNECT fast_connect
#else
   #define  CONNECT connect
#endif
bool connect(string h,int p)      {    }        // код 1
bool fast_connect(string h,int p) {    }        // код 2
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return CONNECT(host, port);                  // вот тут нужен префикс и будет или connect(host, port);  или fast_connect(host, port); 
   }
//+------------------------------------------------------------------+
void OnStart()
   {
      Connect(...);
   }
//+------------------------------------------------------------------+

BUT!!!

CONNECT macro cannot be overridden by making #undef USE_FAST

 

so I can do what I want.

#define   USE_FAST
bool connect(string h,int p)      { Print("On");   return true; }
bool fast_connect(string h,int p) { Print("Off");   return true;}
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   #ifdef  USE_FAST
   return connect(host, port);
   #else return fast_connect(host, port);
   #endif 
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+


interested in options suggested yesterday using the magic spell #defaine abracadabra ##_

UPD:

Vladimir Simakov OK, I'll try to figure it out

UPD:

HOORAY! IT WORKED! (С)

#define   USE_FAST
#ifdef  USE_FAST
   #define  CONNECT fast_connect
#else
   #define  CONNECT connect
#endif

bool connect(string h,int p)      { Print("On");   return true; }
bool fast_connect(string h,int p) { Print("Off");   return true;}
//+------------------------------------------------------------------+
bool Connect(string host = "localhost", int port = 6379)
   {
   return CONNECT(host, port);
   }

//+------------------------------------------------------------------+
void OnStart()
   {
      Connect();
   }
//+------------------------------------------------------------------+


Thank you! It worked as intended!


PZY: How would I add __MQL5__ and __MQL4__ macros to this abracadabra, but fast_xxx() functions will never be in MQL4 code at all - from the word NEVER