A
Ash
Hello all,
I am hoping this is the appropriate newsgroup for a C++ interface
design question. I am trying to design an interface for a subscriber
to register/deregister handlers for various events. The callbacks
specified by the subscriber will be called when the events get
trigerred in a different thread. Each event has different kinds of
data associated with it. To achieve this I have the following:
// The following describes the event types
enum EventTypeEnum
{
EVENT_TYPEA.
EVENT_TYPEB,
EVENT_TYPEC,
EVENT_TYPE_LAST
};
// The following defines the event objects of each type
class EventTypeObj
{
public:
EventTypeObj (EventTypeEnum evtType);
~EventTypeObj ();
// additional methods to get/set various attributes on event objects
};
// On event triggering the following kinds of data will be passed in
// to the handler
class EventTypeAData;
clsss EventTypeBData;
class EventTypeCData;
// The following interface needs to be implemented by each subscriber
// to handle events
class EventHandlerInterface
{
public:
virtual void HandleEventA (EventTypeAData info) = 0;
virtual void HandleEventB (EventTypeBData info) = 0;
virtual void HandleEventC (EventTypeBData info) = 0;
};
So, the subscriber can sub-class the event handler interface to
implement their own logic of handling the events. Now, how the caller
can subscribe to the events:
// Object encapsulates the registration and de-registration of event
class EventRegistry
{
private:
// ctor and dtor are private because a single instance
// exists in every process
EventRegistry ();
~EventRegistry ();
// To register for an event, specify the type of the event
// and the object that is handling the event
// May be the evnet handler interface object should be recounted
// so that if the subscriber deletes the object we don't deliver
the event
void RegisterEvent (EventTypeEnum evtType,
EventHandlerInterface *hndlrObj);
void DeRegisterEvent (EventTypeEnum evtType,
EventHandlerInterface *hndlrObj);
};
The good thing about the above subscriber interface is that all the
event handlers are encapsulated in a single object. The bad thing is
if we add new events everyone has to recompile but since this is only
for internal use so it is ok for now. I could have provided separate C
method signatures for each type of event but I prefer providing a
handler interface. Also, I tried using functors to encapsulate the
events but I wasn't sure how to and so I didn't. What do people think
about the above interface? Any thoughts/suggestions would be
appreciated.
Regards,
Ash
I am hoping this is the appropriate newsgroup for a C++ interface
design question. I am trying to design an interface for a subscriber
to register/deregister handlers for various events. The callbacks
specified by the subscriber will be called when the events get
trigerred in a different thread. Each event has different kinds of
data associated with it. To achieve this I have the following:
// The following describes the event types
enum EventTypeEnum
{
EVENT_TYPEA.
EVENT_TYPEB,
EVENT_TYPEC,
EVENT_TYPE_LAST
};
// The following defines the event objects of each type
class EventTypeObj
{
public:
EventTypeObj (EventTypeEnum evtType);
~EventTypeObj ();
// additional methods to get/set various attributes on event objects
};
// On event triggering the following kinds of data will be passed in
// to the handler
class EventTypeAData;
clsss EventTypeBData;
class EventTypeCData;
// The following interface needs to be implemented by each subscriber
// to handle events
class EventHandlerInterface
{
public:
virtual void HandleEventA (EventTypeAData info) = 0;
virtual void HandleEventB (EventTypeBData info) = 0;
virtual void HandleEventC (EventTypeBData info) = 0;
};
So, the subscriber can sub-class the event handler interface to
implement their own logic of handling the events. Now, how the caller
can subscribe to the events:
// Object encapsulates the registration and de-registration of event
class EventRegistry
{
private:
// ctor and dtor are private because a single instance
// exists in every process
EventRegistry ();
~EventRegistry ();
// To register for an event, specify the type of the event
// and the object that is handling the event
// May be the evnet handler interface object should be recounted
// so that if the subscriber deletes the object we don't deliver
the event
void RegisterEvent (EventTypeEnum evtType,
EventHandlerInterface *hndlrObj);
void DeRegisterEvent (EventTypeEnum evtType,
EventHandlerInterface *hndlrObj);
};
The good thing about the above subscriber interface is that all the
event handlers are encapsulated in a single object. The bad thing is
if we add new events everyone has to recompile but since this is only
for internal use so it is ok for now. I could have provided separate C
method signatures for each type of event but I prefer providing a
handler interface. Also, I tried using functors to encapsulate the
events but I wasn't sure how to and so I didn't. What do people think
about the above interface? Any thoughts/suggestions would be
appreciated.
Regards,
Ash