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

Last change on this file since 12 was 12, checked in by sgosseli, 12 years ago

Update some Qt includes.

File size: 1.8 KB
RevLine 
[3]1#pragma once
2#ifndef __GENERIC_OBSERVER_H__
3#define __GENERIC_OBSERVER_H__
4
5#include <list>
6
7#include "GenericObserverInterface.h"
[12]8#include <QMutex>
[3]9
10namespace pacpus {
11
12/** Base class for te GenericObservable template.
13
14 The purpose of this class is to be able to take generic pointers on
15 GenericObservable class template instances.
16*/
17class GenericObservableBase {
18public:
19 virtual ~GenericObservableBase() {}
20};
21
22/** GenericObservable
23 Base class template for observable objects (see Observable/Observer
24 design pattern).
25
26 This class implements a simple subject able to notify a list of
27 observers through calls to the notifyObservers protected method.
28*/
29template <typename T>
30class GenericObservable : GenericObservableBase {
31public:
32 typedef GenericObserverInterface<T> ObserverType;
33
34 GenericObservable() {}
35 virtual ~GenericObservable() {}
36
37 /** Attaches a new observer to the observable.
38 @param observer A reference to a class obeying the GenericObserverInterface interface.
39 */
40 void attachObserver(ObserverType& observer) {
41 observers_.push_back(&observer);
42 }
43
44 /** Detaches an observer.
45
46 @param observer A reference to a class obeying the GenericObserverInterface interface.
47 @note Does nothing if the observer has not been previously attached
48 */
49 void detachObserver(ObserverType& observer) {
50 observers_.remove(&observer);
51 }
52
53 /** Notifies all observers about an observable event.
54 */
55 void notifyObservers() {
56 typename std::list<ObserverType*>::const_iterator it;
57 for (it = observers_.begin(); it != observers_.end(); ++it) {
58 (*it)->update(static_cast<T*>(this));
59 }
60 }
61
62private:
63 std::list<ObserverType*> observers_;
64};
65
66} // namespace pacpus
67
68#endif
Note: See TracBrowser for help on using the repository browser.