사전 정의된 매크로 대체

디버깅 프로세스를 단순화하고 mql5 프로그램의 작동에 대한 정보를 얻기 위해 컴파일 시 설정되는 특수 매크로 상수가 있습니다. 이 상수를 사용하는 가장 쉬운 방법은 예제외 같이 Print() 함수로 값을 출력하는 것입니다.

정수

설명

__CPU_ARCHITECTURE__

컴파일된 아키텍처 이름(명령 집합) EX5 파일

__DATE__

시간이 없는 파일 컴파일 날짜(시간, 분 및 초는 0)

__DATETIME__

파일 컴파일 날짜 및 시간

__LINE__

매크로가 위치한 소스 코드의 라인 번호

__FILE__

현재 컴파일된 파일의 이름

__PATH__

현재 컴파일되고 있는 파일의 절대 경로입니다

__FUNCTION__

매크로가 위치한 본문의 함수 이름

__FUNCSIG__

매크로가 위치한 본문의 함수 서명입니다. 기능의 전체 설명을 로깅하는 것은 과부하된 기능 식별에 유용할 수 있습니다.

__MQLBUILD__,__MQL5BUILD__

컴파일러 빌드 번호

__COUNTER__

The compiler for each encountered __COUNTER__ declaration substitutes the counter value from 0 to N-1 where N is a number of uses in the code.  The __COUNTER__ order is guaranteed when recompiling the source code with no changes.

The __COUNTER__ value is calculated the following way:

  • the initial counter value is 0,
  • after each counter usage, its value is increased by 1,
  • first, the compiler expands all macros and templates into source code on-site,
  • a separate code is created for each template function specialization,
  • a separate code is created for each template class/structure specialization,
  • next, the compiler passes through the obtained source code in the defined order and replaces each detected __COUNTER__ usage with the current counter value.

The example below shows how the compiler handles the source code and replaces all instances of __COUNTER__ it meets with sequentially increasing values.

__RANDOM__

The compiler inserts a random ulong value for each __RANDOM__ declaration.

예:

#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net"
//+------------------------------------------------------------------+
//| 엑스퍼트 초기화 기능                                               |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- 엑스퍼트 어드바이저 초기화 시 정보 출력 예
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__);
//--- 타이머 이벤트 간 간격 설정
   EventSetTimer(5);
//---
  }
//+------------------------------------------------------------------+
//| 엑스퍼트 초기화 취소 함수                                           |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- 엑스퍼트 어드바이저 초기화 취소 시 정보 출력의 예
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__);
//---
  }
//+------------------------------------------------------------------+
//| 엑스퍼트 틱 기능                                                   |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 틱 수신 시 정보 출력
   Print(" __MQLBUILD__ = ",__MQLBUILD__,"  __FILE__ = ",__FILE__);
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__);
   test1(__FUNCTION__);
   test2();
//---
  }
//+------------------------------------------------------------------+
//| test1                                                            |
//+------------------------------------------------------------------+
void test1(string par)
  {
//--- 함수 내 정보 출력
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__," par = ",par);
  }
//+------------------------------------------------------------------+
//| test2                                                            |
//+------------------------------------------------------------------+
void test2()
  {
//--- 함수 내 정보 출력
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__);
  }
//+------------------------------------------------------------------+
//| OnTimer 이벤트 처리기                                             |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   Print(" __FUNCTION__ = ",__FUNCTION__,"  __LINE__ = ",__LINE__);
   test1(__FUNCTION__);
  }

 

The example for learning how to work with the __COUNTER__ macro

//--- create a macro for a quick display of the expression and its value in the journal
#define print(exprPrint(#expr,"=",expr)
 
//--- define the MACRO_COUNTER custom macro via the predefined __COUNTER__ macro
#define MACRO_COUNTER __COUNTER__
 
//--- set the input value of the variable using the __COUNTER__ macro
input int InpVariable = __COUNTER__;
 
//--- set the value of the global variable using the __COUNTER__ macro before defining the functions
int ExtVariable = __COUNTER__;
 
//+------------------------------------------------------------------+
//| the function returns the __COUNTER__ value                       |
//+------------------------------------------------------------------+
int GlobalFunc(void)
  {
   return(__COUNTER__);
  }
//+------------------------------------------------------------------+
//| the template function returns the __COUNTER__ value              |
//+------------------------------------------------------------------+
template<typename T>
int GlobalTemplateFunc(void)
  {
   return(__COUNTER__);
  }
//+------------------------------------------------------------------+
//| the structure with the method returning __COUNTER__              |
//+------------------------------------------------------------------+
struct A
  {
   int               dummy;  // not used
 
   int               Method(void)
     {
      return(__COUNTER__);
     }
  };
//+------------------------------------------------------------------+
//| the template structure with the method returning __COUNTER__     |
//+------------------------------------------------------------------+
template<typename T>
struct B
  {
   int               dummy;  // not used
 
   int               Method(void)
     {
      return(__COUNTER__);
     }
  };
//+------------------------------------------------------------------+
//| the structure with the template method returning __COUNTER__     |
//+------------------------------------------------------------------+
struct C
  {
   int               dummy;  // not used
 
   template<typename T>
   int               Method(void)
     {
      return(__COUNTER__);
     }
  };
//+------------------------------------------------------------------+
//| the function #2, which returns the __COUNTER__ value             |
//+------------------------------------------------------------------+
int GlobalFunc2(void)
  {
   return(__COUNTER__);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(void)
  {
// __COUNTER__ in the macro and the variables
   print(MACRO_COUNTER);
   print(InpVariable);
   print(ExtVariable);
 
//--- __COUNTER__ in the functions
   print(GlobalFunc());
   print(GlobalFunc());                // the value is not changed
   print(GlobalTemplateFunc<int>());
   print(GlobalTemplateFunc<int>());   // the value is not changed
   print(GlobalTemplateFunc<double>());// the value has changed
   print(GlobalFunc2());
   print(GlobalFunc2());               // the value is not changed
 
// __COUNTER__ in the structure
   A a1a2;
   print(a1.Method());
   print(a2.Method());                 // the value is not changed
 
// __COUNTER__ in the template structure
   B<intb1b2;
   B<doubleb3;
   print(b1.Method());
   print(b2.Method());                 // the value is not changed
   print(b3.Method());                 // the value has changed
 
// __COUNTER__ in the structure with the template function
   C c1c2;
   print(c1.Method<int>());
   print(c1.Method<double>());         // the value has changed
   print(c2.Method<int>());            // the same value as during the first c1.Method<int>() call
 
//--- let's have another look at __COUNTER__ in the macro and the global variable
   print(MACRO_COUNTER);  // the value has changed
   print(ExtGlobal2);
  }
//--- set the value of the global variable using the __COUNTER__ macro after defining the functions
int ExtGlobal2 = __COUNTER__;
//+------------------------------------------------------------------+
 
/* Result
   __COUNTER__=3
   InpVariable=0
   ExtVariable=1
   GlobalFunc()=5
   GlobalFunc()=5
   GlobalTemplateFunc<int>()=8
   GlobalTemplateFunc<int>()=8
   GlobalTemplateFunc<double>()=9
   GlobalFunc2()=7
   GlobalFunc2()=7
   a1.Method()=6
   a2.Method()=6
   b1.Method()=10
   b2.Method()=10
   b3.Method()=11
   c1.Method<int>()=12
   c1.Method<double>()=13
   c2.Method<int>()=12
   __COUNTER__=4
   ExtGlobal2=2
 
*/