[63c1f6] | 1 | /*
|
---|
| 2 | * Observer.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jan 19, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[112b09] | 13 | #include "Helpers/MemDebug.hpp"
|
---|
| 14 |
|
---|
[63c1f6] | 15 | #include "Observer.hpp"
|
---|
| 16 |
|
---|
| 17 | #include <iostream>
|
---|
[4fb5a3] | 18 |
|
---|
| 19 | #include "Helpers/Assert.hpp"
|
---|
[cd5047] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
[63c1f6] | 21 |
|
---|
| 22 | using namespace std;
|
---|
| 23 |
|
---|
| 24 | /****************** Static stuff for the observer mechanism ************/
|
---|
| 25 |
|
---|
| 26 | // All infrastructure for the observer-pattern is bundled at a central place
|
---|
| 27 | // this is more efficient if many objects can be observed (inherit from observable)
|
---|
| 28 | // but only few are actually coupled with observers. E.g. TMV has over 500.000 Atoms,
|
---|
| 29 | // which might become observable. Handling Observerable infrastructure in each of
|
---|
| 30 | // these would use memory for each atom. By handling Observer-infrastructure
|
---|
| 31 | // here we only need memory for objects that actually are observed.
|
---|
| 32 | // See [Gamma et al, 1995] p. 297
|
---|
| 33 |
|
---|
[c296c2] | 34 | map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers
|
---|
[033a05] | 35 | map<Observable*,multimap<int,Observer*> > Observable::callTable; //!< Table for each Observable of all its Observers
|
---|
[ccacba] | 36 | std::map<Observable*,std::set<Notification*> > Observable::notifications;
|
---|
[c296c2] | 37 | set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers
|
---|
[63c1f6] | 38 |
|
---|
[c296c2] | 39 | /** Attaching Sub-observables to Observables.
|
---|
| 40 | * Increases entry in Observable::depth for this \a *publisher by one.
|
---|
| 41 | *
|
---|
| 42 | * The two functions \sa start_observer_internal() and \sa finish_observer_internal()
|
---|
| 43 | * have to be used together at all time. Never use these functions directly
|
---|
| 44 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
|
---|
| 45 | * thus producing compiler-errors whenever only one is used.
|
---|
| 46 | * \param *publisher reference of sub-observable
|
---|
| 47 | */
|
---|
[63c1f6] | 48 | void Observable::start_observer_internal(Observable *publisher){
|
---|
| 49 | // increase the count for this observable by one
|
---|
| 50 | // if no entry for this observable is found, an new one is created
|
---|
| 51 | // by the STL and initialized to 0 (see STL documentation)
|
---|
[cd5047] | 52 | #ifdef LOG_OBSERVER
|
---|
| 53 | observerLog().addMessage(depth[publisher]) << ">> Locking " << observerLog().getName(publisher) << endl;
|
---|
| 54 | #endif
|
---|
[63c1f6] | 55 | depth[publisher]++;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[c296c2] | 58 | /** Detaching Sub-observables from Observables.
|
---|
| 59 | * Decreases entry in Observable::depth for this \a *publisher by one. If zero, we
|
---|
| 60 | * start notifying all our Observers.
|
---|
| 61 | *
|
---|
| 62 | * The two functions start_observer_internal() and finish_observer_internal()
|
---|
| 63 | * have to be used together at all time. Never use these functions directly
|
---|
| 64 | * START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
|
---|
| 65 | * thus producing compiler-errors whenever only one is used.
|
---|
| 66 | * \param *publisher reference of sub-observable
|
---|
| 67 | */
|
---|
[63c1f6] | 68 | void Observable::finish_observer_internal(Observable *publisher){
|
---|
| 69 | // decrease the count for this observable
|
---|
| 70 | // if zero is reached all observed blocks are done and we can
|
---|
| 71 | // start to notify our observers
|
---|
[cd5047] | 72 | --depth[publisher];
|
---|
| 73 | #ifdef LOG_OBSERVER
|
---|
| 74 | observerLog().addMessage(depth[publisher]) << "<< Unlocking " << observerLog().getName(publisher) << endl;
|
---|
| 75 | #endif
|
---|
| 76 | if(depth[publisher]){}
|
---|
[63c1f6] | 77 | else{
|
---|
| 78 | publisher->notifyAll();
|
---|
| 79 | // this item is done, so we don't have to keep the count with us
|
---|
| 80 | // save some memory by erasing it
|
---|
| 81 | depth.erase(publisher);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[ccacba] | 85 | void Observable::enque_notification_internal(Observable *publisher, Notification_ptr notification){
|
---|
| 86 | ASSERT(notification->owner==publisher,"Some object tried to send a notification it does not own");
|
---|
| 87 | notifications[publisher].insert(notification);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[c296c2] | 90 | /** Constructor for Observable Protector.
|
---|
| 91 | * Basically, calls start_observer_internal(). Hence use this class instead of
|
---|
| 92 | * calling the function directly.
|
---|
| 93 | *
|
---|
| 94 | * \param *protege Observable to be protected.
|
---|
| 95 | */
|
---|
[317df8] | 96 | Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
|
---|
| 97 | protege(_protege)
|
---|
| 98 | {
|
---|
| 99 | start_observer_internal(protege);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[bd58fb] | 102 | Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) :
|
---|
| 103 | protege(dest.protege)
|
---|
| 104 | {
|
---|
| 105 | start_observer_internal(protege);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[c296c2] | 108 | /** Destructor for Observable Protector.
|
---|
| 109 | * Basically, calls finish_observer_internal(). Hence use this class instead of
|
---|
| 110 | * calling the function directly.
|
---|
| 111 | *
|
---|
| 112 | * \param *protege Observable to be protected.
|
---|
| 113 | */
|
---|
[317df8] | 114 | Observable::_Observable_protector::~_Observable_protector()
|
---|
| 115 | {
|
---|
| 116 | finish_observer_internal(protege);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[63c1f6] | 119 | /************* Notification mechanism for observables **************/
|
---|
| 120 |
|
---|
[c296c2] | 121 | /** Notify all Observers of changes.
|
---|
| 122 | * Puts \a *this into Observable::busyObservables, calls Observer::update() for all in callee_t
|
---|
| 123 | * and removes from busy list.
|
---|
| 124 | */
|
---|
[033a05] | 125 | void Observable::notifyAll() {
|
---|
[63c1f6] | 126 | // we are busy notifying others right now
|
---|
| 127 | // add ourselves to the list of busy subjects to enable circle detection
|
---|
| 128 | busyObservables.insert(this);
|
---|
| 129 | // see if anyone has signed up for observation
|
---|
| 130 | // and call all observers
|
---|
[033a05] | 131 | try {
|
---|
| 132 | if(callTable.count(this)) {
|
---|
| 133 | // elements are stored sorted by keys in the multimap
|
---|
| 134 | // so iterating over it gives us a the callees sorted by
|
---|
| 135 | // the priorities
|
---|
| 136 | callees_t callees = callTable[this];
|
---|
| 137 | callees_t::iterator iter;
|
---|
| 138 | for(iter=callees.begin();iter!=callees.end();++iter){
|
---|
[cd5047] | 139 | #ifdef LOG_OBSERVER
|
---|
| 140 | observerLog().addMessage() << "-> Sending update from " << observerLog().getName(this)
|
---|
| 141 | << " to " << observerLog().getName((*iter).second)
|
---|
| 142 | << " (priority=" << (*iter).first << ")"<< endl;
|
---|
| 143 | #endif
|
---|
[033a05] | 144 | (*iter).second->update(this);
|
---|
| 145 | }
|
---|
[0c1d97] | 146 | }
|
---|
[63c1f6] | 147 | }
|
---|
[033a05] | 148 | ASSERT_NOCATCH("Exception thrown from Observer Update");
|
---|
[ccacba] | 149 |
|
---|
| 150 | // send out all notifications that need to be done
|
---|
| 151 |
|
---|
| 152 | notificationSet currentNotifications = notifications[this];
|
---|
| 153 | for(notificationSet::iterator it = currentNotifications.begin();
|
---|
| 154 | it != currentNotifications.end();++it){
|
---|
| 155 | (*it)->notifyAll();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | notifications.erase(this);
|
---|
| 159 |
|
---|
| 160 | // done with notification, we can leave the set of busy subjects
|
---|
[63c1f6] | 161 | busyObservables.erase(this);
|
---|
| 162 | }
|
---|
[033a05] | 163 |
|
---|
[63c1f6] | 164 |
|
---|
[c296c2] | 165 | /** Handles passing on updates from sub-Observables.
|
---|
| 166 | * Mimicks basically the Observer::update() function.
|
---|
| 167 | *
|
---|
| 168 | * \param *publisher The \a *this we observe.
|
---|
| 169 | */
|
---|
[63c1f6] | 170 | void Observable::update(Observable *publisher) {
|
---|
| 171 | // circle detection
|
---|
| 172 | if(busyObservables.find(this)!=busyObservables.end()) {
|
---|
| 173 | // somehow a circle was introduced... we were busy notifying our
|
---|
| 174 | // observers, but still we are called by one of our sub-Observables
|
---|
| 175 | // we cannot be sure observation will still work at this point
|
---|
[4fb5a3] | 176 | ASSERT(0,"Circle detected in observation-graph.\n"
|
---|
| 177 | "Observation-graph always needs to be a DAG to work correctly!\n"
|
---|
| 178 | "Please check your observation code and fix this!\n");
|
---|
[63c1f6] | 179 | return;
|
---|
| 180 | }
|
---|
| 181 | else {
|
---|
| 182 | // see if we are in the process of changing ourselves
|
---|
| 183 | // if we are changing ourselves at the same time our sub-observables change
|
---|
| 184 | // we do not need to publish all the changes at each time we are called
|
---|
| 185 | if(depth.find(this)==depth.end()) {
|
---|
[cd5047] | 186 | #ifdef LOG_OBSERVER
|
---|
| 187 | observerLog().addMessage() << "-* Update from " << observerLog().getName(publisher)
|
---|
| 188 | << " propagated by " << observerLog().getName(this) << endl;
|
---|
| 189 | #endif
|
---|
[63c1f6] | 190 | notifyAll();
|
---|
| 191 | }
|
---|
[cd5047] | 192 | else{
|
---|
| 193 | #ifdef LOG_OBSERVER
|
---|
| 194 | observerLog().addMessage() << "-| Update from " << observerLog().getName(publisher)
|
---|
| 195 | << " not propagated by " << observerLog().getName(this) << endl;
|
---|
| 196 | #endif
|
---|
| 197 | }
|
---|
[63c1f6] | 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[c296c2] | 201 | /** Sign on an Observer to this Observable.
|
---|
| 202 | * Puts \a *target into Observable::callTable list.
|
---|
| 203 | * \param *target Observer
|
---|
| 204 | * \param priority number in [-20,20]
|
---|
| 205 | */
|
---|
[0c1d97] | 206 | void Observable::signOn(Observer *target,int priority) {
|
---|
[4fb5a3] | 207 | ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
|
---|
[cd5047] | 208 | #ifdef LOG_OBSERVER
|
---|
| 209 | observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(this) << endl;
|
---|
| 210 | #endif
|
---|
[63c1f6] | 211 | bool res = false;
|
---|
[033a05] | 212 | callees_t &callees = callTable[this];
|
---|
[0c1d97] | 213 |
|
---|
| 214 | callees_t::iterator iter;
|
---|
[033a05] | 215 | for(iter=callees.begin();iter!=callees.end();++iter){
|
---|
[63c1f6] | 216 | res |= ((*iter).second == target);
|
---|
| 217 | }
|
---|
| 218 | if(!res)
|
---|
[033a05] | 219 | callees.insert(pair<int,Observer*>(priority,target));
|
---|
[63c1f6] | 220 | }
|
---|
| 221 |
|
---|
[c296c2] | 222 | /** Sign off an Observer from this Observable.
|
---|
| 223 | * Removes \a *target from Observable::callTable list.
|
---|
| 224 | * \param *target Observer
|
---|
| 225 | */
|
---|
[63c1f6] | 226 | void Observable::signOff(Observer *target) {
|
---|
[4fb5a3] | 227 | ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
|
---|
[cd5047] | 228 | #ifdef LOG_OBSERVER
|
---|
| 229 | observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(this) << endl;
|
---|
| 230 | #endif
|
---|
[033a05] | 231 | callees_t &callees = callTable[this];
|
---|
[a7b761b] | 232 |
|
---|
[0c1d97] | 233 | callees_t::iterator iter;
|
---|
[c296c2] | 234 | callees_t::iterator deliter;
|
---|
[033a05] | 235 | for(iter=callees.begin();iter!=callees.end();) {
|
---|
[cf1a07] | 236 | if((*iter).second == target) {
|
---|
[033a05] | 237 | callees.erase(iter++);
|
---|
[cf1a07] | 238 | }
|
---|
| 239 | else {
|
---|
| 240 | ++iter;
|
---|
| 241 | }
|
---|
[0c1d97] | 242 | }
|
---|
[033a05] | 243 | if(callees.empty()){
|
---|
[0c1d97] | 244 | callTable.erase(this);
|
---|
[63c1f6] | 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[ccacba] | 248 | void Observable::signOn(Observer *target, Notification_ptr notification){
|
---|
| 249 | ASSERT(notification->owner==this,
|
---|
| 250 | "Trying to sign on for a notification that is not provided by this object");
|
---|
| 251 |
|
---|
| 252 | notification->addObserver(target);
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | void Observable::signOff(Observer *target, Notification_ptr notification){
|
---|
| 256 | ASSERT(notification->owner==this,
|
---|
| 257 | "Trying to sign off from a notification that is not provided by this object");
|
---|
| 258 |
|
---|
| 259 | notification->removeObserver(target);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[4fb5a3] | 262 | bool Observable::isBlocked(){
|
---|
| 263 | return depth.count(this) > 0;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[c296c2] | 266 | /** Handles sub-observables that just got killed
|
---|
| 267 | * when an sub-observerable dies we usually don't need to do anything
|
---|
| 268 | * \param *publisher Sub-Observable.
|
---|
| 269 | */
|
---|
[63c1f6] | 270 | void Observable::subjectKilled(Observable *publisher){
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[c296c2] | 273 | /** Constructor for class Observable.
|
---|
| 274 | */
|
---|
[cd5047] | 275 | Observable::Observable(string name) :
|
---|
| 276 | Observer(Observer::BaseConstructor())
|
---|
| 277 | {
|
---|
| 278 | #ifdef LOG_OBSERVER
|
---|
| 279 | observerLog().addName(this,name);
|
---|
| 280 | observerLog().addMessage() << "++ Creating Observable " << observerLog().getName(this) << endl;
|
---|
| 281 | #endif
|
---|
| 282 | }
|
---|
[63c1f6] | 283 |
|
---|
[c296c2] | 284 | /** Destructor for class Observable.
|
---|
| 285 | * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled().
|
---|
| 286 | */
|
---|
[63c1f6] | 287 | Observable::~Observable()
|
---|
| 288 | {
|
---|
[cd5047] | 289 | #ifdef LOG_OBSERVER
|
---|
| 290 | observerLog().addMessage() << "-- Destroying Observable " << observerLog().getName(this) << endl;
|
---|
| 291 | #endif
|
---|
[0c1d97] | 292 | if(callTable.count(this)) {
|
---|
| 293 | // delete all entries for this observable
|
---|
[033a05] | 294 | callees_t callees = callTable[this];
|
---|
[0c1d97] | 295 | callees_t::iterator iter;
|
---|
[033a05] | 296 | for(iter=callees.begin();iter!=callees.end();++iter){
|
---|
[0c1d97] | 297 | (*iter).second->subjectKilled(this);
|
---|
| 298 | }
|
---|
| 299 | callTable.erase(this);
|
---|
[63c1f6] | 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[c296c2] | 303 | /** Constructor for class Observer.
|
---|
| 304 | */
|
---|
[cd5047] | 305 | Observer::Observer(string name)
|
---|
| 306 | {
|
---|
| 307 | #ifdef LOG_OBSERVER
|
---|
| 308 | observerLog().addName(this,name);
|
---|
| 309 | observerLog().addMessage() << "++ Creating Observer " << observerLog().getName(this) << endl;
|
---|
| 310 | #endif
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | /**
|
---|
| 314 | * Base Constructor for class Observer
|
---|
| 315 | *
|
---|
| 316 | * only called from Observable Constructor
|
---|
| 317 | */
|
---|
| 318 | Observer::Observer(Observer::BaseConstructor){
|
---|
| 319 | #ifdef LOG_OBSERVER
|
---|
| 320 | observerLog().addObservable(this);
|
---|
| 321 | #endif
|
---|
| 322 | }
|
---|
[63c1f6] | 323 |
|
---|
[c296c2] | 324 | /** Destructor for class Observer.
|
---|
| 325 | */
|
---|
[63c1f6] | 326 | Observer::~Observer()
|
---|
[cd5047] | 327 | {
|
---|
| 328 | #ifdef LOG_OBSERVER
|
---|
| 329 | if(!observerLog().isObservable(this)){
|
---|
| 330 | observerLog().addMessage() << "-- Destroying Observer " << observerLog().getName(this) << endl;
|
---|
| 331 | }
|
---|
| 332 | #endif
|
---|
| 333 | }
|
---|
[ccacba] | 334 |
|
---|
| 335 | /**
|
---|
| 336 | * Method for specialized notifications.
|
---|
| 337 | * Most Observers wont need or use this, so it is implemented
|
---|
| 338 | * empty in the base case;
|
---|
| 339 | */
|
---|
| 340 | void Observer::recieveNotification(Observable *publisher, Notification_ptr notification){
|
---|
[033a05] | 341 | ASSERT(0,"Notification received by object that did not sign on for it.");
|
---|
[ccacba] | 342 | }
|
---|
| 343 |
|
---|
| 344 | Notification::Notification(Observable *_owner) :
|
---|
| 345 | owner(_owner)
|
---|
| 346 | {}
|
---|
| 347 |
|
---|
| 348 | Notification::~Notification(){}
|
---|
| 349 |
|
---|
| 350 | void Notification::addObserver(Observer *target){
|
---|
| 351 | targets.insert(target);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | void Notification::removeObserver(Observer *target){
|
---|
| 355 | targets.erase(target);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | void Notification::notifyAll(){
|
---|
| 359 | for(std::set<Observer*>::iterator it=targets.begin();
|
---|
| 360 | it!=targets.end();++it){
|
---|
| 361 | (*it)->recieveNotification(owner,this);
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
[cd5047] | 364 |
|
---|
| 365 | #ifdef LOG_OBSERVER
|
---|
| 366 |
|
---|
| 367 | /************************* Methods to do logging of the Observer Mechanism *********/
|
---|
| 368 |
|
---|
| 369 | // The log needs to exist fairly early, so we make it construct on first use,
|
---|
| 370 | // and never destroy it
|
---|
| 371 | ObserverLog &observerLog(){
|
---|
| 372 | // yes, this memory is never freed... we need it around for the whole programm,
|
---|
| 373 | // so no freeing is possible
|
---|
| 374 | static ObserverLog *theLog = Memory::ignore(new ObserverLog());
|
---|
| 375 | return *theLog;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
| 379 | ObserverLog::ObserverLog() :
|
---|
| 380 | count (0)
|
---|
| 381 | {}
|
---|
| 382 |
|
---|
| 383 | ObserverLog::~ObserverLog(){}
|
---|
| 384 |
|
---|
| 385 | string ObserverLog::getLog(){return log.str();}
|
---|
| 386 |
|
---|
| 387 | std::string ObserverLog::getName(void* obj){
|
---|
| 388 | return names[obj];
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | bool ObserverLog::isObservable(void* obj){
|
---|
| 392 | return observables.count(obj);
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | void ObserverLog::addName(void* obj , string name){
|
---|
| 396 | stringstream sstr;
|
---|
| 397 | sstr << name << "_" << count++;
|
---|
| 398 | names[obj] = sstr.str();
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | void ObserverLog::addObservable(void* obj){
|
---|
| 402 | observables.insert(obj);
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | void ObserverLog::deleteName(void* obj){
|
---|
| 406 | names.erase(obj);
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | void ObserverLog::deleteObservable(void* obj){
|
---|
| 410 | observables.erase(obj);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | stringstream &ObserverLog::addMessage(int depth){
|
---|
| 414 | for(int i=depth;i--;)
|
---|
| 415 | log << " ";
|
---|
| 416 | return log;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | #endif
|
---|