MQL5源码解读:CObject类

MQL标准库文件 Include/Object.mqh 中有这么一个类 CObject,作为 MQL中的对象基类存在。

Source : Include/Object.mqh

//+------------------------------------------------------------------+
//| Object.mqh |
//| Copyright 2009-2013, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#include "StdLibErr.mqh"
//+------------------------------------------------------------------+
//| Class CObject. |
//| Purpose: Base class for storing elements. |
//+------------------------------------------------------------------+
class CObject
  {
private:
   CObject          *m_prev;               // previous item of list
   CObject          *m_next;               // next item of list

public:
                     CObject(void): m_prev(NULL),m_next(NULL)            {                 }
                    ~CObject(void)                                       {                 }
   //--- methods to access protected data
   CObject          *Prev(void)                                    const { return(m_prev); }
   void              Prev(CObject *node)                                 { m_prev=node;    }
   CObject          *Next(void)                                    const { return(m_next); }
   void              Next(CObject *node)                                 { m_next=node;    }
   //--- methods for working with files
   virtual bool      Save(const int file_handle)                         { return(true);   }
   virtual bool      Load(const int file_handle)                         { return(true);   }
   //--- method of identifying the object
   virtual int       Type(void)                                    const { return(0);      }
   //--- method of comparing the objects
   virtual int       Compare(const CObject *node,const int mode=0) const { return(0);      }
  };
//+------------------------------------------------------------------+

类 CObject

这个类还是比较简单的。
对于每一个 CObject 对象,私有保存其前驱、后继的指针:m_prevm_next
公开如下方法:

方法名 参数列表 返回值 意义
CObject void 默认构造函数:使其前驱、后继均为 NULL
Prev void CObject* 返回前驱的指针
Prev CObject* node void 设置前驱为node
Next void CObject* 返回后继的指针
Next CObject* node void 设置后继为node
Save const int file_handle bool 保存到句柄为file_handle文件里,并返回是否成功
Load const int file_handle bool 从句柄为file_handle的文件里读取数据,并返回是否成功
Type void int 对象类型标识
Compare const CObject *node,const int mode=0 int 与另一对象进行比较,默认使用模式0

其实我觉得以上方法本身其实是没有太大用处的,因为这个只是一个基类,它定义了所有MQL对象的公有属性,不可避免地产生笼统的释义。

但是不管怎样,我们可以看到,在MQL中的对象所具有的一些特性:

  • 双向链表结构,任意的对象都具有直接访问前驱后继的可能,因此在MQL中,逐一顺序访问前驱后继是非常简单的事,在列表的末尾添加一个新的对象也是一件非常方便的事。可以看出来MQL是针对交易系统作了优化的,时间序列 具有的特性就应该使用链表的结构来存储,因为它并对“访问一个特定的位置”这个操作并不敏感,更多的是“聚合(aggregate)从某一个位置往前若干项的数据”这样的操作。
  • 文件读写,MQL可能是按对象(或其列表)这样的粒度来存储数据的。一般看来,一组时间序列可能是存放在一个文件内的。
  • 异质性,MQL可能定义了一套MQL对象序列(类似BSON于JSON的做法),来规范可能存放的对象类型,并且,在MQL的底层列表中,元素之间的类型可能是不一致的(异质链表)
  • 可比较性,MQL对象具有比较的可能,这从数学上就定义了一种对象之间的半序关系。

你可能感兴趣的:(源码,mql)