5 #ifndef EMP_EVENT_LIB_H 6 #define EMP_EVENT_LIB_H 9 #include <unordered_set> 14 #include "../tools/FunctionSet.h" 15 #include "../tools/map_utils.h" 16 #include "../base/vector.h" 20 template<
typename HARDWARE_T>
24 using event_t =
typename hardware_t::event_t;
25 using fun_t = std::function<void(hardware_t &, const event_t &)>;
37 EventDef(
const std::string & _n,
fun_t _handler,
const std::string & _d,
39 : name(_n), handler(_handler), desc(_d), properties(_properties),
59 const std::string &
GetName(
size_t id)
const {
return event_lib[id].name; }
68 const std::string &
GetDesc(
size_t id)
const {
return event_lib[id].desc; }
74 bool HasProperty(
size_t id, std::string property)
const {
return event_lib[id].properties.count(property); }
82 return Find(name_map, name, (
size_t)-1);
87 const fun_t & handler_fun,
88 const std::string &
desc=
"",
91 const size_t id = event_lib.
size();
98 event_lib[id].dispatch_funs.Add(dispatch_fun);
103 event_lib[
GetID(name)].dispatch_funs.Add(dispatch_fun);
108 event_lib[
event.id].dispatch_funs.Run(hw, event);
113 event_lib[
event.id].handler(hw, event);
const std::string & GetDesc(size_t id) const
Get the string description of the specified event definition.
Definition: EventLib.h:68
const fun_t & GetHandler(size_t id) const
Get the handler function of the specified event definition.
Definition: EventLib.h:62
std::map< std::string, size_t > name_map
Definition: EventLib.h:47
bool HasProperty(size_t id, std::string property) const
Does the event definition specified by id have the property specified.
Definition: EventLib.h:74
void AddEvent(const std::string &name, const fun_t &handler_fun, const std::string &desc="", const properties_t &event_properties=properties_t())
Add a new event to the event library.
Definition: EventLib.h:86
void HandleEvent(hardware_t &hw, const event_t &event) const
Handle event.
Definition: EventLib.h:112
void RegisterDispatchFun(size_t id, fun_t dispatch_fun)
Register a dispatch function for the event specified by id.
Definition: EventLib.h:97
size_t GetSize() const
Get the number of events registered to this event library.
Definition: EventLib.h:77
std::unordered_set< std::string > properties_t
Definition: EventLib.h:26
size_t size() const
Definition: vector.h:151
Definition: EventLib.h:21
void emplace_back(ARGS &&...args)
Definition: vector.h:219
std::function< void(hardware_t &, const event_t &)> fun_t
Definition: EventLib.h:25
EventLib()
Definition: EventLib.h:51
emp::vector< EventDef > event_lib
Definition: EventLib.h:46
auto Find(const MAP_T &in_map, const KEY_T &key, const typename MAP_T::mapped_type &dval)
Definition: map_utils.h:29
std::string name
Definition: EventLib.h:31
typename hardware_t::event_t event_t
Definition: EventLib.h:24
bool Has(const MAP_T &in_map, const KEY_T &key)
Take any map type, and run find to determine if a key is present.
Definition: map_utils.h:21
~EventLib()
Definition: EventLib.h:53
const fun_set_t & GetDispatchFuns(size_t id) const
Get the dispatch function set of the specified event definition.
Definition: EventLib.h:65
properties_t properties
Definition: EventLib.h:34
fun_t handler
Definition: EventLib.h:32
std::string desc
Definition: EventLib.h:33
fun_set_t dispatch_funs
Definition: EventLib.h:35
const properties_t & GetProperties(size_t id) const
Get a const reference to an event definition's properties.
Definition: EventLib.h:71
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
#define emp_assert(...)
Definition: assert.h:199
size_t GetID(const std::string &name) const
Get the event ID of the event given by string name.
Definition: EventLib.h:80
const std::string & GetName(size_t id) const
Get the string name of the specified event definition.
Definition: EventLib.h:59
Event definition structure. Maintains information about a type of event.
Definition: EventLib.h:30
HARDWARE_T hardware_t
Definition: EventLib.h:23
void RegisterDispatchFun(const std::string &name, fun_t dispatch_fun)
Register a dispatch function for the event specified by name.
Definition: EventLib.h:102
EventDef(const std::string &_n, fun_t _handler, const std::string &_d, const properties_t &_properties)
Definition: EventLib.h:37
void TriggerEvent(hardware_t &hw, const event_t &event) const
Trigger event.
Definition: EventLib.h:107
EventLib & operator=(const EventLib &)=default