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