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

Last change on this file since 67 was 67, checked in by Marek Kurdej, 11 years ago

Documentation: file info.
Fixed: problem with includes in PacpusPluginInterface.h.

  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
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 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.
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
21namespace 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*/
28class GenericObservableBase
29{
30public:
31 /// @todo Documentation
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*/
42template <typename T>
43class GenericObservable
44 : GenericObservableBase
45{
46public:
47 /// @todo Documentation
48 typedef GenericObserverInterface<T> ObserverType;
49
50 /// @todo Documentation
51 GenericObservable() {}
52 /// @todo Documentation
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
80private:
81 std::list<ObserverType*> observers_;
82};
83
84} // namespace pacpus
85
86#endif // DEF_PACPUS_GENERIC_OBSERVER_H
Note: See TracBrowser for help on using the repository browser.