Last active
December 14, 2015 07:08
-
-
Save Hydrotoast/5047967 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef EVENT_H | |
#define EVENT_H | |
#include "DiscreteClock.h" | |
#include "SimulationState.h" | |
#include <functional> | |
class EventHandler; | |
class Event { | |
public: | |
Event(); | |
Event(discrete_time::time_point time); | |
~Event(); | |
Event(const Event& evt); | |
Event& operator=(const Event& evt); | |
discrete_time::time_point time() const; | |
void setHandler(EventHandler* handler); | |
void execute(SimulationState& state) const; | |
friend bool operator<(const Event& op1, const Event& op2); | |
friend bool operator==(const Event& op1, const Event& op2); | |
protected: | |
discrete_time::time_point time_; | |
EventHandler* handler; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef EVENT_HANDLER_H | |
#define EVENT_HANDLER_H | |
#include "SimulationState.h" | |
#include "Event.h" | |
// Old event handler | |
// class EventHandler { | |
// public: | |
// virtual EventHandler* clone() const = 0; | |
// virtual void operator()(const Event* evt, SimulationState& state) = 0; | |
// }; | |
// New CRTP event handler that doesn't generate virtual tables | |
template <class Derived> | |
class EventHandler { | |
public: | |
Derived* clone() const { return new Derived(static_cast<const Derived&>(*this)); } | |
void operator()(const Event* evt, SimulationState& state) { return static_cast<Derived&>(*this).execute(evt, state); } | |
private: | |
void execute()(const Event* evt, SimulationState& state) { | |
// Do something by default? | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment