[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>
|
---|
| 13 |
|
---|
[9ad391] | 14 | #ifndef NO_CACHING
|
---|
| 15 |
|
---|
| 16 | template<typename T>
|
---|
| 17 | class Cacheable : public Observer
|
---|
| 18 | {
|
---|
| 19 | public:
|
---|
| 20 | Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
|
---|
| 21 | virtual ~Cacheable();
|
---|
| 22 |
|
---|
| 23 | const bool isValid();
|
---|
| 24 | const T& operator*();
|
---|
| 25 | const bool operator==(const T&);
|
---|
| 26 | const bool operator!=(const T&);
|
---|
| 27 |
|
---|
| 28 | // methods implemented for base-class Observer
|
---|
| 29 | void update(Observable *subject);
|
---|
| 30 | void subjectKilled(Observable *subject);
|
---|
| 31 | private:
|
---|
| 32 | void checkValid();
|
---|
| 33 |
|
---|
| 34 | T content;
|
---|
| 35 | Observable *owner;
|
---|
| 36 | bool valid;
|
---|
| 37 | bool canBeUsed;
|
---|
| 38 | boost::function<T()> recalcMethod;
|
---|
| 39 | };
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | template<typename T>
|
---|
| 43 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
| 44 | owner(_owner),
|
---|
| 45 | recalcMethod(_recalcMethod),
|
---|
| 46 | valid(false),
|
---|
| 47 | canBeUsed(true)
|
---|
| 48 | {
|
---|
| 49 | owner->signOn(this);
|
---|
[e3c8b4] | 50 | }
|
---|
[9ad391] | 51 |
|
---|
| 52 | template<typename T>
|
---|
| 53 | const T& Cacheable<T>::operator*(){
|
---|
| 54 | checkValid();
|
---|
| 55 | return content;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | template<typename T>
|
---|
| 59 | const bool Cacheable<T>::operator==(const T& rval){
|
---|
| 60 | checkValid();
|
---|
| 61 | return (content == rval);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | template<typename T>
|
---|
| 65 | const bool Cacheable<T>::operator!=(const T& rval){
|
---|
| 66 | checkValid();
|
---|
| 67 | return (content != rval);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | template<typename T>
|
---|
| 71 | Cacheable<T>::~Cacheable()
|
---|
| 72 | {
|
---|
| 73 | owner->signOff(this);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | template<typename T>
|
---|
| 77 | const bool Cacheable<T>::isValid(){
|
---|
| 78 | return valid;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | template<typename T>
|
---|
| 82 | void Cacheable<T>::update(Observable *subject) {
|
---|
| 83 | valid = false;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | template<typename T>
|
---|
| 87 | void Cacheable<T>::subjectKilled(Observable *subject) {
|
---|
| 88 | valid = false;
|
---|
| 89 | canBeUsed = false;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | template<typename T>
|
---|
| 93 | void Cacheable<T>::checkValid(){
|
---|
| 94 | assert(canBeUsed && "Cacheable used after owner was deleted");
|
---|
| 95 | if(!isValid()){
|
---|
| 96 | content = recalcMethod();
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | #else
|
---|
| 100 | template<typename T>
|
---|
| 101 | class Cacheable : public Observer
|
---|
| 102 | {
|
---|
| 103 | public:
|
---|
| 104 | Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
|
---|
| 105 | virtual ~Cacheable();
|
---|
| 106 |
|
---|
| 107 | const bool isValid();
|
---|
| 108 | const T& operator*();
|
---|
| 109 | const bool operator==(const T&);
|
---|
| 110 | const bool operator!=(const T&);
|
---|
| 111 |
|
---|
| 112 | // methods implemented for base-class Observer
|
---|
| 113 | void update(Observable *subject);
|
---|
| 114 | void subjectKilled(Observable *subject);
|
---|
| 115 | private:
|
---|
| 116 |
|
---|
| 117 | boost::function<T()> recalcMethod;
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | template<typename T>
|
---|
| 121 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
| 122 | recalcMethod(_recalcMethod)
|
---|
| 123 | {}
|
---|
| 124 |
|
---|
| 125 | template<typename T>
|
---|
| 126 | const T& Cacheable<T>::operator*(){
|
---|
| 127 | return recalcMethod();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | template<typename T>
|
---|
| 131 | const bool Cacheable<T>::operator==(const T& rval){
|
---|
| 132 | return (recalcMethod() == rval);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | template<typename T>
|
---|
| 136 | const bool Cacheable<T>::operator!=(const T& rval){
|
---|
| 137 | return (recalcMethod() != rval);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | template<typename T>
|
---|
| 141 | Cacheable<T>::~Cacheable()
|
---|
| 142 | {}
|
---|
| 143 |
|
---|
| 144 | template<typename T>
|
---|
| 145 | const bool Cacheable<T>::isValid(){
|
---|
| 146 | return true;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | template<typename T>
|
---|
| 150 | void Cacheable<T>::update(Observable *subject) {
|
---|
| 151 | assert(0 && "Cacheable::update should never be called when caching is disabled");
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | template<typename T>
|
---|
| 155 | void Cacheable<T>::subjectKilled(Observable *subject) {
|
---|
| 156 | assert(0 && "Cacheable::subjectKilled should never be called when caching is disabled");
|
---|
| 157 | }
|
---|
| 158 | #endif
|
---|
[e3c8b4] | 159 |
|
---|
| 160 | #endif /* CACHEABLE_HPP_ */
|
---|