COSA
An Object-Oriented Platform for Arduino Programming
Listener.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_LISTENER_HH
22 #define COSA_LISTENER_HH
23 
24 #include "Cosa/Linkage.hh"
25 
32 template <typename T>
33 class Listener : public Link {
34 public:
40  Link(),
41  m_key(key)
42  {}
43 
48  T key() const
49  {
50  return (m_key);
51  }
52 
57  void key(T key)
58  {
59  m_key = key;
60  }
61 
67  bool match(T key) const
68  {
69  return (m_key == key);
70  }
71 
80  static void dispatch(Head* head, T key, uint8_t type, uint16_t value);
81 
82 private:
84  T m_key;
85 };
86 
87 template <typename T>
88 void
89 Listener<T>::dispatch(Head* head, T key, uint8_t type, uint16_t value)
90 {
91  Linkage* link = head->succ();
92  while (link != head) {
93  Listener<T>* listener = (Listener<T>*) link;
94  if (listener->match(key)) listener->on_event(type, value);
95  link = link->succ();
96  }
97 }
98 #endif
bool match(T key) const
Definition: Listener.hh:67
static void dispatch(Head *head, T key, uint8_t type, uint16_t value)
Definition: Listener.hh:89
T key() const
Definition: Listener.hh:48
void key(T key)
Definition: Listener.hh:57
Listener(T key)
Definition: Listener.hh:39
Linkage * succ() const
Definition: Linkage.hh:51
virtual void on_event(uint8_t type, uint16_t value)
Definition: Event.hh:107
Definition: Linkage.hh:132