| 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 |
|
|---|
| 26 | #include "Observer/defs.hpp"
|
|---|
| 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 Observable;
|
|---|
| 47 | friend class Notification;
|
|---|
| 48 | template<class> friend class ObservedIterator;
|
|---|
| 49 |
|
|---|
| 50 | // indicates the constructor called from Observables
|
|---|
| 51 | struct BaseConstructor{};
|
|---|
| 52 |
|
|---|
| 53 | public:
|
|---|
| 54 | Observer(BaseConstructor);
|
|---|
| 55 | Observer(std::string);
|
|---|
| 56 | virtual ~Observer();
|
|---|
| 57 |
|
|---|
| 58 | protected:
|
|---|
| 59 | /**
|
|---|
| 60 | * This method is called upon changes of the Observable
|
|---|
| 61 | */
|
|---|
| 62 | virtual void update(Observable *publisher)=0;
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * This method is called when a special named change
|
|---|
| 66 | * of the Observable occured
|
|---|
| 67 | */
|
|---|
| 68 | virtual void recieveNotification(Observable *publisher, Notification_ptr notification);
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * This method is called when the observed object is destroyed.
|
|---|
| 72 | */
|
|---|
| 73 | virtual void subjectKilled(Observable *publisher)=0;
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | #ifdef LOG_OBSERVER
|
|---|
| 77 | #include "ObserverLog.hpp"
|
|---|
| 78 | #endif
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | #endif /* OBSERVER_HPP_ */
|
|---|