Empirical
EventLib.h
Go to the documentation of this file.
1 
2 // This file is largely based on InstLib.h.
3 // This file maintains information about events available in virtual hardware.
4 
5 #ifndef EMP_EVENT_LIB_H
6 #define EMP_EVENT_LIB_H
7 
8 #include <functional>
9 #include <unordered_set>
10 #include <map>
11 #include <string>
12 #include <iostream>
13 
14 #include "../tools/FunctionSet.h"
15 #include "../tools/map_utils.h"
16 #include "../base/vector.h"
17 
18 namespace emp {
19 
20  template<typename HARDWARE_T>
21  class EventLib {
22  public:
23  using hardware_t = HARDWARE_T;
24  using event_t = typename hardware_t::event_t;
25  using fun_t = std::function<void(hardware_t &, const event_t &)>; //< Function type alias for event handler functions.
26  using properties_t = std::unordered_set<std::string>; //< Type for event definition properties.
27  using fun_set_t = FunctionSet<void(hardware_t &, const event_t &)>; //< Type for event dispatch function sets.
28 
30  struct EventDef {
31  std::string name; //< Name of this event.
32  fun_t handler; //< Function to call to handle this event.
33  std::string desc; //< Description of event.
34  properties_t properties; //< Any properties that should be associated with this type of event.
35  fun_set_t dispatch_funs; //< Functions to call when this type of event is triggered.
36 
37  EventDef(const std::string & _n, fun_t _handler, const std::string & _d,
38  const properties_t & _properties)
39  : name(_n), handler(_handler), desc(_d), properties(_properties),
40  dispatch_funs() { ; }
41 
42  EventDef(const EventDef &) = default;
43  };
44 
45  protected:
46  emp::vector<EventDef> event_lib; //< Full definitions of each event.
47  std::map<std::string, size_t> name_map; //< Event name -> ID map.
48 
49 
50  public:
51  EventLib() : event_lib(), name_map() { ; }
52  EventLib(const EventLib &) = default;
53  ~EventLib() { ; }
54 
55  EventLib & operator=(const EventLib &) = default;
56  EventLib & operator=(EventLib &&) = default;
57 
59  const std::string & GetName(size_t id) const { return event_lib[id].name; }
60 
62  const fun_t & GetHandler(size_t id) const { return event_lib[id].handler; }
63 
65  const fun_set_t & GetDispatchFuns(size_t id) const { return event_lib[id].dispatch_funs; }
66 
68  const std::string & GetDesc(size_t id) const { return event_lib[id].desc; }
69 
71  const properties_t & GetProperties(size_t id) const { return event_lib[id].properties; }
72 
74  bool HasProperty(size_t id, std::string property) const { return event_lib[id].properties.count(property); }
75 
77  size_t GetSize() const { return event_lib.size(); }
78 
80  size_t GetID(const std::string & name) const {
81  emp_assert(Has(name_map, name), name);
82  return Find(name_map, name, (size_t)-1);
83  }
84 
86  void AddEvent(const std::string & name,
87  const fun_t & handler_fun,
88  const std::string & desc="",
89  const properties_t & event_properties=properties_t())
90  {
91  const size_t id = event_lib.size();
92  event_lib.emplace_back(name, handler_fun, desc, event_properties);
93  name_map[name] = id;
94  }
95 
97  void RegisterDispatchFun(size_t id, fun_t dispatch_fun) {
98  event_lib[id].dispatch_funs.Add(dispatch_fun);
99  }
100 
102  void RegisterDispatchFun(const std::string & name, fun_t dispatch_fun) {
103  event_lib[GetID(name)].dispatch_funs.Add(dispatch_fun);
104  }
105 
107  void TriggerEvent(hardware_t & hw, const event_t & event) const {
108  event_lib[event.id].dispatch_funs.Run(hw, event);
109  }
110 
112  void HandleEvent(hardware_t & hw, const event_t & event) const {
113  event_lib[event.id].handler(hw, event);
114  }
115 
116  };
117 
118 }
119 
120 #endif
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&#39;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