Changeset 454bc54 for src/CodePatterns
- Timestamp:
- Oct 30, 2015, 11:43:01 AM (10 years ago)
- Children:
- e24dde
- Parents:
- 1b5188
- git-author:
- Frederik Heber <heber@…> (06/19/15 22:28:24)
- git-committer:
- Frederik Heber <heber@…> (10/30/15 11:43:01)
- Location:
- src/CodePatterns
- Files:
-
- 1 added
- 4 edited
-
Cacheable.hpp (modified) (10 diffs)
-
ObservedValue.hpp (added)
-
Observer/Notification.hpp (modified) (3 diffs)
-
Observer/Observable.hpp (modified) (3 diffs)
-
Observer/Relay.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/CodePatterns/Cacheable.hpp
r1b5188 r454bc54 20 20 21 21 #include "CodePatterns/Assert.hpp" 22 23 #include "CodePatterns/Observer/Notification.hpp" 22 24 23 25 #ifndef NO_CACHING … … 142 144 143 145 public: 144 Cacheable(Observable *_owner, boost::function<T()> _recalcMethod, std::string name); 146 Cacheable( 147 Observable *_owner, 148 const boost::function<T()> _recalcMethod, 149 const std::string &name, 150 const Observable::channels_t &_channels = Observable::channels_t()); 145 151 virtual ~Cacheable(); 146 152 … … 150 156 // methods implemented for base-class Observer 151 157 void update(Observable *subject); 158 void recieveNotification(Observable *publisher, Notification_ptr notification); 152 159 void subjectKilled(Observable *subject); 153 160 private: … … 160 167 // destroyed state is not predefined, because we rarely enter that state and never leave 161 168 162 Observable *owner; 163 boost::function<T()> recalcMethod; 169 const Observable * owner; 170 const boost::function<T()> recalcMethod; 171 172 //!> contains list of possible channels to enlist, if empty we signOn globally 173 const Observable::channels_t channels; 164 174 165 175 // de-activated copy constructor … … 169 179 170 180 template<typename T> 171 Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod, std::string name) : 172 Observer(name + "(Cached)"), 181 Cacheable<T>::Cacheable( 182 Observable *_owner, 183 const boost::function<T()> _recalcMethod, 184 const std::string &_name, 185 const Observable::channels_t &_channels) : 186 Observer(_name + "(Cached)"), 173 187 owner(_owner), 174 recalcMethod(_recalcMethod) 188 recalcMethod(_recalcMethod), 189 channels(_channels) 175 190 { 176 191 // create all states needed for this object … … 180 195 // we sign on with the best(=lowest) priority, so cached values are recalculated before 181 196 // anybody else might ask for updated values 182 if (owner != NULL) 183 owner->signOn(this,Observable::PriorityLevel(int(-20))); 197 if (owner != NULL) { 198 if (channels.empty()) { 199 owner->signOn(this,Observable::PriorityLevel(int(-20))); 200 } else { 201 for (Observable::channels_t::const_iterator iter = channels.begin(); 202 iter != channels.end(); ++iter) 203 owner->signOn(this,*iter,Observable::PriorityLevel(int(-20))); 204 } 205 } 184 206 } 185 207 … … 204 226 Cacheable<T>::~Cacheable() 205 227 { 206 if (owner != NULL) 207 owner->signOff(this); 228 if (owner != NULL) { 229 if (channels.empty()) { 230 owner->signOff(this); 231 } else { 232 for (Observable::channels_t::const_iterator iter = channels.begin(); 233 iter != channels.end(); ++iter) 234 owner->signOff(this,*iter); 235 } 236 } 208 237 } 209 238 … … 215 244 template<typename T> 216 245 void Cacheable<T>::update(Observable *subject) { 246 ASSERT( channels.empty(), 247 "Cacheable<T>::update() - we are listening only to global updates."); 217 248 state->invalidate(); 249 } 250 251 template<typename T> 252 void Cacheable<T>::recieveNotification(Observable *publisher, Notification_ptr notification) { 253 if (publisher == owner) { 254 ASSERT( !channels.empty(), 255 "Cacheable<T>::update() - we are not listening to global updates."); 256 const Observable::channels_t::const_iterator iter = std::find( 257 channels.begin(), channels.end(), notification->getChannelNo()); 258 if (iter != channels.end()) 259 state->invalidate(); 260 } 218 261 } 219 262 … … 248 291 // methods implemented for base-class Observer 249 292 void update(Observable *subject); 293 void recieveNotification(Observable *publisher, Notification_ptr notification); 250 294 void subjectKilled(Observable *subject); 251 295 private: … … 280 324 281 325 template<typename T> 326 void Cacheable<T>::recieveNotification(Observable *publisher, Notification_ptr notification) { 327 ASSERT(0, "Cacheable::recieveNotification should never be called when caching is disabled"); 328 } 329 330 template<typename T> 282 331 void Cacheable<T>::subjectKilled(Observable *subject){ 283 332 ASSERT(0, "Cacheable::subjectKilled should never be called when caching is disabled"); -
src/CodePatterns/Observer/Notification.hpp
r1b5188 r454bc54 15 15 16 16 #include <set> 17 18 #include <boost/thread/recursive_mutex.hpp> 17 19 18 20 class Channels; … … 41 43 protected: 42 44 43 void addObserver(Observer *target );45 void addObserver(Observer *target, const int priority); 44 46 void removeObserver(Observer *target); 45 47 … … 73 75 74 76 private: 75 typedef std:: set<Observer*> targets_t;77 typedef std::multimap<int,Observer*> targets_t; 76 78 targets_t targets; 77 79 const size_t channelno; 80 81 mutable boost::recursive_mutex TargetsLock; //!< a lock for the pointer of the instance 78 82 }; 79 83 -
src/CodePatterns/Observer/Observable.hpp
r1b5188 r454bc54 45 45 class Observable : public Observer { 46 46 public: 47 Observable(std::string _name); 47 //!> typedef for a vector of channels 48 typedef std::vector<size_t> channels_t; 49 50 Observable(std::string _name, const channels_t &_channels = channels_t()); 48 51 virtual ~Observable(); 49 52 … … 63 66 static range<int> ValidRange; 64 67 }; 68 69 private: 70 /** Helper class to create a unique increasing list. 71 * 72 */ 73 struct UniqueNumber 74 { 75 int operator()() 76 { return value++; } 77 int value; 78 }; 79 80 public: 81 static channels_t getChannelList(const size_t max); 65 82 66 83 /** … … 86 103 * Sign on for specialized notifications 87 104 */ 88 virtual void signOn(Observer *target, size_t channelno) const; 105 virtual void signOn( 106 Observer *target, 107 size_t channelno, 108 PriorityLevel priority = PriorityDefault) const; 89 109 90 110 /** -
src/CodePatterns/Observer/Relay.hpp
r1b5188 r454bc54 40 40 virtual void signOff(Observer *target) const; 41 41 42 virtual void signOn(Observer *target, size_t channelno ) const;42 virtual void signOn(Observer *target, size_t channelno, PriorityLevel priority = Observable::PriorityDefault) const; 43 43 44 44 virtual void signOff(Observer *target, size_t channelno) const;
Note:
See TracChangeset
for help on using the changeset viewer.
