[31] | 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 | */
|
---|
[3] | 10 |
|
---|
[31] | 11 | #ifndef DEF_PACPUS_GENERIC_OBSERVER_H
|
---|
| 12 | #define DEF_PACPUS_GENERIC_OBSERVER_H
|
---|
| 13 |
|
---|
[3] | 14 | #include <list>
|
---|
| 15 |
|
---|
[31] | 16 | #include <Pacpus/kernel/GenericObserverInterface.h>
|
---|
[12] | 17 | #include <QMutex>
|
---|
[3] | 18 |
|
---|
| 19 | namespace 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 | */
|
---|
| 26 | class GenericObservableBase {
|
---|
| 27 | public:
|
---|
| 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 | */
|
---|
| 38 | template <typename T>
|
---|
| 39 | class GenericObservable : GenericObservableBase {
|
---|
| 40 | public:
|
---|
| 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 |
|
---|
| 71 | private:
|
---|
| 72 | std::list<ObserverType*> observers_;
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | } // namespace pacpus
|
---|
| 76 |
|
---|
[31] | 77 | #endif // DEF_PACPUS_GENERIC_OBSERVER_H
|
---|