source: src/CodePatterns/Observer/Observable.hpp@ b324a3

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

Added Graveyard for to-be-destroyed Observables.

  • also added Zombie class that wraps an Observable.
  • Notification and Observable have new function getNumberOfObservers().
  • added unit test for Graveyard.
  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[e2e035e]1/*
2 * Observable.hpp
3 *
4 * Created on: Dec 1, 2011
5 * Author: heber
6 */
7
8#ifndef OBSERVABLE_HPP_
9#define OBSERVABLE_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>
[b324a3]19#include <boost/function.hpp>
[e2e035e]20
[9b8fa4]21#include "CodePatterns/Range.hpp"
22#include "CodePatterns/Observer/defs.hpp"
23#include "CodePatterns/Observer/Observer.hpp"
[e2e035e]24
[b324a3]25class Graveyard;
26
[e2e035e]27/**
28 * An Observable implements all neccessary method for being observed.
29 *
30 * That is, it provides methods for signing on and of from an
31 * Observable that can be used by any observer. The actual
32 * observer-mechanism is handled at a central static place
33 * to avoid memory issues when many observable are around but only few
34 * are actually observed.
[1c291d]35 *
36 * \note We have to clean our Channels from static NotificationChannels and
37 * we call Channels::subjectKilled() to let Observers that have only signed
38 * on to single channel still know when their observable has died.
[b324a3]39 *
40 * Note that one may allow an Observable to live some over-time by using
41 * the Graveyard. This allows any Observer to still access the instance
42 * in order to properly sign off. It is destroyed when no Observer is left.
[e2e035e]43 */
44class Observable : public Observer {
45public:
46 Observable(std::string _name);
47 virtual ~Observable();
48
[8fe1e2]49 /** This class is only used to distinguish from size_t in the overload.
50 *
51 * It encapsulates a const int (the priority level) and checks valid bounds
52 * in constructor.
53 *
54 */
55 class PriorityLevel {
56 public:
57 explicit PriorityLevel(const int i);
58 ~PriorityLevel();
59
60 const int level;
61 private:
62 static range<int> ValidRange;
63 };
64
[e2e035e]65 /**
66 * Sign an Observer on to this Observable. The Observer will be notified
67 * whenever something inside the Observable changes. The Observer can
68 * assign itself a priority for the changes in the range of -20:+20.
69 * The Observer with lower priority will be called before the others,
70 * same as with Unix nice-levels. This can be used when an Object
71 * contains other objects that observe it (derived values), and these objects have
72 * to recalculate their states before the changes should be propageted to the
73 * UI. A default priority of 0 should be fine in most cases, since there is
74 * ussually no need to order the update sequence.
75 */
[8fe1e2]76 virtual void signOn(Observer *target, PriorityLevel priority = PriorityDefault) const;
[e2e035e]77
78 /**
79 * Sign of a previously signed on Observer. After this no more
80 * updates will be recieved from that observer.
81 */
82 virtual void signOff(Observer *target) const;
83
84 /**
85 * Sign on for specialized notifications
86 */
[40f2e6]87 virtual void signOn(Observer *target, size_t channelno) const;
[e2e035e]88
89 /**
90 * Stop receiving a specialized notification
91 */
[40f2e6]92 virtual void signOff(Observer *target, size_t channelno) const;
[e2e035e]93
94 /**
95 * Ask an Observer if it is currently in a blocked state, i.e. if
96 * Changes are in Progress, that are not yet published.
97 */
98 virtual bool isBlocked() const;
99
100 Notification_ptr getChannel(size_t no) const;
101
[b324a3]102 size_t getNumberOfObservers() const;
103
[e2e035e]104protected:
105 virtual void update(Observable *publisher);
106 virtual void subjectKilled(Observable *publisher);
107
108 virtual void notifyAll();
109protected:
110// Observer mechanism is done from a static central place
111 /**
112 * Internal method.
113 * Do not call directly. Use OBSERVE macro instead
114 */
115 static void start_observer_internal(Observable *publisher);
116 /**
117 * Internal method.
118 * Do not call directly. Use OBSERVE macro instead
119 */
120 static void finish_observer_internal(Observable *publisher);
121
122 static void enque_notification_internal(Observable *publisher, Notification_ptr notification);
123
124 typedef std::map<Observable*, Channels *> ChannelMap;
125 static ChannelMap NotificationChannels;
126
[8fe1e2]127 static PriorityLevel PriorityDefault;
128
[d85532]129protected:
[e2e035e]130 typedef std::multimap<int,Observer*> callees_t;
131 typedef std::set<Notification*> notificationSet;
132 static std::map<Observable*, int> depth;
133 static std::map<Observable*,callees_t> callTable;
134 static std::map<Observable*,notificationSet> notifications;
135 static std::set<Observable*> busyObservables;
136
[b324a3]137private:
138 friend class Zombie;
139 friend class Graveyard;
140
141 typedef boost::function<void (const Observable*)> graveyard_informer_t;
142
143 //!> bound function to call when Observer are signing off
144 graveyard_informer_t &graveyard_informer;
145
146 //!> default informer that does nothing
147 static graveyard_informer_t noop_informer;
148
149 /** Sets the bound function for over-time life-time management.
150 *
151 * \param _graveyard Graveyard to inform of leaving Observers
152 */
153 void setGraveyardInformer(graveyard_informer_t &_graveyard_informer)
154 {
155 graveyard_informer = _graveyard_informer;
156 }
157
[e2e035e]158 //! @cond
159 // Structure for RAII-Style notification
160public:
161 /**
162 * This structure implements the Observer-mechanism RAII-Idiom.
163 * It triggers certain functions on creation and destruction so that
164 * Observer mechanisms can be linked to scope block.
165 */
166 class _Observable_protector {
167 public:
168 _Observable_protector(Observable *);
169 _Observable_protector(const _Observable_protector&);
170 ~_Observable_protector();
171 private:
172 Observable *protege;
173 };
174 //! @endcond
175};
176
177
178// extra macro is necessary to work with __LINE__
179#define PASTE(a,b) PASTE_HELPER(a,b)
180#define PASTE_HELPER(a,b) a ## b
181#define OBSERVE Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(this)
[40f2e6]182#define NOTIFY(channelno) do{Observable::enque_notification_internal(this,NotificationChannels[this]->getChannel(channelno));}while(0)
[e2e035e]183#define LOCK_OBSERVABLE(observable) Observable::_Observable_protector PASTE(_scope_obs_protector_,__LINE__)(&(observable))
184
185#endif /* OBSERVABLE_HPP_ */
Note: See TracBrowser for help on using the repository browser.