summaryrefslogtreecommitdiff
path: root/src/CommonAPI/Event.h
blob: b032da9bd752cc2fc8722e21c43059e92d780fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.h> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_EVENT_H_
#define COMMONAPI_EVENT_H_

#include <functional>
#include <list>
#include <tuple>
#include <mutex>

namespace CommonAPI {

enum class SubscriptionStatus {
	RETAIN,
	CANCEL
};

/**
 * \brief Class representing an event
 *
 * Class representing an event
 */
template <typename... _Arguments>
class Event {
 public:
	typedef std::tuple<_Arguments...> ArgumentsTuple;
	typedef std::function<void(const _Arguments&...)> Listener;
	typedef std::function<SubscriptionStatus(const _Arguments&...)> CancellableListener;
	typedef std::list<CancellableListener> ListenersList;
	typedef typename ListenersList::iterator Subscription;

	class CancellableListenerWrapper;

	/**
	 * \brief Subscribe a listener to this event
	 *
	 * Subscribe a listener to this event.
	 * ATTENTION: You should not build new proxies or register services in callbacks
	 * from events. This can cause a deadlock or assert. Instead, you should set a
	 * trigger for your application to do this on the next iteration of your event loop
	 * if needed. The preferred solution is to build all proxies you need at the
	 * beginning and react to events appropriatly for each.
	 *
	 * @param listener A listener to be added
	 * @return A token identifying this subscription
	 */
	inline Subscription subscribe(Listener listener);

	/**
     * \brief Subscribe a cancellable listener to this event
     *
     * Subscribe a cancellable listener to this event
     * ATTENTION: You should not build new proxies or register services in callbacks
     * from events. This can cause a deadlock or assert. Instead, you should set a
     * trigger for your application to do this on the next iteration of your event loop
     * if needed. The preferred solution is to build all proxies you need at the
     * beginning and react to events appropriatly for each.
     *
     * @param listener A cancellable listener to be added
     * @return A token identifying this subscription
     */
	Subscription subscribeCancellableListener(CancellableListener listener);

	/**
     * \brief Remove a listener from this event
     *
     * Remove a listener from this event
     * Note: Do not call this inside a listener notification callback it will deadlock! Use cancellable listeners instead.
     *
     * @param listenerSubscription A listener token to be removed
     */
	void unsubscribe(Subscription listenerSubscription);

	virtual ~Event() {}

 protected:
	// Returns false if all subscriptions were cancelled
	// Does not send *Removed events!
	SubscriptionStatus notifyListeners(const _Arguments&... eventArguments);

	// Events sent during subscribe()
	virtual void onFirstListenerAdded(const CancellableListener& listener) { }
	virtual void onListenerAdded(const CancellableListener& listener) { }

	// Events sent during unsubscribe()
	virtual void onListenerRemoved(const CancellableListener& listener) { }
	virtual void onLastListenerRemoved(const CancellableListener& listener) { }

	inline bool hasListeners() const;

 private:
	ListenersList listenersList_;
	std::mutex listenerListMutex_;
};

template <typename... _Arguments>
class Event<_Arguments...>::CancellableListenerWrapper {
 public:
	CancellableListenerWrapper(Listener&& listener): listener_(std::move(listener)) {
	}

	SubscriptionStatus operator()(const _Arguments&... eventArguments) {
		listener_(eventArguments...);
		return SubscriptionStatus::RETAIN;
	}

 private:
	Listener listener_;
};

template <typename... _Arguments>
typename Event<_Arguments...>::Subscription Event<_Arguments...>::subscribe(Listener listener) {
	return subscribeCancellableListener(CancellableListenerWrapper(std::move(listener)));
}

template <typename... _Arguments>
typename Event<_Arguments...>::Subscription Event<_Arguments...>::subscribeCancellableListener(CancellableListener listener) {
    listenerListMutex_.lock();
    const bool firstListenerAdded = listenersList_.empty();

	listenersList_.emplace_front(std::move(listener));
	Subscription listenerSubscription = listenersList_.begin();
	listenerListMutex_.unlock();

	if (firstListenerAdded) {
		onFirstListenerAdded(*listenerSubscription);
	}

	onListenerAdded(*listenerSubscription);

	return listenerSubscription;
}

template <typename... _Arguments>
void Event<_Arguments...>::unsubscribe(Subscription listenerSubscription) {
	const CancellableListener cancellableListener = *listenerSubscription;

	listenerListMutex_.lock();
	listenersList_.erase(listenerSubscription);
	const bool lastListenerRemoved = listenersList_.empty();
	listenerListMutex_.unlock();

	onListenerRemoved(cancellableListener);

	if (lastListenerRemoved) {
		onLastListenerRemoved(cancellableListener);
	}
}

template <typename... _Arguments>
SubscriptionStatus Event<_Arguments...>::notifyListeners(const _Arguments&... eventArguments) {
	listenerListMutex_.lock();
	for (auto iterator = listenersList_.begin(); iterator != listenersList_.end(); ) {
		const CancellableListener& cancellableListener = *iterator;
		const SubscriptionStatus listenerSubscriptionStatus = cancellableListener(eventArguments...);

		if (listenerSubscriptionStatus == SubscriptionStatus::CANCEL) {
			auto listenerIterator = iterator;
			iterator++;
			listenersList_.erase(listenerIterator);
		} else
			iterator++;
	}

	const bool lEmpty = listenersList_.empty();

	listenerListMutex_.unlock();

	return lEmpty ? SubscriptionStatus::CANCEL : SubscriptionStatus::RETAIN;
}

template <typename... _Arguments>
bool Event<_Arguments...>::hasListeners() const {
	return !listenersList_.empty();
}

} // namespace CommonAPI

#endif // COMMONAPI_EVENT_H_