source: pacpusframework/trunk/include/Pacpus/kernel/GenericObservable.h@ 57

Last change on this file since 57 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

File size: 2.1 KB
Line 
1/**
2 *
3 * Distributed under the UTC Heudiascy Pacpus License, Version 1.0.
4 * Copyright (c) UTC Heudiasyc 2010 - 2013. All rights reserved.
5 *
6 * See the LICENSE file for more information or a copy at:
7 * http://www.hds.utc.fr/~kurdejma/LICENSE_1_0.txt
8 *
9 */
10
11#ifndef DEF_PACPUS_GENERIC_OBSERVER_H
12#define DEF_PACPUS_GENERIC_OBSERVER_H
13
14#include <list>
15
16#include <Pacpus/kernel/GenericObserverInterface.h>
17#include <QMutex>
18
19namespace pacpus {
20
21/** Base class for te GenericObservable template.
22
23 The purpose of this class is to be able to take generic pointers on
24 GenericObservable class template instances.
25*/
26class GenericObservableBase {
27public:
28 virtual ~GenericObservableBase() {}
29};
30
31/** GenericObservable
32 Base class template for observable objects (see Observable/Observer
33 design pattern).
34
35 This class implements a simple subject able to notify a list of
36 observers through calls to the notifyObservers protected method.
37*/
38template <typename T>
39class GenericObservable : GenericObservableBase {
40public:
41 typedef GenericObserverInterface<T> ObserverType;
42
43 GenericObservable() {}
44 virtual ~GenericObservable() {}
45
46 /** Attaches a new observer to the observable.
47 @param observer A reference to a class obeying the GenericObserverInterface interface.
48 */
49 void attachObserver(ObserverType& observer) {
50 observers_.push_back(&observer);
51 }
52
53 /** Detaches an observer.
54
55 @param observer A reference to a class obeying the GenericObserverInterface interface.
56 @note Does nothing if the observer has not been previously attached
57 */
58 void detachObserver(ObserverType& observer) {
59 observers_.remove(&observer);
60 }
61
62 /** Notifies all observers about an observable event.
63 */
64 void notifyObservers() {
65 typename std::list<ObserverType*>::const_iterator it;
66 for (it = observers_.begin(); it != observers_.end(); ++it) {
67 (*it)->update(static_cast<T*>(this));
68 }
69 }
70
71private:
72 std::list<ObserverType*> observers_;
73};
74
75} // namespace pacpus
76
77#endif // DEF_PACPUS_GENERIC_OBSERVER_H
Note: See TracBrowser for help on using the repository browser.