1 | // This file is part of the PACPUS framework distributed under the
|
---|
2 | // CECILL-C License, Version 1.0.
|
---|
3 | //
|
---|
4 | /// @file
|
---|
5 | /// @author Stephane Bonnet <firstname.surname@utc.fr>
|
---|
6 | /// @date October, 2012
|
---|
7 | /// @version $Id: GenericObservable.h 66 2013-01-09 16:54:11Z kurdejma $
|
---|
8 | /// @copyright Copyright (c) UTC/CNRS Heudiasyc 2006 - 2013. All rights reserved.
|
---|
9 | /// @brief Brief description.
|
---|
10 | ///
|
---|
11 | /// Detailed description.
|
---|
12 |
|
---|
13 | #ifndef DEF_PACPUS_GENERIC_OBSERVER_H
|
---|
14 | #define DEF_PACPUS_GENERIC_OBSERVER_H
|
---|
15 |
|
---|
16 | #include <list>
|
---|
17 |
|
---|
18 | #include <Pacpus/kernel/GenericObserverInterface.h>
|
---|
19 | #include <QMutex>
|
---|
20 |
|
---|
21 | namespace pacpus {
|
---|
22 |
|
---|
23 | /** Base class for te GenericObservable template.
|
---|
24 |
|
---|
25 | The purpose of this class is to be able to take generic pointers on
|
---|
26 | GenericObservable class template instances.
|
---|
27 | */
|
---|
28 | class GenericObservableBase {
|
---|
29 | public:
|
---|
30 | virtual ~GenericObservableBase() {}
|
---|
31 | };
|
---|
32 |
|
---|
33 | /** GenericObservable
|
---|
34 | Base class template for observable objects (see Observable/Observer
|
---|
35 | design pattern).
|
---|
36 |
|
---|
37 | This class implements a simple subject able to notify a list of
|
---|
38 | observers through calls to the notifyObservers protected method.
|
---|
39 | */
|
---|
40 | template <typename T>
|
---|
41 | class GenericObservable : GenericObservableBase {
|
---|
42 | public:
|
---|
43 | typedef GenericObserverInterface<T> ObserverType;
|
---|
44 |
|
---|
45 | GenericObservable() {}
|
---|
46 | virtual ~GenericObservable() {}
|
---|
47 |
|
---|
48 | /** Attaches a new observer to the observable.
|
---|
49 | @param observer A reference to a class obeying the GenericObserverInterface interface.
|
---|
50 | */
|
---|
51 | void attachObserver(ObserverType& observer) {
|
---|
52 | observers_.push_back(&observer);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Detaches an observer.
|
---|
56 |
|
---|
57 | @param observer A reference to a class obeying the GenericObserverInterface interface.
|
---|
58 | @note Does nothing if the observer has not been previously attached
|
---|
59 | */
|
---|
60 | void detachObserver(ObserverType& observer) {
|
---|
61 | observers_.remove(&observer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Notifies all observers about an observable event.
|
---|
65 | */
|
---|
66 | void notifyObservers() {
|
---|
67 | typename std::list<ObserverType*>::const_iterator it;
|
---|
68 | for (it = observers_.begin(); it != observers_.end(); ++it) {
|
---|
69 | (*it)->update(static_cast<T*>(this));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private:
|
---|
74 | std::list<ObserverType*> observers_;
|
---|
75 | };
|
---|
76 |
|
---|
77 | } // namespace pacpus
|
---|
78 |
|
---|
79 | #endif // DEF_PACPUS_GENERIC_OBSERVER_H
|
---|