forked from ETLCPP/etl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.h
357 lines (315 loc) · 11.4 KB
/
observer.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
Copyright(c) 2014 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef __ETL_OBSERVER__
#define __ETL_OBSERVER__
//*****************************************************************************
///\defgroup observer observer
/// A templated implementation to simplify the creation of the observer pattern
/// and attempts to eliminate certain runtime errors by turning them into compile errors.
/// The pattern consists of two template classes.
/// \li <b>Observer</b><br>
/// This template may take up to eight notification types.
/// Each notification type will generate a pure virtual 'notification'
/// function. The class that inherits from this *must* define all
/// of the 'notification' function overloads otherwise the object will
/// remain 'abstract' and will not compile.
/// This ensures that no overload can be forgotten.<br>
///
/// \li <b>observable</b><br>
/// The class derived from this will be observed by the above class.
/// It keeps a list of registered observers and will notify all
/// of them with the notifications.
///\ingroup patterns
//*****************************************************************************
#include <algorithm>
#include "vector.h"
#include "exception.h"
#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif
namespace etl
{
//***************************************************************************
///\ingroup observer
/// The base class for observer exceptions.
//***************************************************************************
class observer_exception : public exception
{
public:
observer_exception(const char* what)
: exception(what)
{
}
};
//***************************************************************************
///\ingroup observer
/// The exception thrown when the observer list is full.
//***************************************************************************
class observer_list_full : public observer_exception
{
public:
observer_list_full()
: observer_exception("observer: list full")
{
}
};
//*********************************************************************
/// The object that is being observed.
///\tparam TObserver The observer type.
///\tparam MAX_OBSERVERS The maximum number of observers that can be accomodated.
///\ingroup observer
//*********************************************************************
template <typename TObserver, const size_t MAX_OBSERVERS>
class observable
{
public:
typedef size_t size_type;
typedef etl::vector<TObserver*, MAX_OBSERVERS> Observer_List;
//*****************************************************************
/// Add an observer to the list.
/// If ETL_THROW_EXCEPTIONS is defined then an etl::observable_observer_list_full
/// is emitted if the observer list is already full.
///\param observer A reference to the observer.
//*****************************************************************
void add_observer(TObserver& observer)
{
// See if we already have it in our list.
typename Observer_List::const_iterator i_observer = std::find(observer_list.begin(),
observer_list.end(),
&observer);
// Not there?
if (i_observer == observer_list.end())
{
// Is there enough room?
if (!observer_list.full())
{
// Add it.
observer_list.push_back(&observer);
}
#ifdef ETL_THROW_EXCEPTIONS
else
{
throw observer_list_full();
}
#else
else
{
error_handler::error(observer_list_full());
}
#endif
}
}
//*****************************************************************
/// Remove a particular observer from the list.
///\param observer A reference to the observer.
//*****************************************************************
void remove_observer(TObserver& observer)
{
// See if we have it in our list.
typename Observer_List::iterator i_observer = std::find(observer_list.begin(),
observer_list.end(),
&observer);
// Found it?
if (i_observer != observer_list.end())
{
// Erase it.
observer_list.erase(i_observer);
}
}
//*****************************************************************
/// Clear all observers from the list.
//*****************************************************************
void clear_observers()
{
observer_list.clear();
}
//*****************************************************************
/// Returns the number of observers.
//*****************************************************************
size_type number_of_observers() const
{
return observer_list.size();
}
//*****************************************************************
/// Notify all of the observers, sending them the notification.
///\tparam TNotification the notification type.
///\param n The notification.
//*****************************************************************
template <typename TNotification>
void notify_observers(TNotification n)
{
for (size_t i = 0; i < observer_list.size(); ++i)
{
observer_list[i]->notification(n);
}
}
private:
/// The list of observers.
Observer_List observer_list;
};
//*********************************************************************
/// The observer interface for eight notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2 = void,
typename T3 = void,
typename T4 = void,
typename T5 = void,
typename T6 = void,
typename T7 = void,
typename T8 = void>
class observer
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
virtual void notification(T4) = 0;
virtual void notification(T5) = 0;
virtual void notification(T6) = 0;
virtual void notification(T7) = 0;
virtual void notification(T8) = 0;
};
//*********************************************************************
/// The observer interface for seven notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7>
class observer<T1, T2, T3, T4, T5, T6, T7>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
virtual void notification(T4) = 0;
virtual void notification(T5) = 0;
virtual void notification(T6) = 0;
virtual void notification(T7) = 0;
};
//*********************************************************************
/// The observer interface for six notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6>
class observer<T1, T2, T3, T4, T5, T6>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
virtual void notification(T4) = 0;
virtual void notification(T5) = 0;
virtual void notification(T6) = 0;
};
//*********************************************************************
/// The observer interface for five notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2,
typename T3,
typename T4,
typename T5>
class observer<T1, T2, T3, T4, T5>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
virtual void notification(T4) = 0;
virtual void notification(T5) = 0;
};
//*********************************************************************
/// The observer interface for four notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2,
typename T3,
typename T4>
class observer<T1, T2, T3, T4>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
virtual void notification(T4) = 0;
};
//*********************************************************************
/// The observer interface for three notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2,
typename T3>
class observer<T1, T2, T3>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
virtual void notification(T3) = 0;
};
//*********************************************************************
/// The observer interface for two notification types.
///\ingroup observer
//*********************************************************************
template <typename T1,
typename T2>
class observer<T1, T2>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
virtual void notification(T2) = 0;
};
//*********************************************************************
/// The observer interface for one notification type.
///\ingroup observer
//*********************************************************************
template <typename T1>
class observer<T1>
{
public:
virtual ~observer() {}
virtual void notification(T1) = 0;
};
}
#endif