| [e3c8b4] | 1 | /*
 | 
|---|
 | 2 |  * Cacheable.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Feb 2, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef CACHEABLE_HPP_
 | 
|---|
 | 9 | #define CACHEABLE_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | #include "Patterns/Observer.hpp"
 | 
|---|
 | 12 | #include <boost/function.hpp>
 | 
|---|
| [052b8d0] | 13 | #include <boost/shared_ptr.hpp>
 | 
|---|
 | 14 | #include <iostream>
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "Helpers/Assert.hpp"
 | 
|---|
| [e3c8b4] | 17 | 
 | 
|---|
| [9ad391] | 18 | #ifndef NO_CACHING
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 |   template<typename T>
 | 
|---|
 | 21 |   class Cacheable : public Observer
 | 
|---|
 | 22 |   {
 | 
|---|
| [052b8d0] | 23 |     // we define the states of the cacheable so we can do very fast state-checks
 | 
|---|
 | 24 |     class State{
 | 
|---|
 | 25 |     public:
 | 
|---|
 | 26 |       State(Cacheable *_owner) :
 | 
|---|
| [adc42b] | 27 |         busy(false),
 | 
|---|
 | 28 |         owner(_owner)
 | 
|---|
| [052b8d0] | 29 |         {}
 | 
|---|
| [adc42b] | 30 |       virtual T getValue()=0;
 | 
|---|
| [052b8d0] | 31 |       virtual void invalidate()=0;
 | 
|---|
 | 32 |       virtual bool isValid()=0;
 | 
|---|
 | 33 |       virtual void enter()=0;
 | 
|---|
 | 34 |       bool isBusy(){
 | 
|---|
 | 35 |         return busy;
 | 
|---|
 | 36 |       }
 | 
|---|
 | 37 |     protected:
 | 
|---|
 | 38 |       bool busy;
 | 
|---|
 | 39 |       Cacheable *owner;
 | 
|---|
 | 40 |     };
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |     class InvalidState : public State{
 | 
|---|
 | 43 |     public:
 | 
|---|
 | 44 |       InvalidState(Cacheable *_owner):
 | 
|---|
 | 45 |         State(_owner)
 | 
|---|
 | 46 |         {}
 | 
|---|
 | 47 | 
 | 
|---|
| [adc42b] | 48 |       virtual T getValue(){
 | 
|---|
| [052b8d0] | 49 |         // set the state to valid
 | 
|---|
 | 50 |         State::owner->switchState(State::owner->validState);
 | 
|---|
 | 51 |         // get the value from the now valid state
 | 
|---|
 | 52 |         return State::owner->state->getValue();
 | 
|---|
 | 53 |       }
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |       virtual void invalidate(){
 | 
|---|
 | 56 |         // nothing to do on this message
 | 
|---|
 | 57 |       }
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 |       virtual bool isValid(){
 | 
|---|
 | 60 |         return false;
 | 
|---|
 | 61 |       }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |       virtual void enter(){
 | 
|---|
 | 64 |         // nothing to do when entering this
 | 
|---|
 | 65 |       }
 | 
|---|
 | 66 |     };
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |     class ValidState : public State{
 | 
|---|
 | 69 |     public:
 | 
|---|
 | 70 |       ValidState(Cacheable *_owner) :
 | 
|---|
 | 71 |         State(_owner)
 | 
|---|
 | 72 |         {}
 | 
|---|
 | 73 | 
 | 
|---|
| [adc42b] | 74 |       virtual T getValue(){
 | 
|---|
| [052b8d0] | 75 |         return content;
 | 
|---|
 | 76 |       }
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |       virtual void invalidate(){
 | 
|---|
 | 79 |         State::owner->switchState(State::owner->invalidState);
 | 
|---|
 | 80 |       }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |       virtual bool isValid(){
 | 
|---|
 | 83 |         return true;
 | 
|---|
 | 84 |       }
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |       virtual void enter(){
 | 
|---|
 | 87 |         State::busy= true;
 | 
|---|
 | 88 |         // as soon as we enter the valid state we recalculate
 | 
|---|
 | 89 |         content = State::owner->recalcMethod();
 | 
|---|
 | 90 |         State::busy = false;
 | 
|---|
 | 91 |       }
 | 
|---|
 | 92 |     private:
 | 
|---|
 | 93 |       T content;
 | 
|---|
 | 94 |     };
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |     class DestroyedState : public State {
 | 
|---|
 | 97 |     public:
 | 
|---|
 | 98 |       DestroyedState(Cacheable *_owner) :
 | 
|---|
 | 99 |         State(_owner)
 | 
|---|
 | 100 |         {}
 | 
|---|
 | 101 | 
 | 
|---|
| [adc42b] | 102 |       virtual T getValue(){
 | 
|---|
| [052b8d0] | 103 |         ASSERT(0,"Cannot get a value from a Cacheable after it's Observable has died");
 | 
|---|
 | 104 |         // we have to return a grossly invalid reference, because no value can be produced anymore
 | 
|---|
 | 105 |         return *(static_cast<T*>(0));
 | 
|---|
 | 106 |       }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 |       virtual void invalidate(){
 | 
|---|
 | 109 |         ASSERT(0,"Cannot invalidate a Cacheable after it's Observable has died");
 | 
|---|
 | 110 |       }
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |       virtual bool isValid(){
 | 
|---|
 | 113 |         ASSERT(0,"Cannot check validity of a Cacheable after it's Observable has died");
 | 
|---|
 | 114 |         return false;
 | 
|---|
 | 115 |       }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 |       virtual void enter(){
 | 
|---|
 | 118 |         // nothing to do when entering this state
 | 
|---|
 | 119 |       }
 | 
|---|
 | 120 |     };
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 |   typedef boost::shared_ptr<State> state_ptr;
 | 
|---|
 | 124 | 
 | 
|---|
| [9ad391] | 125 |   public:
 | 
|---|
 | 126 |     Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
 | 
|---|
 | 127 |     virtual ~Cacheable();
 | 
|---|
 | 128 | 
 | 
|---|
| [052b8d0] | 129 |     const bool isValid() const;
 | 
|---|
| [adc42b] | 130 |     const T operator*() const;
 | 
|---|
| [9ad391] | 131 | 
 | 
|---|
 | 132 |     // methods implemented for base-class Observer
 | 
|---|
 | 133 |     void update(Observable *subject);
 | 
|---|
 | 134 |     void subjectKilled(Observable *subject);
 | 
|---|
 | 135 |   private:
 | 
|---|
 | 136 | 
 | 
|---|
| [052b8d0] | 137 |     void switchState(state_ptr newState);
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |     mutable state_ptr state;
 | 
|---|
 | 140 |     // pre-defined state so we don't have to construct to much
 | 
|---|
 | 141 |     state_ptr invalidState;
 | 
|---|
 | 142 |     state_ptr validState;
 | 
|---|
 | 143 |     // destroyed state is not predefined, because we rarely enter that state and never leave
 | 
|---|
 | 144 | 
 | 
|---|
| [9ad391] | 145 |     Observable *owner;
 | 
|---|
| [052b8d0] | 146 | 
 | 
|---|
| [9ad391] | 147 |     boost::function<T()> recalcMethod;
 | 
|---|
| [052b8d0] | 148 | 
 | 
|---|
 | 149 |     // de-activated copy constructor
 | 
|---|
 | 150 |     Cacheable(const Cacheable&);
 | 
|---|
| [9ad391] | 151 |   };
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 |   template<typename T>
 | 
|---|
 | 155 |   Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
 | 
|---|
 | 156 |     owner(_owner),
 | 
|---|
| [24a5e0] | 157 |     recalcMethod(_recalcMethod)
 | 
|---|
| [9ad391] | 158 |   {
 | 
|---|
| [052b8d0] | 159 |     // create all states needed for this object
 | 
|---|
 | 160 |     invalidState = state_ptr(new InvalidState(this));
 | 
|---|
 | 161 |     validState = state_ptr(new ValidState(this));
 | 
|---|
 | 162 |     state = invalidState;
 | 
|---|
| [314ff6] | 163 |     // we sign on with the best(=lowest) priority, so cached values are recalculated before
 | 
|---|
 | 164 |     // anybody else might ask for updated values
 | 
|---|
 | 165 |     owner->signOn(this,-20);
 | 
|---|
| [e3c8b4] | 166 |   }
 | 
|---|
| [9ad391] | 167 | 
 | 
|---|
| [052b8d0] | 168 |   // de-activated copy constructor
 | 
|---|
| [9ad391] | 169 |   template<typename T>
 | 
|---|
| [052b8d0] | 170 |   Cacheable<T>::Cacheable(const Cacheable&){
 | 
|---|
 | 171 |     ASSERT(0,"Cacheables should never be copied");
 | 
|---|
| [9ad391] | 172 |   }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |   template<typename T>
 | 
|---|
| [adc42b] | 175 |   const T Cacheable<T>::operator*() const{
 | 
|---|
 | 176 |     // we can only use the cacheable when the owner is not changing at the moment
 | 
|---|
 | 177 |     if(!owner->isBlocked()){
 | 
|---|
 | 178 |       return state->getValue();
 | 
|---|
 | 179 |     }
 | 
|---|
 | 180 |     else{
 | 
|---|
 | 181 |       return recalcMethod();
 | 
|---|
 | 182 |     }
 | 
|---|
| [9ad391] | 183 |   }
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |   template<typename T>
 | 
|---|
 | 186 |   Cacheable<T>::~Cacheable()
 | 
|---|
 | 187 |   {
 | 
|---|
 | 188 |     owner->signOff(this);
 | 
|---|
 | 189 |   }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |   template<typename T>
 | 
|---|
| [052b8d0] | 192 |   const bool Cacheable<T>::isValid() const{
 | 
|---|
 | 193 |     return state->isValid();
 | 
|---|
| [9ad391] | 194 |   }
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 |   template<typename T>
 | 
|---|
 | 197 |   void Cacheable<T>::update(Observable *subject) {
 | 
|---|
| [052b8d0] | 198 |     state->invalidate();
 | 
|---|
| [9ad391] | 199 |   }
 | 
|---|
 | 200 | 
 | 
|---|
 | 201 |   template<typename T>
 | 
|---|
 | 202 |   void Cacheable<T>::subjectKilled(Observable *subject) {
 | 
|---|
| [052b8d0] | 203 |     state_ptr destroyed = state_ptr(new DestroyedState(this));
 | 
|---|
 | 204 |     switchState(destroyed);
 | 
|---|
| [9ad391] | 205 |   }
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 |   template<typename T>
 | 
|---|
| [052b8d0] | 208 |   void Cacheable<T>::switchState(state_ptr newState){
 | 
|---|
 | 209 |     ASSERT(!state->isBusy(),"LOOP DETECTED: Cacheable state switched while recalculating.\nDid the recalculation trigger the Observable?");
 | 
|---|
 | 210 |     state = newState;
 | 
|---|
 | 211 |     state->enter();
 | 
|---|
| [9ad391] | 212 |   }
 | 
|---|
| [052b8d0] | 213 | 
 | 
|---|
| [9ad391] | 214 | #else
 | 
|---|
 | 215 |   template<typename T>
 | 
|---|
 | 216 |   class Cacheable : public Observer
 | 
|---|
 | 217 |   {
 | 
|---|
 | 218 |   public:
 | 
|---|
 | 219 |     Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
 | 
|---|
 | 220 |     virtual ~Cacheable();
 | 
|---|
 | 221 | 
 | 
|---|
| [052b8d0] | 222 |     const bool isValid() const;
 | 
|---|
| [adc42b] | 223 |     const T operator*() const;
 | 
|---|
| [9ad391] | 224 | 
 | 
|---|
 | 225 |     // methods implemented for base-class Observer
 | 
|---|
 | 226 |     void update(Observable *subject);
 | 
|---|
 | 227 |     void subjectKilled(Observable *subject);
 | 
|---|
 | 228 |   private:
 | 
|---|
 | 229 | 
 | 
|---|
 | 230 |     boost::function<T()> recalcMethod;
 | 
|---|
 | 231 |   };
 | 
|---|
 | 232 | 
 | 
|---|
 | 233 |   template<typename T>
 | 
|---|
 | 234 |   Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
 | 
|---|
 | 235 |     recalcMethod(_recalcMethod)
 | 
|---|
 | 236 |   {}
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |   template<typename T>
 | 
|---|
| [adc42b] | 239 |   const T Cacheable<T>::operator*() const{
 | 
|---|
| [9ad391] | 240 |     return recalcMethod();
 | 
|---|
 | 241 |   }
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 |   template<typename T>
 | 
|---|
 | 244 |   Cacheable<T>::~Cacheable()
 | 
|---|
 | 245 |   {}
 | 
|---|
 | 246 | 
 | 
|---|
 | 247 |   template<typename T>
 | 
|---|
| [052b8d0] | 248 |   const bool Cacheable<T>::isValid() const{
 | 
|---|
| [9ad391] | 249 |     return true;
 | 
|---|
 | 250 |   }
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 |   template<typename T>
 | 
|---|
 | 253 |   void Cacheable<T>::update(Observable *subject) {
 | 
|---|
| [052b8d0] | 254 |     ASSERT(0, "Cacheable::update should never be called when caching is disabled");
 | 
|---|
| [9ad391] | 255 |   }
 | 
|---|
 | 256 | 
 | 
|---|
 | 257 |   template<typename T>
 | 
|---|
| [052b8d0] | 258 |   void Cacheable<T>::subjectKilled(Observable *subject){
 | 
|---|
 | 259 |     ASSERT(0, "Cacheable::subjectKilled should never be called when caching is disabled");
 | 
|---|
| [9ad391] | 260 |   }
 | 
|---|
 | 261 | #endif
 | 
|---|
| [e3c8b4] | 262 | 
 | 
|---|
 | 263 | #endif /* CACHEABLE_HPP_ */
 | 
|---|