OOP, plantillas y macros en mql5, sutilezas y usos - página 22

 
fxsaber:

Gracias.

Pero no he descubierto cómo anular la macro

Aquí está el código:

#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;
}

ahora funciona, pero con el prefijoFAST_ , quiero anularMACROS_PREFIX , pero para que se llame aconnect(string host = "localhost",int port = 6379)

 
Igor Makanu:

Quiero anularMACROS_PREFIX pero llamar aconnect(string host = "localhost", int port = 6379)

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

Gracias.

Pero no he descubierto cómo anular la macro

Aquí está el código:

ahora funciona, pero con el prefijoFAST_ , quiero anularMACROS_PREFIX , pero para que sellame aconnect(string host = "localhost", int port = 6379)

#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)();
}
 

Es extraño que funcione, pero no lo hace.

F_CALL()
 
fxsaber:

Es extraño que funcione, pero no lo hace.

Mi macro tiene dos parámetros, pero se pasa uno, por lo que el compilador jura.

 

Gracias... pero todavía no lo he descubierto.... ¡¡¡¡ciencia del infierno!!!!

No puedo explicarlo con palabras. Lo intentaré en lenguaje de notas.

Este código funciona como se pretende:

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();
   }
//+------------------------------------------------------------------+

Pero quiero hacer una sustitución de macros en MQL para minimizar el texto del código en sí, por lo que necesito:

#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:

Mi macro tiene dos parámetros y tú pasas uno, por lo que el compilador se pelea

No funciona.

   F_CALL(,)
 
fxsaber:

No funciona.

Por supuesto que sí. Lo tienes expandiendo en _
 
Igor Makanu:

Gracias... pero todavía no lo he descubierto.... ¡¡¡¡ciencia del infierno!!!!

No puedo explicarlo con palabras. Lo intentaré en lenguaje de notas.

Este código funciona como se pretende:

pero quiero hacer una sustitución de macros en MQL para minimizar el texto del código en sí, así que lo necesito:

#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(...);
   }
//+------------------------------------------------------------------+

¡¡¡PERO!!!

La macro CONNECT no puede ser anulada haciendo #undef USE_FAST

 

para poder hacer lo que quiera.

#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();
   }
//+------------------------------------------------------------------+


interesado en las opciones sugeridas ayer usando el hechizo mágico #defaine abracadabra ##_

UPD:

Vladimir Simakov OK, trataré de entenderlo

UPD:

¡HOORAY! ¡FUNCIONÓ! (С)

#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();
   }
//+------------------------------------------------------------------+


¡Gracias! ¡Funcionó como estaba previsto!


PZY: ¿Cómo podría añadir macros __MQL5__ y __MQL4__ a este abracadabra, pero las funciones fast_xxx() nunca estarán en el código MQL4 en absoluto - desde la palabra NUNCA

Razón de la queja: