Changes in src/Patterns/Observer.cpp [4fb5a3:cf1a07]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Patterns/Observer.cpp
r4fb5a3 rcf1a07 10 10 11 11 #include <iostream> 12 13 #include "Helpers/Assert.hpp" 12 #include <cassert> 14 13 15 14 using namespace std; … … 25 24 // See [Gamma et al, 1995] p. 297 26 25 27 map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers28 map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers29 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; 30 29 31 /** Attaching Sub-observables to Observables. 32 * Increases entry in Observable::depth for this \a *publisher by one. 33 * 34 * The two functions \sa start_observer_internal() and \sa finish_observer_internal() 35 * have to be used together at all time. Never use these functions directly 36 * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop 37 * thus producing compiler-errors whenever only one is used. 38 * \param *publisher reference of sub-observable 39 */ 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 40 35 void Observable::start_observer_internal(Observable *publisher){ 41 36 // increase the count for this observable by one … … 45 40 } 46 41 47 /** Detaching Sub-observables from Observables.48 * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we49 * start notifying all our Observers.50 *51 * The two functions start_observer_internal() and finish_observer_internal()52 * have to be used together at all time. Never use these functions directly53 * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop54 * thus producing compiler-errors whenever only one is used.55 * \param *publisher reference of sub-observable56 */57 42 void Observable::finish_observer_internal(Observable *publisher){ 58 43 // decrease the count for this observable … … 68 53 } 69 54 70 /** Constructor for Observable Protector.71 * Basically, calls start_observer_internal(). Hence use this class instead of72 * calling the function directly.73 *74 * \param *protege Observable to be protected.75 */76 55 Observable::_Observable_protector::_Observable_protector(Observable *_protege) : 77 56 protege(_protege) … … 80 59 } 81 60 82 /** Destructor for Observable Protector.83 * Basically, calls finish_observer_internal(). Hence use this class instead of84 * calling the function directly.85 *86 * \param *protege Observable to be protected.87 */88 61 Observable::_Observable_protector::~_Observable_protector() 89 62 { … … 93 66 /************* Notification mechanism for observables **************/ 94 67 95 /** Notify all Observers of changes. 96 * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t 97 * and removes from busy list. 98 */ 68 99 69 void Observable::notifyAll() { 100 70 // we are busy notifying others right now … … 117 87 } 118 88 119 /** Handles passing on updates from sub-Observables. 120 * Mimicks basically the Observer::update() function. 121 * 122 * \param *publisher The \a *this we observe. 123 */ 89 // this handles passing on updates from sub-Observables 124 90 void Observable::update(Observable *publisher) { 125 91 // circle detection … … 128 94 // observers, but still we are called by one of our sub-Observables 129 95 // we cannot be sure observation will still work at this point 130 ASSERT(0,"Circle detected in observation-graph.\n"131 "Observation-graph always needs to be a DAG to work correctly!\n"132 "Please check your observation code and fix this!\n");96 cerr << "Circle detected in observation-graph." << endl; 97 cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl; 98 cerr << "Please check your observation code and fix this!" << endl; 133 99 return; 134 100 } … … 143 109 } 144 110 145 /** Sign on an Observer to this Observable. 146 * Puts \a *target into Observable::callTable list. 147 * \param *target Observer 148 * \param priority number in [-20,20] 149 */ 111 // methods to sign-on and off 150 112 void Observable::signOn(Observer *target,int priority) { 151 ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");113 assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]"); 152 114 bool res = false; 153 115 callees_t *callees = 0; … … 168 130 } 169 131 170 /** Sign off an Observer from this Observable.171 * Removes \a *target from Observable::callTable list.172 * \param *target Observer173 */174 132 void Observable::signOff(Observer *target) { 175 ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");133 assert(callTable.count(this) && "SignOff called for an Observable without Observers."); 176 134 callees_t *callees = callTable[this]; 177 135 callees_t::iterator iter; 178 callees_t::iterator deliter;179 136 for(iter=callees->begin();iter!=callees->end();) { 180 137 if((*iter).second == target) { … … 191 148 } 192 149 193 bool Observable::isBlocked(){ 194 return depth.count(this) > 0; 195 } 196 197 /** Handles sub-observables that just got killed 198 * when an sub-observerable dies we usually don't need to do anything 199 * \param *publisher Sub-Observable. 200 */ 150 // when an sub-observerable dies we usually don't need to do anything 201 151 void Observable::subjectKilled(Observable *publisher){ 202 152 } 203 153 204 /** Constructor for class Observable.205 */206 154 Observable::Observable() 207 155 {} 208 156 209 /** Destructor for class Observable. 210 * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled(). 211 */ 157 // when an observable is deleted, we let all our observers know 212 158 Observable::~Observable() 213 159 { … … 224 170 } 225 171 226 /** Constructor for class Observer.227 */228 172 Observer::Observer() 229 173 {} 230 174 231 /** Destructor for class Observer.232 */233 175 Observer::~Observer() 234 176 {}
Note:
See TracChangeset
for help on using the changeset viewer.