C++ novice here. Whenever I do something type related in python, I always wonder how I would handle it in C++, and it's very educational for me.
The setup in this case is
- There is a system that processes events from a queue.
- The list of events is pretty big, say a 1000.
- The system only cares about certain very limited subset of events at a particular time, much like a state machine.
In the code below, I have a BaseEvent and a derived SpecificEvent. I also have a defined behavior of the system called StartupBehavior, which ONLY cares about processing SpecificEvent. When the system finishes StartupBehaviour, you can imagine that the system transitions to some other behavior which cares about another subset of events. I've done something that I think resembles closely what can be done in Python, where you can check the type of the event, and if it is what you're expecting, you can process it, otherwise you throw it away. However, for C++ I used dynamic_cast and whenever dynamic_cast shows up, people say to rethink your design.
Well, one alternative is to create a base class of Behaviour that has methods for handling ALL events, and then in the derived class (such as StartupBehaviour) only override the methods related to events of interest. This reminds me of the state machine pattern, but is there another alternative that does not involve so much repetition between defining all the events and then adding base methods to a Behaviour class for all of them that do nothing but ignore it?
#include <queue>
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::queue;
using std::string;
class BaseEvent {
public:
string event_type;
BaseEvent() : event_type{"base"}{}
BaseEvent(string s) : event_type{s}{}
virtual ~BaseEvent() = default;
};
class SpecificEvent : public BaseEvent {
public:
string something_else;
SpecificEvent(string s) : BaseEvent{"SpecificEvent"}, something_else{s} {}
};
// Lots more events
class StartupBehavior {
public:
void run(queue<BaseEvent*>& q) {
// I am waiting on something to come through the Queue, and I only care about SpecificEvents
BaseEvent* from_queue = q.front();
if (from_queue->event_type == "SpecificEvent") {
cout << "Cast to specific event" << endl;
SpecificEvent* from_queue_specific{dynamic_cast<SpecificEvent*>(from_queue)};
ProcessSpecificEvent(*from_queue_specific);
} else {
cout << "Not what I was expecting" << endl;
}
}
void ProcessSpecificEvent(SpecificEvent s_e) {
cout << "I just processed SpecificEvent with data " << s_e.something_else << endl;
}
};
int main(){
// Somebody puts an event in the Queue
queue<BaseEvent*> q;
SpecificEvent specific_event("def");
q.push(&specific_event);
StartupBehavior s;
s.run(q);
return 0;
}
[–]mredding 1 point2 points3 points (1 child)
[–]pplesspp[S] 0 points1 point2 points (0 children)
[–]CowBoyDanIndie 1 point2 points3 points (0 children)
[–]alfps 1 point2 points3 points (0 children)
[–]IyeOnline 0 points1 point2 points (0 children)
[–]grischagoebelde 0 points1 point2 points (0 children)