Changeset c0332d for molecuilder/src/Patterns/Observer.cpp
- Timestamp:
- Apr 24, 2010, 3:27:00 PM (16 years ago)
- Children:
- 65b413
- Parents:
- 298621
- File:
-
- 1 edited
-
molecuilder/src/Patterns/Observer.cpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/Patterns/Observer.cpp
r298621 rc0332d 27 27 map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers 28 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; 29 30 set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers 30 31 … … 68 69 } 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 70 76 /** Constructor for Observable Protector. 71 77 * Basically, calls start_observer_internal(). Hence use this class instead of … … 97 103 * and removes from busy list. 98 104 */ 99 void Observable::notifyAll() {105 void Observable::notifyAll() try { 100 106 // we are busy notifying others right now 101 107 // add ourselves to the list of busy subjects to enable circle detection … … 113 119 } 114 120 } 115 // done with notification, we can leave the set of busy subjects 121 122 // send out all notifications that need to be done 123 124 notificationSet currentNotifications = notifications[this]; 125 for(notificationSet::iterator it = currentNotifications.begin(); 126 it != currentNotifications.end();++it){ 127 (*it)->notifyAll(); 128 } 129 130 notifications.erase(this); 131 132 // done with notification, we can leave the set of busy subjects 116 133 busyObservables.erase(this); 117 134 } 135 ASSERT_NOCATCH("Exception thrown from Observer Update") 118 136 119 137 /** Handles passing on updates from sub-Observables. … … 191 209 } 192 210 211 void Observable::signOn(Observer *target, Notification_ptr notification){ 212 ASSERT(notification->owner==this, 213 "Trying to sign on for a notification that is not provided by this object"); 214 215 notification->addObserver(target); 216 } 217 218 void Observable::signOff(Observer *target, Notification_ptr notification){ 219 ASSERT(notification->owner==this, 220 "Trying to sign off from a notification that is not provided by this object"); 221 222 notification->removeObserver(target); 223 } 224 193 225 bool Observable::isBlocked(){ 194 226 return depth.count(this) > 0; … … 233 265 Observer::~Observer() 234 266 {} 267 268 /** 269 * Method for specialized notifications. 270 * Most Observers wont need or use this, so it is implemented 271 * empty in the base case; 272 */ 273 void Observer::recieveNotification(Observable *publisher, Notification_ptr notification){ 274 } 275 276 Notification::Notification(Observable *_owner) : 277 owner(_owner) 278 {} 279 280 Notification::~Notification(){} 281 282 void Notification::addObserver(Observer *target){ 283 targets.insert(target); 284 } 285 286 void Notification::removeObserver(Observer *target){ 287 targets.erase(target); 288 } 289 290 void Notification::notifyAll(){ 291 for(std::set<Observer*>::iterator it=targets.begin(); 292 it!=targets.end();++it){ 293 (*it)->recieveNotification(owner,this); 294 } 295 }
Note:
See TracChangeset
for help on using the changeset viewer.
