//+------------------------------------------------------------------+
//| MyTreeNode.mq5 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "2010, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
//---
#include <Arrays\TreeNode.mqh>
//+------------------------------------------------------------------+
//| Describe classderived from CTreeNode. |
//+------------------------------------------------------------------+
//| Class CMyTreeNode. |
//| Purpose: Class of element of a binary tree. |
//| Descendant of class CTreeNode. |
//+------------------------------------------------------------------+
class CMyTreeNode : public CTreeNode
{
protected:
//--- user's data
long m_long; // key field of type long
double m_double; // custom variable of type double
string m_string; // custom variable of type string
datetime m_datetime; // custom variable of type datetime
public:
CMyTreeNode();
//--- methods of accessing these user's data
long GetLong(void) { return(m_long); }
void SetLong(long value) { m_long=value; }
double GetDouble(void) { return(m_double); }
void SetDouble(double value) { m_double=value; }
string GetString(void) { return(m_string); }
void SetString(string value) { m_string=value; }
datetime GetDateTime(void) { return(m_datetime); }
void SetDateTime(datetime value) { m_datetime=value; }
//--- methods of working with files
virtual bool Save(int file_handle);
virtual bool Load(int file_handle);
protected:
virtual int Compare(const CObject *node,int mode);
//--- methods of creating class instances
virtual CTreeNode* CreateSample();
};
//+------------------------------------------------------------------+
//| CMyTreeNode class constructor. |
//| INPUT: none. |
//| OUTPUT: none. |
//| REMARK: none. |
//+------------------------------------------------------------------+
void CMyTreeNode::CMyTreeNode()
{
//--- initialization of user's data
m_long =0;
m_double =0.0;
m_string ="";
m_datetime =0;
}
//+------------------------------------------------------------------+
//| Comparison with another three node by the specified algorithm. |
//| INPUT: node - array element to compare, |
//| mode - identifier of comparison algorithm. |
//| OUTPUT: result of comparison (>0,0,<0). |
//| REMARK: none. |
//+------------------------------------------------------------------+
int CMyTreeNode::Compare(const CObject *node,int mode)
{
//--- parameter mode is ignored, because tree construction algorithm is the only one
int res=0;
//--- explicit type casting
CMyTreeNode *n=node;
res=(int)(m_long-n.m_long);
//---
return(res);
}
//+------------------------------------------------------------------+
//| Creation of a new class instance. |
//| INPUT: none. |
//| OUTPUT: pointer to a new instance of class CMyTreeNode. |
//| REMARK: none. |
//+------------------------------------------------------------------+
CTreeNode* CMyTreeNode::CreateSample()
{
CMyTreeNode *result=new CMyTreeNode;
//---
return(result);
}
//+------------------------------------------------------------------+
//| Write tree node data to a file. |
//| INPUT: file_handle -handle of a file pre-opened for writing. |
//| OUTPUT: true if OK, otherwise false. |
//| REMARK: none. |
//+------------------------------------------------------------------+
bool CMyTreeNode::Save(int file_handle)
{
uint i=0,len;
//--- checks
if(file_handle<0) return(false);
//--- writing user data
//--- writing custom variable of type long
if(FileWriteLong(file_handle,m_long)!=sizeof(long)) return(false);
//--- writing custom variable of type double
if(FileWriteDouble(file_handle,m_double)!=sizeof(double)) return(false);
//--- writing custom variable of type string
len=StringLen(m_string);
//--- write string length
if(FileWriteInteger(file_handle,len,INT_VALUE)!=INT_VALUE) return(false);
//--- write the string
if(len!=0 && FileWriteString(file_handle,m_string,len)!=len) return(false);
//--- writing custom variable of type datetime
if(FileWriteLong(file_handle,m_datetime)!=sizeof(long)) return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Read tree node data from a file. |
//| INPUT: file_handle -handle of a file pre-opened for reading. |
//| OUTPUT: true if OK, otherwise false. |
//| REMARK: none. |
//+------------------------------------------------------------------+
bool CMyTreeNode::Load(int file_handle)
{
uint i=0,len;
//--- checks
if(file_handle<0) return(false);
//--- reading
if(FileIsEnding(file_handle)) return(false);
//--- reading custom variable of type char
//--- reading custom variable of type long
m_long=FileReadLong(file_handle);
//--- reading custom variable of type double
m_double=FileReadDouble(file_handle);
//--- reading custom variable of type string
//--- read the string length
len=FileReadInteger(file_handle,INT_VALUE);
//--- read the string
if(len!=0) m_string=FileReadString(file_handle,len);
else m_string="";
//--- reading custom variable of type datetime
m_datetime=FileReadLong(file_handle);
//---
return(true);
}
|