| 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 | #include <cassert> | 
|---|
| 13 |  | 
|---|
| 14 | using namespace std; | 
|---|
| 15 |  | 
|---|
| 16 | /****************** Static stuff for the observer mechanism ************/ | 
|---|
| 17 |  | 
|---|
| 18 | // All infrastructure for the observer-pattern is bundled at a central place | 
|---|
| 19 | // this is more efficient if many objects can be observed (inherit from observable) | 
|---|
| 20 | // but only few are actually coupled with observers. E.g. TMV has over 500.000 Atoms, | 
|---|
| 21 | // which might become observable. Handling Observerable infrastructure in each of | 
|---|
| 22 | // these would use memory for each atom. By handling Observer-infrastructure | 
|---|
| 23 | // here we only need memory for objects that actually are observed. | 
|---|
| 24 | // See [Gamma et al, 1995] p. 297 | 
|---|
| 25 |  | 
|---|
| 26 | map<Observable*, int> Observable::depth;  //!< Map of Observables to the depth of the DAG of Observers | 
|---|
| 27 | map<Observable*,multimap<int,Observer*>*> Observable::callTable; //!< Table for each Observable of all its Observers | 
|---|
| 28 | set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers | 
|---|
| 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 | */ | 
|---|
| 39 | void Observable::start_observer_internal(Observable *publisher){ | 
|---|
| 40 | // increase the count for this observable by one | 
|---|
| 41 | // if no entry for this observable is found, an new one is created | 
|---|
| 42 | // by the STL and initialized to 0 (see STL documentation) | 
|---|
| 43 | depth[publisher]++; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | /** Detaching Sub-observables from Observables. | 
|---|
| 47 | * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we | 
|---|
| 48 | * 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 directly | 
|---|
| 52 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop | 
|---|
| 53 | * thus producing compiler-errors whenever only one is used. | 
|---|
| 54 | * \param *publisher reference of sub-observable | 
|---|
| 55 | */ | 
|---|
| 56 | void Observable::finish_observer_internal(Observable *publisher){ | 
|---|
| 57 | // decrease the count for this observable | 
|---|
| 58 | // if zero is reached all observed blocks are done and we can | 
|---|
| 59 | // start to notify our observers | 
|---|
| 60 | if(--(depth[publisher])){} | 
|---|
| 61 | else{ | 
|---|
| 62 | publisher->notifyAll(); | 
|---|
| 63 | // this item is done, so we don't have to keep the count with us | 
|---|
| 64 | // save some memory by erasing it | 
|---|
| 65 | depth.erase(publisher); | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | /** Constructor for Observable Protector. | 
|---|
| 70 | * Basically, calls start_observer_internal(). Hence use this class instead of | 
|---|
| 71 | * calling the function directly. | 
|---|
| 72 | * | 
|---|
| 73 | * \param *protege Observable to be protected. | 
|---|
| 74 | */ | 
|---|
| 75 | Observable::_Observable_protector::_Observable_protector(Observable *_protege) : | 
|---|
| 76 | protege(_protege) | 
|---|
| 77 | { | 
|---|
| 78 | start_observer_internal(protege); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) : | 
|---|
| 82 | protege(dest.protege) | 
|---|
| 83 | { | 
|---|
| 84 | start_observer_internal(protege); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** Destructor for Observable Protector. | 
|---|
| 88 | * Basically, calls finish_observer_internal(). Hence use this class instead of | 
|---|
| 89 | * calling the function directly. | 
|---|
| 90 | * | 
|---|
| 91 | * \param *protege Observable to be protected. | 
|---|
| 92 | */ | 
|---|
| 93 | Observable::_Observable_protector::~_Observable_protector() | 
|---|
| 94 | { | 
|---|
| 95 | finish_observer_internal(protege); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /************* Notification mechanism for observables **************/ | 
|---|
| 99 |  | 
|---|
| 100 | /** Notify all Observers of changes. | 
|---|
| 101 | * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t | 
|---|
| 102 | * and removes from busy list. | 
|---|
| 103 | */ | 
|---|
| 104 | void Observable::notifyAll() { | 
|---|
| 105 | // we are busy notifying others right now | 
|---|
| 106 | // add ourselves to the list of busy subjects to enable circle detection | 
|---|
| 107 | busyObservables.insert(this); | 
|---|
| 108 | // see if anyone has signed up for observation | 
|---|
| 109 | // and call all observers | 
|---|
| 110 | if(callTable.count(this)) { | 
|---|
| 111 | // elements are stored sorted by keys in the multimap | 
|---|
| 112 | // so iterating over it gives us a the callees sorted by | 
|---|
| 113 | // the priorities | 
|---|
| 114 | callees_t *callees = callTable[this]; | 
|---|
| 115 | callees_t::iterator iter; | 
|---|
| 116 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 117 | (*iter).second->update(this); | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 | // done with notification, we can leave the set of busy subjects | 
|---|
| 121 | busyObservables.erase(this); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /** Handles passing on updates from sub-Observables. | 
|---|
| 125 | * Mimicks basically the Observer::update() function. | 
|---|
| 126 | * | 
|---|
| 127 | * \param *publisher The \a *this we observe. | 
|---|
| 128 | */ | 
|---|
| 129 | void Observable::update(Observable *publisher) { | 
|---|
| 130 | // circle detection | 
|---|
| 131 | if(busyObservables.find(this)!=busyObservables.end()) { | 
|---|
| 132 | // somehow a circle was introduced... we were busy notifying our | 
|---|
| 133 | // observers, but still we are called by one of our sub-Observables | 
|---|
| 134 | // we cannot be sure observation will still work at this point | 
|---|
| 135 | cerr << "Circle detected in observation-graph." << endl; | 
|---|
| 136 | cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl; | 
|---|
| 137 | cerr << "Please check your observation code and fix this!" << endl; | 
|---|
| 138 | return; | 
|---|
| 139 | } | 
|---|
| 140 | else { | 
|---|
| 141 | // see if we are in the process of changing ourselves | 
|---|
| 142 | // if we are changing ourselves at the same time our sub-observables change | 
|---|
| 143 | // we do not need to publish all the changes at each time we are called | 
|---|
| 144 | if(depth.find(this)==depth.end()) { | 
|---|
| 145 | notifyAll(); | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** Sign on an Observer to this Observable. | 
|---|
| 151 | * Puts \a *target into Observable::callTable list. | 
|---|
| 152 | * \param *target Observer | 
|---|
| 153 | * \param priority number in [-20,20] | 
|---|
| 154 | */ | 
|---|
| 155 | void Observable::signOn(Observer *target,int priority) { | 
|---|
| 156 | assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]"); | 
|---|
| 157 | bool res = false; | 
|---|
| 158 | callees_t *callees = 0; | 
|---|
| 159 | if(callTable.count(this)){ | 
|---|
| 160 | callees = callTable[this]; | 
|---|
| 161 | } | 
|---|
| 162 | else { | 
|---|
| 163 | callees = new multimap<int,Observer*>; | 
|---|
| 164 | callTable.insert(pair<Observable*,callees_t*>(this,callees)); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | callees_t::iterator iter; | 
|---|
| 168 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 169 | res |= ((*iter).second == target); | 
|---|
| 170 | } | 
|---|
| 171 | if(!res) | 
|---|
| 172 | callees->insert(pair<int,Observer*>(priority,target)); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | /** Sign off an Observer from this Observable. | 
|---|
| 176 | * Removes \a *target from Observable::callTable list. | 
|---|
| 177 | * \param *target Observer | 
|---|
| 178 | */ | 
|---|
| 179 | void Observable::signOff(Observer *target) { | 
|---|
| 180 | assert(callTable.count(this) && "SignOff called for an Observable without Observers."); | 
|---|
| 181 | callees_t *callees = callTable[this]; | 
|---|
| 182 | callees_t::iterator iter; | 
|---|
| 183 | callees_t::iterator deliter; | 
|---|
| 184 | for(iter=callees->begin();iter!=callees->end();) { | 
|---|
| 185 | if((*iter).second == target) { | 
|---|
| 186 | callees->erase(iter++); | 
|---|
| 187 | } | 
|---|
| 188 | else { | 
|---|
| 189 | ++iter; | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 | if(callees->empty()){ | 
|---|
| 193 | callTable.erase(this); | 
|---|
| 194 | delete callees; | 
|---|
| 195 | } | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | /** Handles sub-observables that just got killed | 
|---|
| 199 | *  when an sub-observerable dies we usually don't need to do anything | 
|---|
| 200 | *  \param *publisher Sub-Observable. | 
|---|
| 201 | */ | 
|---|
| 202 | void Observable::subjectKilled(Observable *publisher){ | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | /** Constructor for class Observable. | 
|---|
| 206 | */ | 
|---|
| 207 | Observable::Observable() | 
|---|
| 208 | {} | 
|---|
| 209 |  | 
|---|
| 210 | /** Destructor for class Observable. | 
|---|
| 211 | * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled(). | 
|---|
| 212 | */ | 
|---|
| 213 | Observable::~Observable() | 
|---|
| 214 | { | 
|---|
| 215 | if(callTable.count(this)) { | 
|---|
| 216 | // delete all entries for this observable | 
|---|
| 217 | callees_t *callees = callTable[this]; | 
|---|
| 218 | callees_t::iterator iter; | 
|---|
| 219 | for(iter=callees->begin();iter!=callees->end();++iter){ | 
|---|
| 220 | (*iter).second->subjectKilled(this); | 
|---|
| 221 | } | 
|---|
| 222 | callTable.erase(this); | 
|---|
| 223 | delete callees; | 
|---|
| 224 | } | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | /** Constructor for class Observer. | 
|---|
| 228 | */ | 
|---|
| 229 | Observer::Observer() | 
|---|
| 230 | {} | 
|---|
| 231 |  | 
|---|
| 232 | /** Destructor for class Observer. | 
|---|
| 233 | */ | 
|---|
| 234 | Observer::~Observer() | 
|---|
| 235 | {} | 
|---|