source: pacpusframework/branches/0.0.x/include/Pacpus/kernel/GenericObservable.h@ 172

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

Added: automated license updating lines:
%pacpus:license{
%pacpus:license}

  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1// %pacpus:license{
2// This file is part of the PACPUS framework distributed under the
3// CECILL-C License, Version 1.0.
4// %pacpus:license}
5/// @file
6/// @author Stephane Bonnet <firstname.surname@utc.fr>
7/// @date October, 2012
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.
13
14#ifndef DEF_PACPUS_GENERIC_OBSERVER_H
15#define DEF_PACPUS_GENERIC_OBSERVER_H
16
17#include <list>
18
19#include <Pacpus/kernel/GenericObserverInterface.h>
20#include <QMutex>
21
22namespace 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*/
29class GenericObservableBase
30{
31public:
32 /// @todo Documentation
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*/
43template <typename T>
44class GenericObservable
45 : GenericObservableBase
46{
47public:
48 /// @todo Documentation
49 typedef GenericObserverInterface<T> ObserverType;
50
51 /// @todo Documentation
52 GenericObservable() {}
53 /// @todo Documentation
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
81private:
82 std::list<ObserverType*> observers_;
83};
84
85} // namespace pacpus
86
87#endif // DEF_PACPUS_GENERIC_OBSERVER_H
Note: See TracBrowser for help on using the repository browser.