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