Consider this syntactically correct(?) pseudocode:
class Event {
public:
virtual int getID() const = 0;
virtual int getSize() = 0;
virtual void* getData() = 0;
virtual void setData() = 0;
//(I cannot define data at this level 'cos I don't know what it is yet)
}
class SpecialEvent : class Event {
public:
virtual int getPGNID() const = 0;
int getSourceAddress() {return source_address;}
int setSourceAddress(int source_address) {this->source_address = source_address;}
protected:
int source_address;
}
template <typename T, typename E>
class EventWrapper : public E {
T data;
public:
static int EVENT_ID;
//implements everything in Event...EVENT_ID is assigned at runtime by some registry
}
class AnEvent : public EventWrapper<int, Event> {
//public methods specific to AnEvent...
}
class AnotherEvent : public EventWrapper<long, SpecialEvent> {
int getPGNID() const {static int ID = 10; return ID;}
}
class TheProcessingClass {
AnEvent event1;
AnotherEvent event2;
void process(Event& e);
void process(SpecialEvent& e);
void doSomething() {
process(event1); //should invoke process(Event&)
process(event2); //should invoke process(SpecialEvent&)
}
}
Essentially, I have a wrapper class that wraps data of type T and inherits from some type of E
(in this case Event
or SpecialEvent
)...
I initially was going to create two wrapper classes EventWrapper
and SpecialEventWrapper
until I figured out that both classes would have the exact same code in it (as long as it was extending from some type of Event
)
First off, this sounds like a policy-based design. However, the Events
don't have any special behavior...they just hold some data... Am I abusing this pattern?
Second off, is there a better way to do this? I'm oversimplifying things greatly here, but any insights will be appreciated...
EDIT I updated my example...in short, the processing classes are listening for Events and should take action based on the Event. I hope this helps...
I suggest adding process() as a member function of class event.
class Event {
int getID() const;
void process();
//class stuff
}
class SpecialEvent : class Event {
int getSpecialID() const;
void process(); //special version of process()
//class stuff
}
class TheProcessingClass {
Event event1;
SpecialEvent event2;
void doSomething() {
event1.process();
event2.process();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With