Changes in src/Patterns/Observer.cpp [c296c2:cf1a07]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/Observer.cpp
rc296c2 rcf1a07 24 24 // See [Gamma et al, 1995] p. 297 25 25 26 map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers27 map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers28 set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers26 map<Observable*, int> Observable::depth; 27 map<Observable*,multimap<int,Observer*>*> Observable::callTable; 28 set<Observable*> Observable::busyObservables; 29 29 30 /** Attaching Sub-observables to Observables. 31 * Increases entry in Observable::depth for this \a *publisher by one. 32 * 33 * The two functions \sa start_observer_internal() and \sa finish_observer_internal() 34 * have to be used together at all time. Never use these functions directly 35 * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop 36 * thus producing compiler-errors whenever only one is used. 37 * \param *publisher reference of sub-observable 38 */ 30 // The two functions start_observer_internal and finish_observer_internal 31 // have to be used together at all time. Never use these functions directly 32 // START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop 33 // thus producing compiler-errors whenever only one is used 34 39 35 void Observable::start_observer_internal(Observable *publisher){ 40 36 // increase the count for this observable by one … … 44 40 } 45 41 46 /** Detaching Sub-observables from Observables.47 * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we48 * start notifying all our Observers.49 *50 * The two functions start_observer_internal() and finish_observer_internal()51 * have to be used together at all time. Never use these functions directly52 * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop53 * thus producing compiler-errors whenever only one is used.54 * \param *publisher reference of sub-observable55 */56 42 void Observable::finish_observer_internal(Observable *publisher){ 57 43 // decrease the count for this observable … … 67 53 } 68 54 69 /** Constructor for Observable Protector.70 * Basically, calls start_observer_internal(). Hence use this class instead of71 * calling the function directly.72 *73 * \param *protege Observable to be protected.74 */75 55 Observable::_Observable_protector::_Observable_protector(Observable *_protege) : 76 56 protege(_protege) … … 79 59 } 80 60 81 /** Destructor for Observable Protector.82 * Basically, calls finish_observer_internal(). Hence use this class instead of83 * calling the function directly.84 *85 * \param *protege Observable to be protected.86 */87 61 Observable::_Observable_protector::~_Observable_protector() 88 62 { … … 92 66 /************* Notification mechanism for observables **************/ 93 67 94 /** Notify all Observers of changes. 95 * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t 96 * and removes from busy list. 97 */ 68 98 69 void Observable::notifyAll() { 99 70 // we are busy notifying others right now … … 108 79 callees_t *callees = callTable[this]; 109 80 callees_t::iterator iter; 110 for(iter=callees->begin();iter!=callees->end(); iter++){81 for(iter=callees->begin();iter!=callees->end();++iter){ 111 82 (*iter).second->update(this); 112 83 } … … 116 87 } 117 88 118 /** Handles passing on updates from sub-Observables. 119 * Mimicks basically the Observer::update() function. 120 * 121 * \param *publisher The \a *this we observe. 122 */ 89 // this handles passing on updates from sub-Observables 123 90 void Observable::update(Observable *publisher) { 124 91 // circle detection … … 142 109 } 143 110 144 /** Sign on an Observer to this Observable. 145 * Puts \a *target into Observable::callTable list. 146 * \param *target Observer 147 * \param priority number in [-20,20] 148 */ 111 // methods to sign-on and off 149 112 void Observable::signOn(Observer *target,int priority) { 150 113 assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]"); … … 160 123 161 124 callees_t::iterator iter; 162 for(iter=callees->begin();iter!=callees->end(); iter++){125 for(iter=callees->begin();iter!=callees->end();++iter){ 163 126 res |= ((*iter).second == target); 164 127 } … … 167 130 } 168 131 169 /** Sign off an Observer from this Observable.170 * Removes \a *target from Observable::callTable list.171 * \param *target Observer172 */173 132 void Observable::signOff(Observer *target) { 174 133 assert(callTable.count(this) && "SignOff called for an Observable without Observers."); 175 134 callees_t *callees = callTable[this]; 176 135 callees_t::iterator iter; 177 callees_t::iterator deliter;178 136 for(iter=callees->begin();iter!=callees->end();) { 179 deliter=iter++; 180 if((*deliter).second == target) 181 callees->erase(deliter); 137 if((*iter).second == target) { 138 callees->erase(iter++); 139 } 140 else { 141 ++iter; 142 } 182 143 } 183 144 if(callees->empty()){ … … 187 148 } 188 149 189 /** Handles sub-observables that just got killed 190 * when an sub-observerable dies we usually don't need to do anything 191 * \param *publisher Sub-Observable. 192 */ 150 // when an sub-observerable dies we usually don't need to do anything 193 151 void Observable::subjectKilled(Observable *publisher){ 194 152 } 195 153 196 /** Constructor for class Observable.197 */198 154 Observable::Observable() 199 155 {} 200 156 201 /** Destructor for class Observable. 202 * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled(). 203 */ 157 // when an observable is deleted, we let all our observers know 204 158 Observable::~Observable() 205 159 { … … 208 162 callees_t *callees = callTable[this]; 209 163 callees_t::iterator iter; 210 for(iter=callees->begin();iter!=callees->end(); iter++){164 for(iter=callees->begin();iter!=callees->end();++iter){ 211 165 (*iter).second->subjectKilled(this); 212 166 } … … 216 170 } 217 171 218 /** Constructor for class Observer.219 */220 172 Observer::Observer() 221 173 {} 222 174 223 /** Destructor for class Observer.224 */225 175 Observer::~Observer() 226 176 {}
Note:
See TracChangeset
for help on using the changeset viewer.