| [63c1f6] | 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> | 
|---|
| [0c1d97] | 12 | #include <cassert> | 
|---|
| [63c1f6] | 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; | 
|---|
| [0c1d97] | 27 | map<Observable*,multimap<int,Observer*>*> Observable::callTable; | 
|---|
| [63c1f6] | 28 | set<Observable*> Observable::busyObservables; | 
|---|
|  | 29 |  | 
|---|
|  | 30 | // The two functions start_observer_internal and finish_observer_internal | 
|---|
|  | 31 | // have to be used together at all time. Never use these functions directly | 
|---|
|  | 32 | // START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop | 
|---|
|  | 33 | // thus producing compiler-errors whenever only one is used | 
|---|
|  | 34 |  | 
|---|
|  | 35 | void Observable::start_observer_internal(Observable *publisher){ | 
|---|
|  | 36 | // increase the count for this observable by one | 
|---|
|  | 37 | // if no entry for this observable is found, an new one is created | 
|---|
|  | 38 | // by the STL and initialized to 0 (see STL documentation) | 
|---|
|  | 39 | depth[publisher]++; | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
|  | 42 | void Observable::finish_observer_internal(Observable *publisher){ | 
|---|
|  | 43 | // decrease the count for this observable | 
|---|
|  | 44 | // if zero is reached all observed blocks are done and we can | 
|---|
|  | 45 | // start to notify our observers | 
|---|
|  | 46 | if(--(depth[publisher])){} | 
|---|
|  | 47 | else{ | 
|---|
|  | 48 | publisher->notifyAll(); | 
|---|
|  | 49 | // this item is done, so we don't have to keep the count with us | 
|---|
|  | 50 | // save some memory by erasing it | 
|---|
|  | 51 | depth.erase(publisher); | 
|---|
|  | 52 | } | 
|---|
|  | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /************* Notification mechanism for observables **************/ | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
|  | 58 | void Observable::notifyAll() { | 
|---|
|  | 59 | // we are busy notifying others right now | 
|---|
|  | 60 | // add ourselves to the list of busy subjects to enable circle detection | 
|---|
|  | 61 | busyObservables.insert(this); | 
|---|
|  | 62 | // see if anyone has signed up for observation | 
|---|
|  | 63 | // and call all observers | 
|---|
|  | 64 | if(callTable.count(this)) { | 
|---|
| [0c1d97] | 65 | // elements are stored sorted by keys in the multimap | 
|---|
|  | 66 | // so iterating over it gives us a the callees sorted by | 
|---|
|  | 67 | // the priorities | 
|---|
|  | 68 | callees_t *callees = callTable[this]; | 
|---|
|  | 69 | callees_t::iterator iter; | 
|---|
|  | 70 | for(iter=callees->begin();iter!=callees->end();iter++){ | 
|---|
|  | 71 | (*iter).second->update(this); | 
|---|
|  | 72 | } | 
|---|
| [63c1f6] | 73 | } | 
|---|
|  | 74 | // done with notification, we can leave the set of busy subjects | 
|---|
|  | 75 | busyObservables.erase(this); | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
|  | 78 | // this handles passing on updates from sub-Observables | 
|---|
|  | 79 | void Observable::update(Observable *publisher) { | 
|---|
|  | 80 | // circle detection | 
|---|
|  | 81 | if(busyObservables.find(this)!=busyObservables.end()) { | 
|---|
|  | 82 | // somehow a circle was introduced... we were busy notifying our | 
|---|
|  | 83 | // observers, but still we are called by one of our sub-Observables | 
|---|
|  | 84 | // we cannot be sure observation will still work at this point | 
|---|
|  | 85 | cerr << "Circle detected in observation-graph." << endl; | 
|---|
|  | 86 | cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl; | 
|---|
|  | 87 | cerr << "Please check your observation code and fix this!" << endl; | 
|---|
|  | 88 | return; | 
|---|
|  | 89 | } | 
|---|
|  | 90 | else { | 
|---|
|  | 91 | // see if we are in the process of changing ourselves | 
|---|
|  | 92 | // if we are changing ourselves at the same time our sub-observables change | 
|---|
|  | 93 | // we do not need to publish all the changes at each time we are called | 
|---|
|  | 94 | if(depth.find(this)==depth.end()) { | 
|---|
|  | 95 | notifyAll(); | 
|---|
|  | 96 | } | 
|---|
|  | 97 | } | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | // methods to sign-on and off | 
|---|
| [0c1d97] | 101 | void Observable::signOn(Observer *target,int priority) { | 
|---|
|  | 102 | assert(priority>=-20 && priority<=+20 && "Priority out of range [-20:+20]"); | 
|---|
| [63c1f6] | 103 | bool res = false; | 
|---|
| [0c1d97] | 104 | callees_t *callees = 0; | 
|---|
|  | 105 | if(callTable.count(this)){ | 
|---|
|  | 106 | callees = callTable[this]; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | else { | 
|---|
|  | 109 | callees = new multimap<int,Observer*>; | 
|---|
|  | 110 | callTable.insert(pair<Observable*,callees_t*>(this,callees)); | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | callees_t::iterator iter; | 
|---|
|  | 114 | for(iter=callees->begin();iter!=callees->end();iter++){ | 
|---|
| [63c1f6] | 115 | res |= ((*iter).second == target); | 
|---|
|  | 116 | } | 
|---|
|  | 117 | if(!res) | 
|---|
| [0c1d97] | 118 | callees->insert(pair<int,Observer*>(priority,target)); | 
|---|
| [63c1f6] | 119 | } | 
|---|
|  | 120 |  | 
|---|
|  | 121 | void Observable::signOff(Observer *target) { | 
|---|
| [0c1d97] | 122 | assert(callTable.count(this) && "SignOff called for an Observable without Observers."); | 
|---|
|  | 123 | callees_t *callees = callTable[this]; | 
|---|
|  | 124 | callees_t::iterator iter; | 
|---|
|  | 125 | for(iter=callees->begin();iter!=callees->end();iter++) { | 
|---|
| [63c1f6] | 126 | if((*iter).second == target) | 
|---|
| [0c1d97] | 127 | callees->erase(iter); | 
|---|
|  | 128 | } | 
|---|
|  | 129 | if(callees->empty()){ | 
|---|
|  | 130 | callTable.erase(this); | 
|---|
|  | 131 | delete callees; | 
|---|
| [63c1f6] | 132 | } | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | // when an sub-observerable dies we usually don't need to do anything | 
|---|
|  | 136 | void Observable::subjectKilled(Observable *publisher){ | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
|  | 139 | Observable::Observable() | 
|---|
|  | 140 | {} | 
|---|
|  | 141 |  | 
|---|
|  | 142 | // when an observable is deleted, we let all our observers know | 
|---|
|  | 143 | Observable::~Observable() | 
|---|
|  | 144 | { | 
|---|
| [0c1d97] | 145 | if(callTable.count(this)) { | 
|---|
|  | 146 | // delete all entries for this observable | 
|---|
|  | 147 | callees_t *callees = callTable[this]; | 
|---|
|  | 148 | callees_t::iterator iter; | 
|---|
|  | 149 | for(iter=callees->begin();iter!=callees->end();iter++){ | 
|---|
|  | 150 | (*iter).second->subjectKilled(this); | 
|---|
|  | 151 | } | 
|---|
|  | 152 | callTable.erase(this); | 
|---|
|  | 153 | delete callees; | 
|---|
| [63c1f6] | 154 | } | 
|---|
|  | 155 | } | 
|---|
|  | 156 |  | 
|---|
|  | 157 | Observer::Observer() | 
|---|
|  | 158 | {} | 
|---|
|  | 159 |  | 
|---|
|  | 160 | Observer::~Observer() | 
|---|
|  | 161 | {} | 
|---|