| 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 | std::map<Observable*,std::set<Notification*> > Observable::notifications; | 
|---|
| 30 | set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers | 
|---|
| 31 |  | 
|---|
| 32 | /** Attaching Sub-observables to Observables. | 
|---|
| 33 | * Increases entry in Observable::depth for this \a *publisher by one. | 
|---|
| 34 | * | 
|---|
| 35 | * The two functions \sa start_observer_internal() and \sa finish_observer_internal() | 
|---|
| 36 | * have to be used together at all time. Never use these functions directly | 
|---|
| 37 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop | 
|---|
| 38 | * thus producing compiler-errors whenever only one is used. | 
|---|
| 39 | * \param *publisher reference of sub-observable | 
|---|
| 40 | */ | 
|---|
| 41 | void Observable::start_observer_internal(Observable *publisher){ | 
|---|
| 42 | // increase the count for this observable by one | 
|---|
| 43 | // if no entry for this observable is found, an new one is created | 
|---|
| 44 | // by the STL and initialized to 0 (see STL documentation) | 
|---|
| 45 | depth[publisher]++; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /** Detaching Sub-observables from Observables. | 
|---|
| 49 | * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we | 
|---|
| 50 | * start notifying all our Observers. | 
|---|
| 51 | * | 
|---|
| 52 | * The two functions start_observer_internal() and finish_observer_internal() | 
|---|
| 53 | * have to be used together at all time. Never use these functions directly | 
|---|
| 54 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop | 
|---|
| 55 | * thus producing compiler-errors whenever only one is used. | 
|---|
| 56 | * \param *publisher reference of sub-observable | 
|---|
| 57 | */ | 
|---|
| 58 | void Observable::finish_observer_internal(Observable *publisher){ | 
|---|
| 59 | // decrease the count for this observable | 
|---|
| 60 | // if zero is reached all observed blocks are done and we can | 
|---|
| 61 | // start to notify our observers | 
|---|
| 62 | if(--(depth[publisher])){} | 
|---|
| 63 | else{ | 
|---|
| 64 | publisher->notifyAll(); | 
|---|
| 65 | // this item is done, so we don't have to keep the count with us | 
|---|
| 66 | // save some memory by erasing it | 
|---|
| 67 | depth.erase(publisher); | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | void Observable::enque_notification_internal(Observable *publisher, Notification_ptr notification){ | 
|---|
| 72 | ASSERT(notification->owner==publisher,"Some object tried to send a notification it does not own"); | 
|---|
| 73 | notifications[publisher].insert(notification); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | /** Constructor for Observable Protector. | 
|---|
| 77 | * Basically, calls start_observer_internal(). Hence use this class instead of | 
|---|
| 78 | * calling the function directly. | 
|---|
| 79 | * | 
|---|
| 80 | * \param *protege Observable to be protected. | 
|---|
| 81 | */ | 
|---|
| 82 | Observable::_Observable_protector::_Observable_protector(Observable *_protege) : | 
|---|
| 83 | protege(_protege) | 
|---|
| 84 | { | 
|---|
| 85 | start_observer_internal(protege); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) : | 
|---|
| 89 | protege(dest.protege) | 
|---|
| 90 | { | 
|---|
| 91 | start_observer_internal(protege); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | /** Destructor for Observable Protector. | 
|---|
| 95 | * Basically, calls finish_observer_internal(). Hence use this class instead of | 
|---|
| 96 | * calling the function directly. | 
|---|
| 97 | * | 
|---|
| 98 | * \param *protege Observable to be protected. | 
|---|
| 99 | */ | 
|---|
| 100 | Observable::_Observable_protector::~_Observable_protector() | 
|---|
| 101 | { | 
|---|
| 102 | finish_observer_internal(protege); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /************* Notification mechanism for observables **************/ | 
|---|
| 106 |  | 
|---|
| 107 | /** Notify all Observers of changes. | 
|---|
| 108 | * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t | 
|---|
| 109 | * and removes from busy list. | 
|---|
| 110 | */ | 
|---|
| 111 | void Observable::notifyAll() { | 
|---|
| 112 | // we are busy notifying others right now | 
|---|
| 113 | // add ourselves to the list of busy subjects to enable circle detection | 
|---|
| 114 | busyObservables.insert(this); | 
|---|
| 115 | // see if anyone has signed up for observation | 
|---|
| 116 | // and call all observers | 
|---|
| 117 | try { | 
|---|
| 118 | if(callTable.count(this)) { | 
|---|
| 119 | // elements are stored sorted by keys in the multimap | 
|---|
| 120 | // so iterating over it gives us a the callees sorted by | 
|---|
| 121 | // the priorities | 
|---|
| 122 | callees_t callees = callTable[this]; | 
|---|
| 123 | callees_t::iterator iter; | 
|---|
| 124 | for(iter=callees.begin();iter!=callees.end();++iter){ | 
|---|
| 125 | (*iter).second->update(this); | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 | } | 
|---|
| 129 | ASSERT_NOCATCH("Exception thrown from Observer Update"); | 
|---|
| 130 |  | 
|---|
| 131 | // send out all notifications that need to be done | 
|---|
| 132 |  | 
|---|
| 133 | notificationSet currentNotifications = notifications[this]; | 
|---|
| 134 | for(notificationSet::iterator it = currentNotifications.begin(); | 
|---|
| 135 | it != currentNotifications.end();++it){ | 
|---|
| 136 | (*it)->notifyAll(); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | notifications.erase(this); | 
|---|
| 140 |  | 
|---|
| 141 | // done with notification, we can leave the set of busy subjects | 
|---|
| 142 | busyObservables.erase(this); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | /** Handles passing on updates from sub-Observables. | 
|---|
| 147 | * Mimicks basically the Observer::update() function. | 
|---|
| 148 | * | 
|---|
| 149 | * \param *publisher The \a *this we observe. | 
|---|
| 150 | */ | 
|---|
| 151 | void Observable::update(Observable *publisher) { | 
|---|
| 152 | // circle detection | 
|---|
| 153 | if(busyObservables.find(this)!=busyObservables.end()) { | 
|---|
| 154 | // somehow a circle was introduced... we were busy notifying our | 
|---|
| 155 | // observers, but still we are called by one of our sub-Observables | 
|---|
| 156 | // we cannot be sure observation will still work at this point | 
|---|
| 157 | ASSERT(0,"Circle detected in observation-graph.\n" | 
|---|
| 158 | "Observation-graph always needs to be a DAG to work correctly!\n" | 
|---|
| 159 | "Please check your observation code and fix this!\n"); | 
|---|
| 160 | return; | 
|---|
| 161 | } | 
|---|
| 162 | else { | 
|---|
| 163 | // see if we are in the process of changing ourselves | 
|---|
| 164 | // if we are changing ourselves at the same time our sub-observables change | 
|---|
| 165 | // we do not need to publish all the changes at each time we are called | 
|---|
| 166 | if(depth.find(this)==depth.end()) { | 
|---|
| 167 | notifyAll(); | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | /** Sign on an Observer to this Observable. | 
|---|
| 173 | * Puts \a *target into Observable::callTable list. | 
|---|
| 174 | * \param *target Observer | 
|---|
| 175 | * \param priority number in [-20,20] | 
|---|
| 176 | */ | 
|---|
| 177 | void Observable::signOn(Observer *target,int priority) { | 
|---|
| 178 | ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer"); | 
|---|
| 179 | bool res = false; | 
|---|
| 180 | callees_t &callees = callTable[this]; | 
|---|
| 181 |  | 
|---|
| 182 | callees_t::iterator iter; | 
|---|
| 183 | for(iter=callees.begin();iter!=callees.end();++iter){ | 
|---|
| 184 | res |= ((*iter).second == target); | 
|---|
| 185 | } | 
|---|
| 186 | if(!res) | 
|---|
| 187 | callees.insert(pair<int,Observer*>(priority,target)); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | /** Sign off an Observer from this Observable. | 
|---|
| 191 | * Removes \a *target from Observable::callTable list. | 
|---|
| 192 | * \param *target Observer | 
|---|
| 193 | */ | 
|---|
| 194 | void Observable::signOff(Observer *target) { | 
|---|
| 195 | ASSERT(callTable.count(this),"SignOff called for an Observable without Observers."); | 
|---|
| 196 | callees_t &callees = callTable[this]; | 
|---|
| 197 |  | 
|---|
| 198 | callees_t::iterator iter; | 
|---|
| 199 | callees_t::iterator deliter; | 
|---|
| 200 | for(iter=callees.begin();iter!=callees.end();) { | 
|---|
| 201 | if((*iter).second == target) { | 
|---|
| 202 | callees.erase(iter++); | 
|---|
| 203 | } | 
|---|
| 204 | else { | 
|---|
| 205 | ++iter; | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 | if(callees.empty()){ | 
|---|
| 209 | callTable.erase(this); | 
|---|
| 210 | } | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void Observable::signOn(Observer *target, Notification_ptr notification){ | 
|---|
| 214 | ASSERT(notification->owner==this, | 
|---|
| 215 | "Trying to sign on for a notification that is not provided by this object"); | 
|---|
| 216 |  | 
|---|
| 217 | notification->addObserver(target); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | void Observable::signOff(Observer *target, Notification_ptr notification){ | 
|---|
| 221 | ASSERT(notification->owner==this, | 
|---|
| 222 | "Trying to sign off from a notification that is not provided by this object"); | 
|---|
| 223 |  | 
|---|
| 224 | notification->removeObserver(target); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | bool Observable::isBlocked(){ | 
|---|
| 228 | return depth.count(this) > 0; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | /** Handles sub-observables that just got killed | 
|---|
| 232 | *  when an sub-observerable dies we usually don't need to do anything | 
|---|
| 233 | *  \param *publisher Sub-Observable. | 
|---|
| 234 | */ | 
|---|
| 235 | void Observable::subjectKilled(Observable *publisher){ | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | /** Constructor for class Observable. | 
|---|
| 239 | */ | 
|---|
| 240 | Observable::Observable() | 
|---|
| 241 | {} | 
|---|
| 242 |  | 
|---|
| 243 | /** Destructor for class Observable. | 
|---|
| 244 | * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled(). | 
|---|
| 245 | */ | 
|---|
| 246 | Observable::~Observable() | 
|---|
| 247 | { | 
|---|
| 248 | if(callTable.count(this)) { | 
|---|
| 249 | // delete all entries for this observable | 
|---|
| 250 | callees_t callees = callTable[this]; | 
|---|
| 251 | callees_t::iterator iter; | 
|---|
| 252 | for(iter=callees.begin();iter!=callees.end();++iter){ | 
|---|
| 253 | (*iter).second->subjectKilled(this); | 
|---|
| 254 | } | 
|---|
| 255 | callTable.erase(this); | 
|---|
| 256 | } | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | /** Constructor for class Observer. | 
|---|
| 260 | */ | 
|---|
| 261 | Observer::Observer() | 
|---|
| 262 | {} | 
|---|
| 263 |  | 
|---|
| 264 | /** Destructor for class Observer. | 
|---|
| 265 | */ | 
|---|
| 266 | Observer::~Observer() | 
|---|
| 267 | {} | 
|---|
| 268 |  | 
|---|
| 269 | /** | 
|---|
| 270 | * Method for specialized notifications. | 
|---|
| 271 | * Most Observers wont need or use this, so it is implemented | 
|---|
| 272 | * empty in the base case; | 
|---|
| 273 | */ | 
|---|
| 274 | void Observer::recieveNotification(Observable *publisher, Notification_ptr notification){ | 
|---|
| 275 | ASSERT(0,"Notification received by object that did not sign on for it."); | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | Notification::Notification(Observable *_owner) : | 
|---|
| 279 | owner(_owner) | 
|---|
| 280 | {} | 
|---|
| 281 |  | 
|---|
| 282 | Notification::~Notification(){} | 
|---|
| 283 |  | 
|---|
| 284 | void Notification::addObserver(Observer *target){ | 
|---|
| 285 | targets.insert(target); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | void Notification::removeObserver(Observer *target){ | 
|---|
| 289 | targets.erase(target); | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | void Notification::notifyAll(){ | 
|---|
| 293 | for(std::set<Observer*>::iterator it=targets.begin(); | 
|---|
| 294 | it!=targets.end();++it){ | 
|---|
| 295 | (*it)->recieveNotification(owner,this); | 
|---|
| 296 | } | 
|---|
| 297 | } | 
|---|