Changeset 454bc54 for src/CodePatterns/Cacheable.hpp
- 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)
- File:
-
- 1 edited
-
src/CodePatterns/Cacheable.hpp (modified) (10 diffs)
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");
Note:
See TracChangeset
for help on using the changeset viewer.
