Hey there,
So... yeah, I've been working on a "state mashine library" and worked with event-tables up until now.
Now I've packed all those things up in a few classes to make it "prettier" or more confortable to use and extend.
This is what it looks like at the moment.
I have a base-class for events.
I want to distinguish what type of event it is so I made... a not so pretty work around with an enum that contains all Events and made the base class a template-class. (please tell me if you have a better Idea for that task)
Then I have a base class for screens.
This is an abstract class with a default implementation of each single event-method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
enum { Button1, Button2, Timer1, Timer2, N_EVENTS };
template <unsigned int event_id>
struct IEvent
{
};
IEvent<Button1> button1;
IEvent<Button2> button2;
IEvent<Timer1> timer1;
IEvent<Timer2> timer2;
// Base State
struct AState
{
public:
virtual void Enter() = 0;
virtual void Leave() = 0;
virtual void Event(const IEvent<Button1>&) {}
virtual void Event(const IEvent<Button2>&) {}
virtual void Event(const IEvent<Timer1>&) {}
virtual void Event(const IEvent<Timer2>&) {}
};
|
This is what it looks like in use:
http://cpp.sh/5ykb
So, I realized that, everytime I implement a new event, I have to change the AState base class.
To avoid that I'd like use something like a virtual template function
1 2
|
template <unsigned int event_id>
virtual void Event(const IEvent<event_id>&) {}
|
Is it possible to do it this way?
Is there a better alternative to make my EventTypes? (I really don't like to rely on an enumeration for my Types)
Is there an other way to default-implement the Event Mehtods?
(The Methods should do nothing if they are not declared in the derived class)
Well, that's about everything, I hope someone can help me :)
- Gamer2015