Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1/*
2 Copyright 2020 natinusala
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <algorithm>
20#include <functional>
21#include <list>
22#include <mutex>
23
24namespace aurore
25{
26
27// Simple observer pattern implementation
28//
29// Usage:
30// 1. typedef your event type
31// 2. create as many events as you want using that type
32// 3. call subscribe on the events with your observers
33// 4. call fire when you want to fire the events
34// it wil return true if at least one subscriber exists
35// for that event
36//
37// Subscribing, unsubscribing and firing may happen on different threads
38// (the logger fires from any); the callbacks run on the thread that fires.
39template <typename... Ts>
40class Event
41{
42 public:
43 typedef std::function<void(Ts...)> Callback;
44 typedef std::list<Callback> CallbacksList;
45 typedef typename CallbacksList::iterator Subscription;
46
47 Event() = default;
48 // The subscribers are copied, not the lock
49 Event(const Event& other)
50 : callbacks(other.copyCallbacks())
51 {
52 }
53 Event& operator=(const Event& other)
54 {
55 if (this != &other)
56 {
57 CallbacksList copy = other.copyCallbacks();
58 std::lock_guard<std::mutex> guard(this->mutex);
59 this->callbacks.swap(copy);
60 }
61 return *this;
62 }
63
65 void unsubscribe(Subscription subscription);
66 void clear();
67 bool fire(Ts... args);
68
69 private:
70 CallbacksList callbacks;
71 mutable std::mutex mutex;
72
73 CallbacksList copyCallbacks() const
74 {
75 std::lock_guard<std::mutex> guard(this->mutex);
76 return this->callbacks;
77 }
78};
79
80template <typename... Ts>
81typename Event<Ts...>::Subscription Event<Ts...>::subscribe(Event<Ts...>::Callback cb)
82{
83 std::lock_guard<std::mutex> guard(this->mutex);
84 this->callbacks.push_back(std::move(cb));
85 return --this->callbacks.end();
86}
87
88template <typename... Ts>
90{
91 std::lock_guard<std::mutex> guard(this->mutex);
92 if (this->callbacks.size() > 0)
93 this->callbacks.erase(subscription);
94}
95
96template <typename... Ts>
98{
99 std::lock_guard<std::mutex> guard(this->mutex);
100 this->callbacks.clear();
101}
102
103template <typename... Ts>
104bool Event<Ts...>::fire(Ts... args)
105{
106 // A callback may subscribe or unsubscribe (itself or another one) while
107 // the event fires: walk a copy, never the list being changed, and call
108 // it without the lock held.
109 std::unique_lock<std::mutex> lock(this->mutex);
110 if (this->callbacks.empty())
111 return false;
112
113 if (this->callbacks.size() == 1)
114 {
115 Callback cb = this->callbacks.front();
116 lock.unlock();
117 cb(args...);
118 return true;
119 }
120
121 CallbacksList snapshot = this->callbacks;
122 lock.unlock();
123 for (Callback& cb : snapshot)
124 cb(args...);
125
126 return true;
127}
128
129}; // namespace aurore
Definition event.hpp:41
Event(const Event &other)
Definition event.hpp:49
void unsubscribe(Subscription subscription)
Definition event.hpp:89
std::function< void(Ts...)> Callback
Definition event.hpp:43
CallbacksList::iterator Subscription
Definition event.hpp:45
std::list< Callback > CallbacksList
Definition event.hpp:44
Event()=default
bool fire(Ts... args)
Definition event.hpp:104
Event & operator=(const Event &other)
Definition event.hpp:53
Subscription subscribe(Callback cb)
Definition event.hpp:81
void clear()
Definition event.hpp:97
Definition actions.hpp:25