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