summaryrefslogtreecommitdiffstats
blob: 655599af29f0534a44597d13c216c486c835c7c5 (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
/*
 * Copyright (c) 2013-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <algorithm>
#include <vector>

#include <boost/bind.hpp>

#include <Swiften/Base/API.h>

namespace Swift {
	template<typename T>
	class SWIFTEN_API Listenable {
		public:
			void addListener(T* listener) {
				listeners.push_back(listener);
			}

			void removeListener(T* listener) {
				listeners.erase(std::remove(listeners.begin(), listeners.end(), listener), listeners.end());
			}

		protected:
			template<typename F>
			void notifyListeners(F event) {
				for (typename std::vector<T*>::iterator i = listeners.begin(); i != listeners.end(); ++i) {
					event(*i);
				}
			}

			template<typename F, typename A1>
			void notifyListeners(F f, const A1& a1) {
				notifyListeners(boost::bind(f, _1, a1));
			}

			template<typename F, typename A1, typename A2>
			void notifyListeners(F f, const A1& a1, const A2& a2) {
				notifyListeners(boost::bind(f, _1, a1, a2));
			}

			template<typename F, typename A1, typename A2, typename A3>
			void notifyListeners(F f, const A1& a1, const A2& a2, const A3& a3) {
				notifyListeners(boost::bind(f, _1, a1, a2, a3));
			}

		private:
			std::vector<T*> listeners;
	};
}