BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/facilities/Observer.h
Go to the documentation of this file.
1// ## begin module%352D11EA00FB.cm preserve=no
2// %X% %Q% %Z% %W%
3// ## end module%352D11EA00FB.cm
4
5// ## Module: Observer%352D11EA00FB; Package specification
6// Implementation of the Observer class.
7// ## Subsystem: facilities%3550CC6801AC
8// ## Source file: D:\code\glastsim\facilities\Observer.h
9
10#ifndef Observer_h
11#define Observer_h 1
12
13#include "facilities/Adapter.h"
14// ## begin module%352D11EA00FB.declarations preserve=yes
15#ifdef _MSC_VER
16# pragma warning( disable : 4786 )
17#endif
18
19#include <vector>
20// ## end module%352D11EA00FB.declarations
21
22// ## Class: Observer%352D0D8F02E2
23// Abstract observer class. Encapsulates the update()
24// behavior.
25// ## Category: facilities%3550CC5302A6
26// ## Subsystem: facilities%3550CC6801AC
27// ## Persistence: Transient
28// ## Cardinality/Multiplicity: n
29
30class Observer {
31public:
32 // ## Constructors (specified)
33 // ## Operation: Observer%894312585
34 // constructor
36 // ## begin Observer::Observer%894312585.initialization preserve=yes
37 // ## end Observer::Observer%894312585.initialization
38 {
39 // ## begin Observer::Observer%894312585.body preserve=yes
40 // ## end Observer::Observer%894312585.body
41 }
42
43 // ## Other Operations (specified)
44 // ## Operation: update%892143866
45 // Encapsulate the update behavior for the Subject-Observer
46 // pattern. Respond to a change in the state of the Subject
47 // to which this observer (when instatiated) is attached.
48 virtual void update() = 0;
49
50protected:
51};
52
53// ## Class: Subject%352D0AF10220; Abstract
54// Abstract subject pattern. Follows Observer behavioral
55// pattern described in 'Design Patterns'.
56// ## Category: facilities%3550CC5302A6
57// ## Subsystem: facilities%3550CC6801AC
58// ## Persistence: Transient
59// ## Cardinality/Multiplicity: n
60
61// ## Uses: <unnamed>%352D101A02D4;Observer { -> }
62
63class Subject {
64public:
65 // ## Constructors (specified)
66 // ## Operation: Subject%894312586
67 // constructor
69 // ## begin Subject::Subject%894312586.initialization preserve=yes
70 : m_observers()
71 // ## end Subject::Subject%894312586.initialization
72 {
73 // ## begin Subject::Subject%894312586.body preserve=yes
74 // ## end Subject::Subject%894312586.body
75 }
76
77 // ## Other Operations (specified)
78 // ## Operation: attach%892143867
79 // Attach an observer to this subject.
80 void attach( Observer* anObserver ) {
81 // ## begin Subject::attach%892143867.body preserve=yes
82 m_observers.push_back( anObserver );
83 // ## end Subject::attach%892143867.body
84 }
85
86 // ## Operation: detach%892143868
87 // Detach an observer from this subject.
88 void detach( Observer* ) {
89 // ## begin Subject::detach%892143868.body preserve=yes
90 // std::vector<Observer*>::const_iterator it = m_observers.find(anObserver);
91 // if (it != m_observers.end()) m_observers.erase(it);
92 // ## end Subject::detach%892143868.body
93 }
94
95 // ## Operation: notify%892143869
96 // Notify all subjects of a change.
97 void notify() {
98 // ## begin Subject::notify%892143869.body preserve=yes
99 std::vector<Observer*>::iterator it = m_observers.begin();
100 while ( it != m_observers.end() )
101 {
102 if ( *it ) ( *it )->update();
103 it++;
104 }
105 // ## end Subject::notify%892143869.body
106 }
107
108protected:
109private:
110private: // ## implementation
111 // Data Members for Class Attributes
112 // ## Attribute: m_observers%352D1996009B
113 // List of all of the observers of this subject.
114 // ## begin Subject::m_observers%352D1996009B.attr preserve=no private: set<Observer*> {U}
115 std::vector<Observer*> m_observers;
116 // ## end Subject::m_observers%352D1996009B.attr
117};
118
119// ## Class: ObserverAdapter%3552473D03A8; Parameterized Class
120// Subclass of Observer which incorporates a callback
121// Adapter to another object.
122// ## Category: facilities%3550CC5302A6
123// ## Subsystem: facilities%3550CC6801AC
124// ## Persistence: Transient
125// ## Cardinality/Multiplicity: n
126
127template <class T, class Y = int>
128class ObserverAdapter : public Observer // ## Inherits: <unnamed>%3552488300D6
129{
130public:
131 // ## Constructors (specified)
132 // ## Operation: ObserverAdapter%894312604
133 // constructor
134 ObserverAdapter( Adapter<Y>* anAdapter = 0 )
135 // ## begin ObserverAdapter::ObserverAdapter%894312604.initialization preserve=yes
136 : itsAdapter( anAdapter )
137 // ## end ObserverAdapter::ObserverAdapter%894312604.initialization
138 {
139 // ## begin ObserverAdapter::ObserverAdapter%894312604.body preserve=yes
140 // ## end ObserverAdapter::ObserverAdapter%894312604.body
141 }
142
144 delete itsAdapter;
145 itsAdapter = 0;
146 }
147
148 // ## Other Operations (specified)
149 // ## Operation: setAdapter%894312605
150 // sets the adapter to use
151 void setAdapter( Adapter<Y>* anAdapter = 0 ) {
152 // ## begin ObserverAdapter::setAdapter%894312605.body preserve=yes
153 delete itsAdapter;
154 itsAdapter = anAdapter;
155 // ## end ObserverAdapter::setAdapter%894312605.body
156 }
157
158 // ## Operation: getAdapter%894312606
159 // access to the adapter object
161 // ## begin ObserverAdapter::getAdapter%894312606.body preserve=yes
162 return itsAdapter;
163 // ## end ObserverAdapter::getAdapter%894312606.body
164 }
165
166 // ## Operation: update%894312607
167 // update overload. calls itsAdapter to execute the adapted
168 // function
169 void update() {
170 // ## begin ObserverAdapter::update%894312607.body preserve=yes
171 if ( itsAdapter ) ( *itsAdapter )();
172 // ## end ObserverAdapter::update%894312607.body
173 }
174
175protected:
176private:
177private: // ## implementation
178 // Data Members for Associations
179 // ## Association: facilities::<unnamed>%3550D15301CA
180 // ## Role: ObserverAdapter::itsAdapter%3550D1540059
181 // adapter to call upon notification of a change in a
182 // Subject
183 // ## begin ObserverAdapter::itsAdapter%3550D1540059.role preserve=no private: ActionAdapter
184 // { -> 0..1RHN}
185 Adapter<Y>* itsAdapter;
186 // ## end ObserverAdapter::itsAdapter%3550D1540059.role
187};
188
189#endif
virtual void update()=0