File: Observable.h

package info (click to toggle)
0ad 0.0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 51,248 kB
  • ctags: 46,933
  • sloc: cpp: 223,208; ansic: 31,240; python: 16,343; perl: 4,083; sh: 1,011; makefile: 915; xml: 733; java: 621; ruby: 229; erlang: 53; sql: 40
file content (188 lines) | stat: -rw-r--r-- 4,475 bytes parent folder | download | duplicates (6)
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) 2014 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_OBSERVABLE
#define INCLUDED_OBSERVABLE

/*
Wrapper around Boost.Signals to make watching objects for changes more convenient.

General usage:

Observable<int> variable_to_be_watched;
...
class Thing {
	ObservableScopedConnection m_Conn;
	Thing() {
		m_Conn = variable_to_be_watched.RegisterObserver(OnChange);
	}
	void OnChange(const int& var) {
		do_something_with(var);
	}
}
...
variable_to_be_watched.NotifyObservers();

*/

#include <boost/bind.hpp>
#include <boost/version.hpp>
#if BOOST_VERSION >= 104000
# include <boost/signals2.hpp>
typedef boost::signals2::connection ObservableConnection;
typedef boost::signals2::scoped_connection ObservableScopedConnection;
#else
# error Atlas requires Boost 1.40 or later
#endif

template <typename T> class Observable : public T
{
public:
	Observable() {}

	template <typename T1>
		explicit Observable(const T1& a1) : T(a1) {}
	template <typename T1, typename T2>
		explicit Observable(T1& a1, T2& a2) : T(a1, a2) {}
	template <typename T1, typename T2>
		explicit Observable(T1& a1, T2 a2) : T(a1, a2) {}

	template<typename C> ObservableConnection RegisterObserver(int order, void (C::*callback) (const T&), C* obj)
	{
		return m_Signal.connect(order, boost::bind(std::mem_fun(callback), obj, _1));
	}

	ObservableConnection RegisterObserver(int order, void (*callback) (const T&))
	{
		return m_Signal.connect(order, callback);
	}

	void RemoveObserver(const ObservableConnection& conn)
	{
		conn.disconnect();
	}

	void NotifyObservers()
	{
		m_Signal(*this);
	}

	// Use when an object is changing something that it's also observing,
	// because it already knows about the change and doesn't need to be notified
	// again (particularly since that may cause infinite loops).
	void NotifyObserversExcept(ObservableConnection& conn)
	{
		if (conn.blocked())
		{
			// conn is already blocked and won't see anything
			NotifyObservers();
		}
		else
		{
			// Temporarily disable conn
			boost::signals2::shared_connection_block blocker(conn);
			NotifyObservers();
		}
	}

	Observable<T>& operator=(const T& rhs)
	{
		*dynamic_cast<T*>(this) = rhs;
		return *this;
	}

private:
	boost::signals2::signal<void (const T&)> m_Signal;
};

// A similar thing, but for wrapping pointers instead of objects
template <typename T> class ObservablePtr
{
public:
	ObservablePtr() : m_Ptr(NULL) {}

	ObservablePtr(T* p) : m_Ptr(p) {}

	ObservablePtr& operator=(T* p)
	{
		m_Ptr = p;
		return *this;
	}

	T* operator->()
	{
		return m_Ptr;
	}

	T* operator*()
	{
		return m_Ptr;
	}

	template<typename C> ObservableConnection RegisterObserver(int order, void (C::*callback) (T*), C* obj)
	{
		return m_Signal.connect(order, boost::bind(std::mem_fun(callback), obj, _1));
	}

	ObservableConnection RegisterObserver(int order, void (*callback) (T*))
	{
		return m_Signal.connect(order, callback);
	}

	void RemoveObserver(const ObservableConnection& conn)
	{
		conn.disconnect();
	}

	void NotifyObservers()
	{
		m_Signal(m_Ptr);
	}

	// Use when an object is changing something that it's also observing,
	// because it already knows about the change and doesn't need to be notified
	// again (particularly since that may cause infinite loops).
	void NotifyObserversExcept(ObservableConnection& conn)
	{
		if (conn.blocked())
		{
			// conn is already blocked and won't see anything
			NotifyObservers();
		}
		else
		{
			// Temporarily disable conn
			boost::signals2::shared_connection_block blocker(conn);
			NotifyObservers();
		}
	}

private:
	T* m_Ptr;
	boost::signals2::signal<void (T*)> m_Signal;
};

class ObservableScopedConnections
{
public:
	void Add(const ObservableConnection& conn);
	~ObservableScopedConnections();
private:
	std::vector<ObservableConnection> m_Conns;
};

#endif // INCLUDED_OBSERVABLE