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

Last change on this file since 62 was 62, checked in by Marek Kurdej, 12 years ago

Update: license info.

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