Empirical
Listeners.h
Go to the documentation of this file.
1 
11 #ifndef EMP_WEB_LISTENERS_H
12 #define EMP_WEB_LISTENERS_H
13 
14 
15 #ifdef EMSCRIPTEN
16 #include <emscripten.h>
17 #endif
18 
19 #include "../tools/string_utils.h"
20 
21 #include <map>
22 #include <string>
23 
24 namespace emp {
25 namespace web {
26 
28  class Listeners {
29  private:
30  std::map<std::string, size_t> listeners;
31 
32  public:
33  Listeners() { ; }
34  Listeners(const Listeners &) = default;
35  Listeners & operator=(const Listeners &) = default;
36 
38  size_t GetSize() const { return listeners.size(); }
39 
41  Listeners & Set(const std::string & name, size_t fun_id) {
42  emp_assert(!Has(name));
43  listeners[name] = fun_id;
44  return *this;
45  }
46 
48  template <typename... Ts>
49  Listeners & Set(const std::string & name, const std::function<void(Ts... args)> & in_fun) {
50  emp_assert(!Has(name));
51  listeners[name] = JSWrap(in_fun);
52  return *this;
53  }
54 
56  bool Has(const std::string & event_name) const {
57  return listeners.find(event_name) != listeners.end();
58  }
59 
61  size_t GetID(const std::string & event_name) {
62  emp_assert(Has(event_name));
63  return listeners[event_name];
64  }
65 
66  const std::map<std::string, size_t> & GetMap() const {
67  return listeners;
68  }
69 
71  void Clear() {
72  // @CAO: Delete functions to be called.
73  listeners.clear();
74  }
75 
77  void Remove(const std::string & event_name) {
78  // @CAO: Delete function to be called.
79  listeners.erase(event_name);
80  }
81 
83  void Apply(const std::string & widget_id) {
84  // Find the current object only once.
85 #ifdef EMSCRIPTEN
86  EM_ASM_ARGS({
87  var id = Pointer_stringify($0);
88  emp_i.cur_obj = $( '#' + id );
89  }, widget_id.c_str());
90 #endif
91 
92  for (auto event_pair : listeners) {
93 #ifdef EMSCRIPTEN
94  EM_ASM_ARGS({
95  var name = Pointer_stringify($0);
96  emp_i.cur_obj.on( name, function(evt) { emp.Callback($1, evt); } );
97  }, event_pair.first.c_str(), event_pair.second);
98 #else
99  std::cout << "Setting '" << widget_id << "' listener '" << event_pair.first
100  << "' to '" << event_pair.second << "'.";
101 #endif
102  }
103  }
104 
105 
107  static void Apply(const std::string & widget_id,
108  const std::string event_name,
109  size_t fun_id) {
110 #ifdef EMSCRIPTEN
111  EM_ASM_ARGS({
112  var id = Pointer_stringify($0);
113  var name = Pointer_stringify($1);
114  $( '#' + id ).on( name, function(evt) { emp.Callback($2, evt); } );
115  }, widget_id.c_str(), event_name.c_str(), fun_id);
116 #else
117  std::cout << "Setting '" << widget_id << "' listener '" << event_name
118  << "' to function id '" << fun_id << "'.";
119 #endif
120  }
121 
123  operator bool() const { return (bool) listeners.size(); }
124  };
125 
126 
127 }
128 }
129 
130 
131 #endif
size_t GetID(const std::string &event_name)
Get the ID associated with a specific listener.
Definition: Listeners.h:61
Listeners()
Definition: Listeners.h:33
Track a set of JavaScript Listeners with their callback IDs.
Definition: Listeners.h:28
Listeners & Set(const std::string &name, size_t fun_id)
Use a pre-calculated function ID with a new listener.
Definition: Listeners.h:41
const std::map< std::string, size_t > & GetMap() const
Definition: Listeners.h:66
void Apply(const std::string &widget_id)
Apply all of the listeners being tracked.
Definition: Listeners.h:83
Listeners & Set(const std::string &name, const std::function< void(Ts...args)> &in_fun)
Calculate its own function ID with JSWrap.
Definition: Listeners.h:49
size_t GetSize() const
How many listeners are we tracking?
Definition: Listeners.h:38
Listeners & operator=(const Listeners &)=default
void Remove(const std::string &event_name)
Remove a specific listener.
Definition: Listeners.h:77
If we are in emscripten, make sure to include the header.
Definition: array.h:37
bool Has(const std::string &event_name) const
Determine if a specified listener exists.
Definition: Listeners.h:56
#define emp_assert(...)
Definition: assert.h:199
void Clear()
Remove all listeners.
Definition: Listeners.h:71
static void Apply(const std::string &widget_id, const std::string event_name, size_t fun_id)
Apply a SPECIFIC listener.
Definition: Listeners.h:107