BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/facilities-00-00-04/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
23//## Class: Observer%352D0D8F02E2
24// Abstract observer class. Encapsulates the update()
25// behavior.
26//## Category: facilities%3550CC5302A6
27//## Subsystem: facilities%3550CC6801AC
28//## Persistence: Transient
29//## Cardinality/Multiplicity: n
30
32{
33 public:
34 //## Constructors (specified)
35 //## Operation: Observer%894312585
36 // constructor
38 //## begin Observer::Observer%894312585.initialization preserve=yes
39 //## end Observer::Observer%894312585.initialization
40 {
41 //## begin Observer::Observer%894312585.body preserve=yes
42 //## end Observer::Observer%894312585.body
43 }
44
45
46 //## Other Operations (specified)
47 //## Operation: update%892143866
48 // Encapsulate the update behavior for the Subject-Observer
49 // pattern. Respond to a change in the state of the Subject
50 // to which this observer (when instatiated) is attached.
51 virtual void update () = 0;
52
53 protected:
54
55};
56
57//## Class: Subject%352D0AF10220; Abstract
58// Abstract subject pattern. Follows Observer behavioral
59// pattern described in 'Design Patterns'.
60//## Category: facilities%3550CC5302A6
61//## Subsystem: facilities%3550CC6801AC
62//## Persistence: Transient
63//## Cardinality/Multiplicity: n
64
65//## Uses: <unnamed>%352D101A02D4;Observer { -> }
66
67class Subject
68{
69 public:
70 //## Constructors (specified)
71 //## Operation: Subject%894312586
72 // constructor
74 //## begin Subject::Subject%894312586.initialization preserve=yes
75 : m_observers()
76 //## end Subject::Subject%894312586.initialization
77 {
78 //## begin Subject::Subject%894312586.body preserve=yes
79 //## end Subject::Subject%894312586.body
80 }
81
82
83 //## Other Operations (specified)
84 //## Operation: attach%892143867
85 // Attach an observer to this subject.
86 void attach (Observer* anObserver)
87 {
88 //## begin Subject::attach%892143867.body preserve=yes
89 m_observers.push_back(anObserver);
90 //## end Subject::attach%892143867.body
91 }
92
93 //## Operation: detach%892143868
94 // Detach an observer from this subject.
96 {
97 //## begin Subject::detach%892143868.body preserve=yes
98 //std::vector<Observer*>::const_iterator it = m_observers.find(anObserver);
99 //if (it != m_observers.end()) m_observers.erase(it);
100 //## end Subject::detach%892143868.body
101 }
102
103 //## Operation: notify%892143869
104 // Notify all subjects of a change.
105 void notify ()
106 {
107 //## begin Subject::notify%892143869.body preserve=yes
108 std::vector<Observer*>::iterator it = m_observers.begin();
109 while (it != m_observers.end()) {
110 if (*it) (*it)->update();
111 it++;
112 }
113 //## end Subject::notify%892143869.body
114 }
115
116 protected:
117 private:
118 private: //## implementation
119 // Data Members for Class Attributes
120
121 //## Attribute: m_observers%352D1996009B
122 // List of all of the observers of this subject.
123 //## begin Subject::m_observers%352D1996009B.attr preserve=no private: set<Observer*> {U}
124 std::vector<Observer*> m_observers;
125 //## end Subject::m_observers%352D1996009B.attr
126
127};
128
129//## Class: ObserverAdapter%3552473D03A8; Parameterized Class
130// Subclass of Observer which incorporates a callback
131// Adapter to another object.
132//## Category: facilities%3550CC5302A6
133//## Subsystem: facilities%3550CC6801AC
134//## Persistence: Transient
135//## Cardinality/Multiplicity: n
136
137template <class T, class Y = int>
138class ObserverAdapter : public Observer //## Inherits: <unnamed>%3552488300D6
139{
140 public:
141 //## Constructors (specified)
142 //## Operation: ObserverAdapter%894312604
143 // constructor
145 //## begin ObserverAdapter::ObserverAdapter%894312604.initialization preserve=yes
146 : itsAdapter(anAdapter)
147 //## end ObserverAdapter::ObserverAdapter%894312604.initialization
148 {
149 //## begin ObserverAdapter::ObserverAdapter%894312604.body preserve=yes
150 //## end ObserverAdapter::ObserverAdapter%894312604.body
151 }
152
154 {
155 delete itsAdapter;
156 itsAdapter = 0;
157 }
158
159
160 //## Other Operations (specified)
161 //## Operation: setAdapter%894312605
162 // sets the adapter to use
163 void setAdapter (Adapter<Y>* anAdapter = 0)
164 {
165 //## begin ObserverAdapter::setAdapter%894312605.body preserve=yes
166 delete itsAdapter;
167 itsAdapter = anAdapter;
168 //## end ObserverAdapter::setAdapter%894312605.body
169 }
170
171 //## Operation: getAdapter%894312606
172 // access to the adapter object
174 {
175 //## begin ObserverAdapter::getAdapter%894312606.body preserve=yes
176 return itsAdapter;
177 //## end ObserverAdapter::getAdapter%894312606.body
178 }
179
180 //## Operation: update%894312607
181 // update overload. calls itsAdapter to execute the adapted
182 // function
183 void update ()
184 {
185 //## begin ObserverAdapter::update%894312607.body preserve=yes
186 if (itsAdapter) (*itsAdapter)();
187 //## end ObserverAdapter::update%894312607.body
188 }
189
190 protected:
191 private:
192 private: //## implementation
193 // Data Members for Associations
194
195 //## Association: facilities::<unnamed>%3550D15301CA
196 //## Role: ObserverAdapter::itsAdapter%3550D1540059
197 // adapter to call upon notification of a change in a
198 // Subject
199 //## begin ObserverAdapter::itsAdapter%3550D1540059.role preserve=no private: ActionAdapter { -> 0..1RHN}
200 Adapter<Y> *itsAdapter;
201 //## end ObserverAdapter::itsAdapter%3550D1540059.role
202
203};
204
205
206
207#endif
virtual void update()=0