struct inheritance in Mql5 - page 2

 

What about defining setters/getters for each individual struct to the hub?

class CEventHub
  {
   private:
      CEvent        *m_event;
   public:
      void           SetEvent(MqlEventMouseMove &evstruct) { m_event=new CEventMouseMove(evstruct); }
      void           GetEvent(MqlEventMouseMove &evstruct) { CEventMouseMove *p=m_event; evstruct=p.data; }
      void           SetEvent(MqlEventMessage &evstruct)   { m_event=new CEventMessage(evstruct); }
      void           GetEvent(MqlEventMessage &evstruct)   { CEventMessage *p=m_event; evstruct=p.data; }
  };


So it's only the hub that utilizes the event classes for wrapping the data - outside of it the program works with the structs.

enum ENUM_EVENT_TYPE
  {
   EVENT_MOUSE_MOVE,
   EVENT_MESSAGE
  };

struct MqlEventMouseMove
  {
   int               x;
   int               y;
  };
struct MqlEventMessage
  {
   string            message;
  };

class CEvent
  {
private:
   ENUM_EVENT_TYPE   event_type;
protected:
                     CEvent(const ENUM_EVENT_TYPE type) {event_type=type;}
public:
   ENUM_EVENT_TYPE   EventType() const {return event_type;}
  };

class CEventMouseMove : public CEvent
  {
public:
   MqlEventMouseMove data;
                     CEventMouseMove(MqlEventMouseMove &ev) : CEvent(EVENT_MOUSE_MOVE) {data=ev;}
  };

class CEventMessage : public CEvent
  {
public:
   MqlEventMessage data;
                     CEventMessage(MqlEventMessage &ev) : CEvent(EVENT_MESSAGE) {data=ev;}
  };
  
class CEventHub
  {
   private:
      CEvent        *m_event;
   public:
      void           SetEvent(MqlEventMouseMove &evstruct) { m_event=new CEventMouseMove(evstruct); }
      void           GetEvent(MqlEventMouseMove &evstruct) { CEventMouseMove *p=m_event; evstruct=p.data; }
      void           SetEvent(MqlEventMessage &evstruct)   { m_event=new CEventMessage(evstruct); }
      void           GetEvent(MqlEventMessage &evstruct)   { CEventMessage *p=m_event; evstruct=p.data; }
      bool           EventType(ENUM_EVENT_TYPE &evtype)    { if(CheckPointer(m_event)==POINTER_DYNAMIC) { evtype=m_event.EventType(); return true; } return false; }
  };

CEventHub hub;

int OnInit()
  {
   MqlEventMouseMove memm = {40,50};
   MqlEventMessage memsg = {"text"};
  
   //--- send mouse event
   hub.SetEvent(memm);
   
   //--- get mouse event
   MqlEventMouseMove memm2;
   hub.GetEvent(memm2);
   Print("Mouse x=",memm2.x," Mouse y=",memm2.y);
   
   //--- send message
   hub.SetEvent(memsg);

   //--- get message
   MqlEventMessage memsg2;
   hub.GetEvent(memsg2);
   Print("Message=",memsg2.message);
 
lippmaje:

What about defining setters/getters for each individual struct to the hub?


So it's only the hub that utilizes the event classes for wrapping the data - outside of it the program works with the structs.

Worth considering. It also allows transfering the event classes to internal private classes inside the hub.
 
Amir Yacoby:
Worth considering. It also allows transfering the event classes to internal private classes inside the hub.
Yup. Memory management / pointer handling would be also encapsuled.
Reason: