AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
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#pragma once
21
22#include <thread>
23#include <mutex>
24#include <queue>
25#include <set>
26
27#include "events/observer.hpp"
28#include "events/event.hpp"
29#include "lessPtrCompare.hpp"
30#include "singleton.hpp"
31
32
33namespace anch::events {
34
44 template<typename T>
45 class EventBus: public anch::Singleton<EventBus<T>> {
46 friend class anch::Singleton<EventBus<T>>;
47
48 // Attributes +
49 private:
51 std::mutex _eventMutex;
52
54 std::mutex _queueMutex;
55
57 std::thread* _thread;
58
60 std::queue<anch::events::Event<T>> _events;
61
63 std::set<anch::events::Observer<T>*, anch::LessPtrCompare<anch::events::Observer<T>>> _observers;
64 // Attributes -
65
66
67 // Constructors +
68 private:
72 EventBus();
73 // Constructors -
74
75 // Destructor +
79 virtual ~EventBus();
80 // Destructor -
81
82
83 // Methods +
84 public:
90 bool addObserver(anch::events::Observer<T>& observer) noexcept;
91
97 static bool AddObserver(anch::events::Observer<T>& observer) noexcept;
98
105
111 static void RemoveObserver(anch::events::Observer<T>& observer) noexcept;
112
119 void fireEvent(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
120
126 void fireEvent(const anch::events::Event<T>& event) noexcept;
127
134 static void FireEvent(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
135
141 static void FireEvent(const anch::events::Event<T>& event) noexcept;
142
149 void scheduleDeferred(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
150
156 void scheduleDeferred(const anch::events::Event<T>& event) noexcept;
157
164 static void ScheduleDeferred(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
165
171 static void ScheduleDeferred(const anch::events::Event<T>& event) noexcept;
172
173 private:
177 void processEvents() noexcept;
178 // Methods -
179
180 };
181
182}
183
184#include "events/impl/eventBus.hpp"
Meyers' singleton implementation.
Definition singleton.hpp:34
static void FireEvent(const T &event, const std::map< std::string, std::string > &headers={}) noexcept
void removeObserver(anch::events::Observer< T > &observer) noexcept
static void ScheduleDeferred(const anch::events::Event< T > &event) noexcept
static void FireEvent(const anch::events::Event< T > &event) noexcept
static bool AddObserver(anch::events::Observer< T > &observer) noexcept
void scheduleDeferred(const T &event, const std::map< std::string, std::string > &headers={}) noexcept
bool addObserver(anch::events::Observer< T > &observer) noexcept
void scheduleDeferred(const anch::events::Event< T > &event) noexcept
void fireEvent(const anch::events::Event< T > &event) noexcept
static void ScheduleDeferred(const T &event, const std::map< std::string, std::string > &headers={}) noexcept
static void RemoveObserver(anch::events::Observer< T > &observer) noexcept
void fireEvent(const T &event, const std::map< std::string, std::string > &headers={}) noexcept
An observer interface of the observers/observable design pattern.
Definition observer.hpp:39
Events management namespace.
Definition event.hpp:25
Less comparator based on object address.
Definition lessPtrCompare.hpp:34
Event representation.
Definition event.hpp:37