[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 | #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 | {
|
---|
[e0b6fd] | 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) :
|
---|
[760ef4] | 27 | busy(false),
|
---|
| 28 | owner(_owner)
|
---|
[e0b6fd] | 29 | {}
|
---|
| 30 | virtual T& getValue()=0;
|
---|
| 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 |
|
---|
| 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 | }
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | class ValidState : public State{
|
---|
| 69 | public:
|
---|
| 70 | ValidState(Cacheable *_owner) :
|
---|
| 71 | State(_owner)
|
---|
| 72 | {}
|
---|
| 73 |
|
---|
| 74 | virtual T& getValue(){
|
---|
| 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 |
|
---|
| 102 | virtual T& getValue(){
|
---|
| 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 |
|
---|
[ea7176] | 129 | const bool isValid() const;
|
---|
[c72112] | 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:
|
---|
[e0b6fd] | 136 | void switchState(state_ptr newState);
|
---|
| 137 |
|
---|
| 138 | mutable state_ptr state;
|
---|
| 139 | // pre-defined state so we don't have to construct to much
|
---|
| 140 | state_ptr invalidState;
|
---|
| 141 | state_ptr validState;
|
---|
| 142 | // destroyed state is not predefined, because we rarely enter that state and never leave
|
---|
[9ad391] | 143 |
|
---|
| 144 | Observable *owner;
|
---|
| 145 | boost::function<T()> recalcMethod;
|
---|
[e0b6fd] | 146 |
|
---|
| 147 | // de-activated copy constructor
|
---|
| 148 | Cacheable(const Cacheable&);
|
---|
[9ad391] | 149 | };
|
---|
| 150 |
|
---|
| 151 |
|
---|
| 152 | template<typename T>
|
---|
| 153 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
| 154 | owner(_owner),
|
---|
[24a5e0] | 155 | recalcMethod(_recalcMethod)
|
---|
[9ad391] | 156 | {
|
---|
[e0b6fd] | 157 | // create all states needed for this object
|
---|
| 158 | invalidState = state_ptr(new InvalidState(this));
|
---|
| 159 | validState = state_ptr(new ValidState(this));
|
---|
| 160 | state = invalidState;
|
---|
[314ff6] | 161 | // we sign on with the best(=lowest) priority, so cached values are recalculated before
|
---|
| 162 | // anybody else might ask for updated values
|
---|
| 163 | owner->signOn(this,-20);
|
---|
[e3c8b4] | 164 | }
|
---|
[9ad391] | 165 |
|
---|
[e0b6fd] | 166 | // de-activated copy constructor
|
---|
| 167 | template<typename T>
|
---|
| 168 | Cacheable<T>::Cacheable(const Cacheable&){
|
---|
| 169 | ASSERT(0,"Cacheables should never be copied");
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[9ad391] | 172 | template<typename T>
|
---|
[c72112] | 173 | const T Cacheable<T>::operator*() const{
|
---|
[760ef4] | 174 | // we can only use the cacheable when the owner is not changing at the moment
|
---|
| 175 | if(!owner->isBlocked()){
|
---|
| 176 | return state->getValue();
|
---|
| 177 | }
|
---|
| 178 | else{
|
---|
| 179 | return recalcMethod();
|
---|
| 180 | }
|
---|
[9ad391] | 181 | }
|
---|
| 182 |
|
---|
| 183 | template<typename T>
|
---|
| 184 | Cacheable<T>::~Cacheable()
|
---|
| 185 | {
|
---|
| 186 | owner->signOff(this);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | template<typename T>
|
---|
[ea7176] | 190 | const bool Cacheable<T>::isValid() const{
|
---|
[e0b6fd] | 191 | return state->isValid();
|
---|
[9ad391] | 192 | }
|
---|
| 193 |
|
---|
| 194 | template<typename T>
|
---|
| 195 | void Cacheable<T>::update(Observable *subject) {
|
---|
[e0b6fd] | 196 | state->invalidate();
|
---|
[9ad391] | 197 | }
|
---|
| 198 |
|
---|
| 199 | template<typename T>
|
---|
| 200 | void Cacheable<T>::subjectKilled(Observable *subject) {
|
---|
[e0b6fd] | 201 | state_ptr destroyed = state_ptr(new DestroyedState(this));
|
---|
| 202 | switchState(destroyed);
|
---|
[9ad391] | 203 | }
|
---|
| 204 |
|
---|
| 205 | template<typename T>
|
---|
[e0b6fd] | 206 | void Cacheable<T>::switchState(state_ptr newState){
|
---|
| 207 | ASSERT(!state->isBusy(),"LOOP DETECTED: Cacheable state switched while recalculating.\nDid the recalculation trigger the Observable?");
|
---|
| 208 | state = newState;
|
---|
| 209 | state->enter();
|
---|
[9ad391] | 210 | }
|
---|
[e0b6fd] | 211 |
|
---|
[9ad391] | 212 | #else
|
---|
| 213 | template<typename T>
|
---|
| 214 | class Cacheable : public Observer
|
---|
| 215 | {
|
---|
| 216 | public:
|
---|
| 217 | Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
|
---|
| 218 | virtual ~Cacheable();
|
---|
| 219 |
|
---|
[ea7176] | 220 | const bool isValid() const;
|
---|
| 221 | const T& operator*() const;
|
---|
[9ad391] | 222 |
|
---|
| 223 | // methods implemented for base-class Observer
|
---|
| 224 | void update(Observable *subject);
|
---|
| 225 | void subjectKilled(Observable *subject);
|
---|
| 226 | private:
|
---|
| 227 |
|
---|
| 228 | boost::function<T()> recalcMethod;
|
---|
| 229 | };
|
---|
| 230 |
|
---|
| 231 | template<typename T>
|
---|
| 232 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
| 233 | recalcMethod(_recalcMethod)
|
---|
| 234 | {}
|
---|
| 235 |
|
---|
| 236 | template<typename T>
|
---|
[ea7176] | 237 | const T& Cacheable<T>::operator*() const{
|
---|
[9ad391] | 238 | return recalcMethod();
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | template<typename T>
|
---|
| 242 | Cacheable<T>::~Cacheable()
|
---|
| 243 | {}
|
---|
| 244 |
|
---|
| 245 | template<typename T>
|
---|
[ea7176] | 246 | const bool Cacheable<T>::isValid() const{
|
---|
[9ad391] | 247 | return true;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | template<typename T>
|
---|
| 251 | void Cacheable<T>::update(Observable *subject) {
|
---|
[e0b6fd] | 252 | ASSERT(0, "Cacheable::update should never be called when caching is disabled");
|
---|
[9ad391] | 253 | }
|
---|
| 254 |
|
---|
| 255 | template<typename T>
|
---|
[ea7176] | 256 | void Cacheable<T>::subjectKilled(Observable *subject){
|
---|
[e0b6fd] | 257 | ASSERT(0, "Cacheable::subjectKilled should never be called when caching is disabled");
|
---|
[9ad391] | 258 | }
|
---|
| 259 | #endif
|
---|
[e3c8b4] | 260 |
|
---|
| 261 | #endif /* CACHEABLE_HPP_ */
|
---|