source: src/Patterns/Observer.cpp@ 74e0f7

Last change on this file since 74e0f7 was 74e0f7, checked in by Frederik Heber <heber@…>, 14 years ago

Extended Observables by NotificationChannels member variable.

  • new class Channels that aggregates Notifications.
  • NOTIFY now just takes a channel number.
  • may or may not be instantiated for a given Observable.
  • Observable just needs to add enum of all desired channels and addChannel on each of these.
  • ObserverUnitTest has been adapted.
  • Property mode set to 100644
File size: 14.6 KB
Line 
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
8/*
9 * Observer.cpp
10 *
11 * Created on: Jan 19, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "MemDebug.hpp"
21
22#include "Observer.hpp"
23
24#include <iostream>
25
26#include "Assert.hpp"
27#include "MemDebug.hpp"
28
29using 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 Observable 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
41map<Observable*, int> Observable::depth; //!< Map of Observables to the depth of the DAG of Observers
42map<Observable*,multimap<int,Observer*> > Observable::callTable; //!< Table for each Observable of all its Observers
43std::map<Observable*,std::set<Notification*> > Observable::notifications;
44set<Observable*> Observable::busyObservables; //!< Set of Observables that are currently busy notifying their sign-on'ed Observers
45
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 */
55void 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)
59#ifdef LOG_OBSERVER
60 observerLog().addMessage(depth[publisher]) << ">> Locking " << observerLog().getName(publisher) << endl;
61#endif
62 depth[publisher]++;
63}
64
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 */
75void 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
79 --depth[publisher];
80#ifdef LOG_OBSERVER
81 observerLog().addMessage(depth[publisher]) << "<< Unlocking " << observerLog().getName(publisher) << endl;
82#endif
83 if(depth[publisher]){}
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
92void 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
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 */
103Observable::_Observable_protector::_Observable_protector(Observable *_protege) :
104 protege(_protege)
105{
106 start_observer_internal(protege);
107}
108
109Observable::_Observable_protector::_Observable_protector(const _Observable_protector &dest) :
110 protege(dest.protege)
111{
112 start_observer_internal(protege);
113}
114
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 */
121Observable::_Observable_protector::~_Observable_protector()
122{
123 finish_observer_internal(protege);
124}
125
126/************* Notification mechanism for observables **************/
127
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 */
132void Observable::notifyAll() {
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
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){
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
151 (*iter).second->update(this);
152 }
153 }
154 }
155 ASSERT_NOCATCH("Exception thrown from Observer Update");
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
168 busyObservables.erase(this);
169}
170
171
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 */
177void 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
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");
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()) {
193#ifdef LOG_OBSERVER
194 observerLog().addMessage() << "-* Update from " << observerLog().getName(publisher)
195 << " propagated by " << observerLog().getName(this) << endl;
196#endif
197 notifyAll();
198 }
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 }
205 }
206}
207
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 */
213void Observable::signOn(Observer *target,int priority) {
214 ASSERT(priority>=-20 && priority<=+20, "Priority out of range [-20:+20] when signing on Observer");
215#ifdef LOG_OBSERVER
216 observerLog().addMessage() << "@@ Signing on " << observerLog().getName(target) << " to " << observerLog().getName(this) << endl;
217#endif
218 bool res = false;
219 callees_t &callees = callTable[this];
220
221 callees_t::iterator iter;
222 for(iter=callees.begin();iter!=callees.end();++iter){
223 res |= ((*iter).second == target);
224 }
225 if(!res)
226 callees.insert(pair<int,Observer*>(priority,target));
227}
228
229/** Sign off an Observer from this Observable.
230 * Removes \a *target from Observable::callTable list.
231 * \param *target Observer
232 */
233void Observable::signOff(Observer *target) {
234 ASSERT(callTable.count(this),"SignOff called for an Observable without Observers.");
235#ifdef LOG_OBSERVER
236 observerLog().addMessage() << "** Signing off " << observerLog().getName(target) << " from " << observerLog().getName(this) << endl;
237#endif
238 callees_t &callees = callTable[this];
239
240 callees_t::iterator iter;
241 callees_t::iterator deliter;
242 for(iter=callees.begin();iter!=callees.end();) {
243 if((*iter).second == target) {
244 callees.erase(iter++);
245 }
246 else {
247 ++iter;
248 }
249 }
250 if(callees.empty()){
251 callTable.erase(this);
252 }
253}
254
255void 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
262void 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
269bool Observable::isBlocked(){
270 return depth.count(this) > 0;
271}
272
273Notification_ptr Observable::getChannel(size_t no) const
274{
275 ASSERT(NotificationChannels != NULL,
276 "Observable::getChannel() - observable has no channels.");
277 return NotificationChannels->getChannel(no);
278}
279
280/** Handles sub-observables that just got killed
281 * when an sub-observerable dies we usually don't need to do anything
282 * \param *publisher Sub-Observable.
283 */
284void Observable::subjectKilled(Observable *publisher){
285}
286
287/** Constructor for class Observable.
288 */
289Observable::Observable(string name) :
290 Observer(Observer::BaseConstructor())
291{
292#ifdef LOG_OBSERVER
293 observerLog().addName(this,name);
294 observerLog().addMessage() << "++ Creating Observable " << observerLog().getName(this) << endl;
295#endif
296}
297
298/** Destructor for class Observable.
299 * When an observable is deleted, we let all our observers know. \sa Observable::subjectKilled().
300 */
301Observable::~Observable()
302{
303#ifdef LOG_OBSERVER
304 observerLog().addMessage() << "-- Destroying Observable " << observerLog().getName(this) << endl;
305#endif
306 if(callTable.count(this)) {
307 // delete all entries for this observable
308 callees_t callees = callTable[this];
309 callees_t::iterator iter;
310 for(iter=callees.begin();iter!=callees.end();++iter){
311 (*iter).second->subjectKilled(this);
312 }
313 callTable.erase(this);
314 }
315}
316
317/** Constructor for class Observer.
318 */
319Observer::Observer(string name)
320{
321#ifdef LOG_OBSERVER
322 observerLog().addName(this,name);
323 observerLog().addMessage() << "++ Creating Observer " << observerLog().getName(this) << endl;
324#endif
325}
326
327/**
328 * Base Constructor for class Observer
329 *
330 * only called from Observable Constructor
331 */
332Observer::Observer(Observer::BaseConstructor){
333#ifdef LOG_OBSERVER
334 observerLog().addObservable(this);
335#endif
336}
337
338/** Destructor for class Observer.
339 */
340Observer::~Observer()
341{
342#ifdef LOG_OBSERVER
343 if(!observerLog().isObservable(this)){
344 observerLog().addMessage() << "-- Destroying Observer " << observerLog().getName(this) << endl;
345 }
346#endif
347}
348
349/**
350 * Method for specialized notifications.
351 * Most Observers wont need or use this, so it is implemented
352 * empty in the base case;
353 */
354void Observer::recieveNotification(Observable *publisher, Notification_ptr notification){
355 ASSERT(0,"Notification received by object that did not sign on for it.");
356}
357
358Notification::Notification(Observable *_owner) :
359 owner(_owner), channelno(-1)
360{}
361
362Notification::Notification(Observable *_owner, size_t _channelno) :
363 owner(_owner), channelno(_channelno)
364{}
365
366Notification::~Notification(){}
367
368void Notification::addObserver(Observer *target){
369 targets.insert(target);
370}
371
372void Notification::removeObserver(Observer *target){
373 targets.erase(target);
374}
375
376void Notification::notifyAll(){
377 for(std::set<Observer*>::iterator it=targets.begin();
378 it!=targets.end();++it){
379 (*it)->recieveNotification(owner,this);
380 }
381}
382
383Channels::Channels(Observable *_owner) :
384 owner(_owner)
385{}
386
387Channels::~Channels()
388{
389 // free all present Notifications
390 for(NotificationTypetoRefMap::iterator iter = ChannelMap.begin();
391 !ChannelMap.empty(); iter = ChannelMap.begin()) {
392 delete iter->second;
393 ChannelMap.erase(iter);
394 }
395}
396
397void Channels::addChannel(size_t no)
398{
399 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no);
400 ASSERT(iter == ChannelMap.end(),
401 "Channels::addChannel() - channel "+toString(int(no))+" is already present in ChannelMap.");
402 ChannelMap.insert( std::make_pair(no, new Notification(owner, no)) );
403}
404
405void Channels::removeChannel(size_t no)
406{
407 NotificationTypetoRefMap::iterator iter = ChannelMap.find(no);
408 ASSERT(iter != ChannelMap.end(),
409 "Channels::removeChannel() - channel "+toString(int(no))+" not present in ChannelMap.");
410 delete iter->second;
411 ChannelMap.erase(iter);
412}
413
414Notification_ptr Channels::getChannel(size_t no) const
415{
416 NotificationTypetoRefMap::const_iterator iter = ChannelMap.find(no);
417 ASSERT(iter != ChannelMap.end(),
418 "Channels::getChannel() - channel "+toString(int(no))+" not present in ChannelMap.");
419 return iter->second;
420}
421
422size_t Channels::getType(Notification_ptr channel) const
423{
424 return channel->getChannelNo();
425}
426
427
428#ifdef LOG_OBSERVER
429
430/************************* Methods to do logging of the Observer Mechanism *********/
431
432// The log needs to exist fairly early, so we make it construct on first use,
433// and never destroy it
434ObserverLog &observerLog(){
435 // yes, this memory is never freed... we need it around for the whole programm,
436 // so no freeing is possible
437 static ObserverLog *theLog = Memory::ignore(new ObserverLog());
438 return *theLog;
439}
440
441
442ObserverLog::ObserverLog() :
443 count (0)
444{}
445
446ObserverLog::~ObserverLog(){}
447
448string ObserverLog::getLog(){return log.str();}
449
450std::string ObserverLog::getName(void* obj){
451 return names[obj];
452}
453
454bool ObserverLog::isObservable(void* obj){
455 return observables.count(obj);
456}
457
458void ObserverLog::addName(void* obj , string name){
459 stringstream sstr;
460 sstr << name << "_" << count++;
461 names[obj] = sstr.str();
462}
463
464void ObserverLog::addObservable(void* obj){
465 observables.insert(obj);
466}
467
468void ObserverLog::deleteName(void* obj){
469 names.erase(obj);
470}
471
472void ObserverLog::deleteObservable(void* obj){
473 observables.erase(obj);
474}
475
476stringstream &ObserverLog::addMessage(int depth){
477 for(int i=depth;i--;)
478 log << " ";
479 return log;
480}
481
482#endif
Note: See TracBrowser for help on using the repository browser.