| [e2e035e] | 1 | /*
 | 
|---|
 | 2 |  * Observer.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Jan 19, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef OBSERVER_HPP_
 | 
|---|
 | 9 | #define OBSERVER_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include <map>
 | 
|---|
 | 17 | #include <set>
 | 
|---|
 | 18 | #include <string>
 | 
|---|
 | 19 | #include <sstream>
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | // Deactivate any logging when we are not in debug mode
 | 
|---|
 | 22 | #ifdef NDEBUG
 | 
|---|
 | 23 | #undef LOG_OBSERVER
 | 
|---|
 | 24 | #endif
 | 
|---|
 | 25 | 
 | 
|---|
| [9b8fa4] | 26 | #include "CodePatterns/Observer/defs.hpp"
 | 
|---|
| [e2e035e] | 27 | 
 | 
|---|
 | 28 | class Channels;
 | 
|---|
 | 29 | class Observable;
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | /**
 | 
|---|
 | 32 |  * An Observer is notified by all Observed objects, when anything changes.
 | 
|---|
 | 33 |  *
 | 
|---|
 | 34 |  * If a simple change is done to an Object the Obervable will call the update() method
 | 
|---|
 | 35 |  * of all signed on observers, passing itself as a parameter for identification. The
 | 
|---|
 | 36 |  * Observers should then react to the changes and update themselves accordingly.
 | 
|---|
 | 37 |  *
 | 
|---|
 | 38 |  * If an observed Object is destroyed it will call the subjectKilled() method
 | 
|---|
 | 39 |  * of all signed on Observers, again passing itself as a parameter for identification.
 | 
|---|
 | 40 |  * The Observers should handle the destruction of an observed Object gracefully, i.e.
 | 
|---|
 | 41 |  * set themselves inactive, display something else, etc. There is no need
 | 
|---|
 | 42 |  * to sign of from the dying object, since this will be handled by the Observable destructor.
 | 
|---|
 | 43 |  */
 | 
|---|
 | 44 | class Observer
 | 
|---|
 | 45 | {
 | 
|---|
 | 46 |   friend class Notification;
 | 
|---|
| [d85532] | 47 |   friend class Observable;
 | 
|---|
 | 48 |   friend class Relay;
 | 
|---|
| [e2e035e] | 49 |   template<class> friend class ObservedIterator;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |   // indicates the constructor called from Observables
 | 
|---|
 | 52 |   struct BaseConstructor{};
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | public:
 | 
|---|
 | 55 |   Observer(BaseConstructor);
 | 
|---|
 | 56 |   Observer(std::string);
 | 
|---|
 | 57 |   virtual ~Observer();
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | protected:
 | 
|---|
 | 60 |   /**
 | 
|---|
 | 61 |    * This method is called upon changes of the Observable
 | 
|---|
 | 62 |    */
 | 
|---|
 | 63 |   virtual void update(Observable *publisher)=0;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |   /**
 | 
|---|
 | 66 |    * This method is called when a special named change
 | 
|---|
 | 67 |    * of the Observable occured
 | 
|---|
 | 68 |    */
 | 
|---|
 | 69 |   virtual void recieveNotification(Observable *publisher, Notification_ptr notification);
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |   /**
 | 
|---|
 | 72 |    * This method is called when the observed object is destroyed.
 | 
|---|
 | 73 |    */
 | 
|---|
 | 74 |   virtual void subjectKilled(Observable *publisher)=0;
 | 
|---|
 | 75 | };
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | #ifdef LOG_OBSERVER
 | 
|---|
 | 78 | #include "ObserverLog.hpp"
 | 
|---|
 | 79 | #endif
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | #endif /* OBSERVER_HPP_ */
 | 
|---|