AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
eventBus.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 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 <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <condition_variable>
23#include <map>
24#include <mutex>
25#include <queue>
26#include <string>
27#include <thread>
28
29#include "events/observer.hpp"
30#include "events/event.hpp"
31#include "events/observerList.hpp"
32#include "singleton.hpp"
33
34namespace anch::events {
35
52 template<typename T>
53 class EventBus: public anch::Singleton<EventBus<T>> {
54 friend class anch::Singleton<EventBus<T>>;
55
56 // Attributes +
57 private:
60
62 std::mutex _mutex;
63
65 std::condition_variable _eventCV;
66
68 std::queue<anch::events::Event<T>> _events;
69
71 bool _stop;
72
74 std::jthread _worker;
75 // Attributes -
76
77 // Constructors +
78 public:
84
88 EventBus(const EventBus&) = delete;
89 // Constructors -
90
91 // Destructor +
92 public:
98 // Destructor -
99
100 // Operators +
101 public:
105 EventBus& operator=(const EventBus&) = delete;
106 // Operators -
107
108 // Methods +
109 public:
117 bool addObserver(anch::events::Observer<T>& observer) noexcept;
118
126 static bool AddObserver(anch::events::Observer<T>& observer) noexcept;
127
135
141 static void RemoveObserver(anch::events::Observer<T>& observer) noexcept;
142
150 void fireEvent(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
151
158 void fireEvent(const anch::events::Event<T>& event) noexcept;
159
166 static void FireEvent(const T& event, const std::map<std::string,std::string>& headers = {}) noexcept;
167
173 static void FireEvent(const anch::events::Event<T>& event) noexcept;
174
179 void release();
180
181 private:
185 void processEvents() noexcept;
186 // Methods -
187
188 };
189
190}
191
192#include "events/impl/eventBus.hpp"
Meyers' singleton implementation.
Definition singleton.hpp:50
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 FireEvent(const anch::events::Event< T > &event) noexcept
static bool AddObserver(anch::events::Observer< T > &observer) noexcept
bool addObserver(anch::events::Observer< T > &observer) noexcept
EventBus(const EventBus &)=delete
void fireEvent(const anch::events::Event< T > &event) noexcept
static void RemoveObserver(anch::events::Observer< T > &observer) noexcept
void fireEvent(const T &event, const std::map< std::string, std::string > &headers={}) noexcept
EventBus & operator=(const EventBus &)=delete
An observer interface of the observers/observable design pattern.
Definition observer.hpp:42
Thread safe observers list (internal usage).
Definition observerList.hpp:54
Events management namespace.
Definition event.hpp:25
Event representation.
Definition event.hpp:37