AnCH Framework  0.1
Another C++ Hack Framework
eventBus.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_EVENTS_EVENT_BUS_H_
21 #define _ANCH_EVENTS_EVENT_BUS_H_
22 
23 #include <thread>
24 #include <mutex>
25 #include <queue>
26 #include <set>
27 
28 #include "events/observer.hpp"
29 #include "lessPtrCompare.hpp"
30 #include "singleton.hpp"
31 
32 
33 namespace anch {
34  namespace events {
35 
45  template<typename Event>
46  class EventBus: public anch::Singleton<EventBus<Event> > {
47  friend class anch::Singleton<EventBus<Event> >;
48 
49  // Attributes +
50  private:
52  std::mutex _eventMutex;
53 
55  std::mutex _queueMutex;
56 
58  std::thread* _thread;
59 
61  std::queue<Event> _events;
62 
64  std::set<anch::events::Observer<Event>*, anch::LessPtrCompare<Observer<Event> > > _observers;
65  // Attributes -
66 
67 
68  // Constructors +
69  private:
73  EventBus(): _eventMutex(), _queueMutex(), _thread(NULL), _events(), _observers() {
74  // Nothing to do
75  }
76  // Constructors -
77 
78  // Destructor +
82  virtual ~EventBus() {
83  if(_thread != NULL) {
84  _thread->join();
85  delete _thread;
86  }
87  }
88  // Destructor -
89 
90 
91  // Methods +
92  public:
98  bool addObserver(anch::events::Observer<Event>& observer) noexcept {
99  std::lock_guard<std::mutex> lock(_eventMutex);
100  bool added = _observers.insert(&observer).second;
101  return added;
102  }
103 
109  static bool AddObserver(anch::events::Observer<Event>& observer) noexcept {
110  return EventBus<Event>::getInstance().addObserver(observer);
111  }
112 
119  std::lock_guard<std::mutex> lock(_eventMutex);
120  _observers.erase(&observer);
121  }
122 
128  static void RemoveObserver(anch::events::Observer<Event>& observer) noexcept {
129  EventBus<Event>::getInstance().removeObserver(observer);
130  }
131 
137  void fireEvent(const Event& event) noexcept {
138  std::lock_guard<std::mutex> lock(_eventMutex);
139  for(anch::events::Observer<Event>* observer : _observers) {
140  observer->notify(event);
141  }
142  }
143 
149  static void FireEvent(const Event& event) noexcept {
150  EventBus<Event>::getInstance().fireEvent(event);
151  }
152 
158  void scheduleDeferred(const Event& event) noexcept {
159  std::lock_guard<std::mutex> lock(_queueMutex);
160  bool empty = _events.empty();
161  _events.push(event);
162  if(empty) {
163  if(_thread != NULL) {
164  _thread->join();
165  delete _thread;
166  }
167  _thread = new std::thread(&EventBus<Event>::processEvents,this);
168  }
169  }
170 
176  static void ScheduleDeferred(const Event& event) noexcept {
177  EventBus<Event>::getInstance().scheduleDeferred(event);
178  }
179 
180  private:
184  void processEvents() noexcept {
185  bool empty = false;
186  do {
187  _queueMutex.lock();
188  Event event = _events.front();
189  _events.pop();
190  empty = _events.empty();
191  _queueMutex.unlock();
192 
193  fireEvent(event);
194 
195  } while(!empty);
196  }
197  // Methods -
198 
199  };
200 
201  }
202 }
203 
204 #endif // _ANCH_EVENTS_EVENT_BUS_H_
An observer interface of the observers/observable design pattern.
Definition: observer.hpp:39
void removeObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:118
void scheduleDeferred(const Event &event) noexcept
Definition: eventBus.hpp:158
static void FireEvent(const Event &event) noexcept
Definition: eventBus.hpp:149
static bool AddObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:109
void fireEvent(const Event &event) noexcept
Definition: eventBus.hpp:137
A class which manage global events firing and events QoS.
Definition: eventBus.hpp:46
AnCH framework base namespace.
Definition: base64.hpp:28
static void RemoveObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:128
static EventBus< Event > & getInstance()
Definition: singleton.hpp:42
Less comparator based on object address.
Definition: lessPtrCompare.hpp:35
static void ScheduleDeferred(const Event &event) noexcept
Definition: eventBus.hpp:176
Meyers&#39; singleton implemtation.
Definition: singleton.hpp:35
bool addObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:98