Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
observer.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_OBSERVER__
30 #define __ETL_OBSERVER__
31 
32 //*****************************************************************************
50 //*****************************************************************************
51 
52 #include <algorithm>
53 #include "vector.h"
54 #include "exception.h"
55 
56 namespace etl
57 {
58 #ifdef ETL_THROW_EXCEPTIONS
59  //***************************************************************************
62  //***************************************************************************
63  class observer_exception : public exception
64  {
65  public:
66 
67  observer_exception(const char* what)
68  : exception(what)
69  {
70  }
71  };
72 
73  //***************************************************************************
76  //***************************************************************************
77  class observer_list_full : public observer_exception
78  {
79  public:
80 
81  observer_list_full()
82  : observer_exception("observer: list full")
83  {
84  }
85  };
86 #endif
87 
88  //*********************************************************************
93  //*********************************************************************
94  template <typename TObserver, const size_t MAX_OBSERVERS>
95  class observable
96  {
97  public:
98 
99  typedef size_t size_type;
100 
102 
103  //*****************************************************************
108  //*****************************************************************
109  void add_observer(TObserver& observer)
110  {
111  // See if we already have it in our list.
112  typename Observer_List::const_iterator i_observer = std::find(observer_list.begin(),
113  observer_list.end(),
114  &observer);
115 
116  // Not there?
117  if (i_observer == observer_list.end())
118  {
119  // Is there enough room?
120  if (!observer_list.full())
121  {
122  // Add it.
123  observer_list.push_back(&observer);
124  }
125 #ifdef ETL_THROW_EXCEPTIONS
126  else
127  {
128  throw observer_list_full();
129  }
130 #endif
131  }
132  }
133 
134  //*****************************************************************
137  //*****************************************************************
138  void remove_observer(TObserver& observer)
139  {
140  // See if we have it in our list.
141  typename Observer_List::iterator i_observer = std::find(observer_list.begin(),
142  observer_list.end(),
143  &observer);
144 
145  // Found it?
146  if (i_observer != observer_list.end())
147  {
148  // Erase it.
149  observer_list.erase(i_observer);
150  }
151  }
152 
153  //*****************************************************************
155  //*****************************************************************
157  {
158  observer_list.clear();
159  }
160 
161  //*****************************************************************
163  //*****************************************************************
164  size_type number_of_observers() const
165  {
166  return observer_list.size();
167  }
168 
169  //*****************************************************************
173  //*****************************************************************
174  template <typename TNotification>
175  void notify_observers(TNotification n)
176  {
177  for (size_t i = 0; i < observer_list.size(); ++i)
178  {
179  observer_list[i]->notification(n);
180  }
181  }
182 
183  private:
184 
186  Observer_List observer_list;
187  };
188 
189  //*********************************************************************
192  //*********************************************************************
193  template <typename T1,
194  typename T2 = void,
195  typename T3 = void,
196  typename T4 = void,
197  typename T5 = void,
198  typename T6 = void,
199  typename T7 = void,
200  typename T8 = void>
201  class observer
202  {
203  public:
204  virtual ~observer() {}
205  virtual void notification(T1) = 0;
206  virtual void notification(T2) = 0;
207  virtual void notification(T3) = 0;
208  virtual void notification(T4) = 0;
209  virtual void notification(T5) = 0;
210  virtual void notification(T6) = 0;
211  virtual void notification(T7) = 0;
212  virtual void notification(T8) = 0;
213  };
214 
215  //*********************************************************************
218  //*********************************************************************
219  template <typename T1,
220  typename T2,
221  typename T3,
222  typename T4,
223  typename T5,
224  typename T6,
225  typename T7>
226  class observer<T1, T2, T3, T4, T5, T6, T7>
227  {
228  public:
229 
230  virtual ~observer() {}
231  virtual void notification(T1) = 0;
232  virtual void notification(T2) = 0;
233  virtual void notification(T3) = 0;
234  virtual void notification(T4) = 0;
235  virtual void notification(T5) = 0;
236  virtual void notification(T6) = 0;
237  virtual void notification(T7) = 0;
238  };
239 
240  //*********************************************************************
243  //*********************************************************************
244  template <typename T1,
245  typename T2,
246  typename T3,
247  typename T4,
248  typename T5,
249  typename T6>
250  class observer<T1, T2, T3, T4, T5, T6>
251  {
252  public:
253 
254  virtual ~observer() {}
255  virtual void notification(T1) = 0;
256  virtual void notification(T2) = 0;
257  virtual void notification(T3) = 0;
258  virtual void notification(T4) = 0;
259  virtual void notification(T5) = 0;
260  virtual void notification(T6) = 0;
261  };
262 
263  //*********************************************************************
266  //*********************************************************************
267  template <typename T1,
268  typename T2,
269  typename T3,
270  typename T4,
271  typename T5>
272  class observer<T1, T2, T3, T4, T5>
273  {
274  public:
275 
276  virtual ~observer() {}
277  virtual void notification(T1) = 0;
278  virtual void notification(T2) = 0;
279  virtual void notification(T3) = 0;
280  virtual void notification(T4) = 0;
281  virtual void notification(T5) = 0;
282  };
283 
284  //*********************************************************************
287  //*********************************************************************
288  template <typename T1,
289  typename T2,
290  typename T3,
291  typename T4>
292  class observer<T1, T2, T3, T4>
293  {
294  public:
295 
296  virtual ~observer() {}
297  virtual void notification(T1) = 0;
298  virtual void notification(T2) = 0;
299  virtual void notification(T3) = 0;
300  virtual void notification(T4) = 0;
301  };
302 
303  //*********************************************************************
306  //*********************************************************************
307  template <typename T1,
308  typename T2,
309  typename T3>
310  class observer<T1, T2, T3>
311  {
312  public:
313 
314  virtual ~observer() {}
315  virtual void notification(T1) = 0;
316  virtual void notification(T2) = 0;
317  virtual void notification(T3) = 0;
318  };
319 
320  //*********************************************************************
323  //*********************************************************************
324  template <typename T1,
325  typename T2>
326  class observer<T1, T2>
327  {
328  public:
329 
330  virtual ~observer() {}
331  virtual void notification(T1) = 0;
332  virtual void notification(T2) = 0;
333  };
334 
335  //*********************************************************************
338  //*********************************************************************
339  template <typename T1>
340  class observer<T1>
341  {
342  public:
343 
344  virtual ~observer() {}
345  virtual void notification(T1) = 0;
346  };
347 }
348 
349 #endif
void push_back(parameter_t value)
Definition: ivector.h:410
iterator end()
Definition: ivector.h:117
void clear()
Definition: ivector.h:546
void add_observer(TObserver &observer)
Definition: observer.h:109
Definition: observer.h:201
Definition: observer.h:95
Definition: algorithm.h:43
void remove_observer(TObserver &observer)
Definition: observer.h:138
iterator begin()
Definition: ivector.h:99
iterator erase(iterator iElement)
Definition: ivector.h:518
void notify_observers(TNotification n)
Definition: observer.h:175
bool full() const
Definition: vector_base.h:134
size_type number_of_observers() const
Returns the number of observers.
Definition: observer.h:164
void clear_observers()
Clear all observers from the list.
Definition: observer.h:156
size_type size() const
Definition: vector_base.h:116