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