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