| 1 | /* | 
|---|
| 2 | * Observer.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jan 19, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Observer.hpp" | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | #include <iostream> | 
|---|
| 12 |  | 
|---|
| 13 | #include "Helpers/Assert.hpp" | 
|---|
| 14 |  | 
|---|
| 15 | using namespace std; | 
|---|
| 16 |  | 
|---|
| 17 | /****************** Static stuff for the observer mechanism ************/ | 
|---|
| 18 |  | 
|---|
| 19 | // All infrastructure for the observer-pattern is bundled at a central place | 
|---|
| 20 | // this is more efficient if many objects can be observed (inherit from observable) | 
|---|
| 21 | // but only few are actually coupled with observers. E.g. TMV has over 500.000 Atoms, | 
|---|
| 22 | // which might become observable. Handling Observerable infrastructure in each of | 
|---|
| 23 | // these would use memory for each atom. By handling Observer-infrastructure | 
|---|
| 24 | // here we only need memory for objects that actually are observed. | 
|---|
| 25 | // See [Gamma et al, 1995] p. 297 | 
|---|
| 26 |  | 
|---|
| 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 | */ | 
|---|
| 40 | void Observable::start_observer_internal(Observable *publisher){ | 
|---|
| 41 | // increase the count for this observable by one | 
|---|
| 42 | // if no entry for this observable is found, an new one is created | 
|---|
| 43 | // by the STL and initialized to 0 (see STL documentation) | 
|---|
| 44 | depth[publisher]++; | 
|---|
| 45 | } | 
|---|
| 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 | */ | 
|---|
| 57 | void Observable::finish_observer_internal(Observable *publisher){ | 
|---|
| 58 | // decrease the count for this observable | 
|---|
| 59 | // if zero is reached all observed blocks are done and we can | 
|---|
| 60 | // start to notify our observers | 
|---|
| 61 | if(--(depth[publisher])){} | 
|---|
| 62 | else{ | 
|---|
| 63 | publisher->notifyAll(); | 
|---|
| 64 | // this item is done, so we don't have to keep the count with us | 
|---|
| 65 | // save some memory by erasing it | 
|---|
| 66 | depth.erase(publisher); | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 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 | */ | 
|---|
| 76 | Observable::_Observable_protector::_Observable_protector(Observable *_protege) : | 
|---|
| 77 | protege(_protege) | 
|---|
| 78 | { | 
|---|
| 79 | start_observer_internal(protege); | 
|---|
| 80 | } | 
|---|
| 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 | */ | 
|---|
| 88 | Observable::_Observable_protector::~_Observable_protector() | 
|---|
| 89 | { | 
|---|
| 90 | finish_observer_internal(protege); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | /************* Notification mechanism for observables **************/ | 
|---|
| 94 |  | 
|---|
| 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 | */ | 
|---|
| 99 | void Observable::notifyAll() { | 
|---|
| 100 | // we are busy notifying others right now | 
|---|
| 101 | // add ourselves to the list of busy subjects to enable circle detection | 
|---|
| 102 | busyObservables.insert(this); | 
|---|
| 103 | // see if anyone has signed up for observation | 
|---|
| 104 | // and call all observers | 
|---|
| 105 | if(callTable.count(this)) { | 
|---|
| 106 | // elements are stored sorted by keys in the multimap | 
|---|
| 107 | // so iterating over it gives us a the callees sorted by | 
|---|
| 108 | // the priorities | 
|---|
| 109 | callees_t *callees = callTable[this]; | 
|---|
| 110 | callees_t::iterator iter; | 
|---|
| 111 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 112 | (*iter).second->update(this); | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 | // done with notification, we can leave the set of busy subjects | 
|---|
| 116 | busyObservables.erase(this); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 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 | */ | 
|---|
| 124 | void Observable::update(Observable *publisher) { | 
|---|
| 125 | // circle detection | 
|---|
| 126 | if(busyObservables.find(this)!=busyObservables.end()) { | 
|---|
| 127 | // somehow a circle was introduced... we were busy notifying our | 
|---|
| 128 | // observers, but still we are called by one of our sub-Observables | 
|---|
| 129 | // 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"); | 
|---|
| 133 | return; | 
|---|
| 134 | } | 
|---|
| 135 | else { | 
|---|
| 136 | // see if we are in the process of changing ourselves | 
|---|
| 137 | // if we are changing ourselves at the same time our sub-observables change | 
|---|
| 138 | // we do not need to publish all the changes at each time we are called | 
|---|
| 139 | if(depth.find(this)==depth.end()) { | 
|---|
| 140 | notifyAll(); | 
|---|
| 141 | } | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 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 | */ | 
|---|
| 150 | void Observable::signOn(Observer *target,int priority) { | 
|---|
| 151 | ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer"); | 
|---|
| 152 | bool res = false; | 
|---|
| 153 | callees_t *callees = 0; | 
|---|
| 154 | if(callTable.count(this)){ | 
|---|
| 155 | callees = callTable[this]; | 
|---|
| 156 | } | 
|---|
| 157 | else { | 
|---|
| 158 | callees = new multimap<int,Observer*>; | 
|---|
| 159 | callTable.insert(pair<Observable*,callees_t*>(this,callees)); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | callees_t::iterator iter; | 
|---|
| 163 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 164 | res |= ((*iter).second == target); | 
|---|
| 165 | } | 
|---|
| 166 | if(!res) | 
|---|
| 167 | callees->insert(pair<int,Observer*>(priority,target)); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | /** Sign off an Observer from this Observable. | 
|---|
| 171 | * Removes \a *target from Observable::callTable list. | 
|---|
| 172 | * \param *target Observer | 
|---|
| 173 | */ | 
|---|
| 174 | void Observable::signOff(Observer *target) { | 
|---|
| 175 | ASSERT(callTable.count(this),"SignOff called for an Observable without Observers."); | 
|---|
| 176 | callees_t *callees = callTable[this]; | 
|---|
| 177 | callees_t::iterator iter; | 
|---|
| 178 | callees_t::iterator deliter; | 
|---|
| 179 | for(iter=callees->begin();iter!=callees->end();) { | 
|---|
| 180 | if((*iter).second == target) { | 
|---|
| 181 | callees->erase(iter++); | 
|---|
| 182 | } | 
|---|
| 183 | else { | 
|---|
| 184 | ++iter; | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 | if(callees->empty()){ | 
|---|
| 188 | callTable.erase(this); | 
|---|
| 189 | delete callees; | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 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 | */ | 
|---|
| 201 | void Observable::subjectKilled(Observable *publisher){ | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | /** Constructor for class Observable. | 
|---|
| 205 | */ | 
|---|
| 206 | Observable::Observable() | 
|---|
| 207 | {} | 
|---|
| 208 |  | 
|---|
| 209 | /** Destructor for class Observable. | 
|---|
| 210 | * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled(). | 
|---|
| 211 | */ | 
|---|
| 212 | Observable::~Observable() | 
|---|
| 213 | { | 
|---|
| 214 | if(callTable.count(this)) { | 
|---|
| 215 | // delete all entries for this observable | 
|---|
| 216 | callees_t *callees = callTable[this]; | 
|---|
| 217 | callees_t::iterator iter; | 
|---|
| 218 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 219 | (*iter).second->subjectKilled(this); | 
|---|
| 220 | } | 
|---|
| 221 | callTable.erase(this); | 
|---|
| 222 | delete callees; | 
|---|
| 223 | } | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | /** Constructor for class Observer. | 
|---|
| 227 | */ | 
|---|
| 228 | Observer::Observer() | 
|---|
| 229 | {} | 
|---|
| 230 |  | 
|---|
| 231 | /** Destructor for class Observer. | 
|---|
| 232 | */ | 
|---|
| 233 | Observer::~Observer() | 
|---|
| 234 | {} | 
|---|