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 |
|
---|
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() const;
|
---|
24 | const T& operator*() const;
|
---|
25 |
|
---|
26 | // methods implemented for base-class Observer
|
---|
27 | void update(Observable *subject);
|
---|
28 | void subjectKilled(Observable *subject);
|
---|
29 | private:
|
---|
30 | void checkValid() const;
|
---|
31 |
|
---|
32 | mutable T content;
|
---|
33 | Observable *owner;
|
---|
34 | mutable bool valid;
|
---|
35 | mutable bool canBeUsed;
|
---|
36 | boost::function<T()> recalcMethod;
|
---|
37 | };
|
---|
38 |
|
---|
39 |
|
---|
40 | template<typename T>
|
---|
41 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
42 | owner(_owner),
|
---|
43 | valid(false),
|
---|
44 | canBeUsed(true),
|
---|
45 | recalcMethod(_recalcMethod)
|
---|
46 | {
|
---|
47 | // we sign on with the best(=lowest) priority, so cached values are recalculated before
|
---|
48 | // anybody else might ask for updated values
|
---|
49 | owner->signOn(this,-20);
|
---|
50 | }
|
---|
51 |
|
---|
52 | template<typename T>
|
---|
53 | const T& Cacheable<T>::operator*() const{
|
---|
54 | checkValid();
|
---|
55 | return content;
|
---|
56 | }
|
---|
57 |
|
---|
58 | template<typename T>
|
---|
59 | Cacheable<T>::~Cacheable()
|
---|
60 | {
|
---|
61 | owner->signOff(this);
|
---|
62 | }
|
---|
63 |
|
---|
64 | template<typename T>
|
---|
65 | const bool Cacheable<T>::isValid() const{
|
---|
66 | return valid;
|
---|
67 | }
|
---|
68 |
|
---|
69 | template<typename T>
|
---|
70 | void Cacheable<T>::update(Observable *subject) {
|
---|
71 | valid = false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | template<typename T>
|
---|
75 | void Cacheable<T>::subjectKilled(Observable *subject) {
|
---|
76 | valid = false;
|
---|
77 | canBeUsed = false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | template<typename T>
|
---|
81 | void Cacheable<T>::checkValid() const{
|
---|
82 | assert(canBeUsed && "Cacheable used after owner was deleted");
|
---|
83 | if(!isValid()){
|
---|
84 | content = recalcMethod();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | #else
|
---|
88 | template<typename T>
|
---|
89 | class Cacheable : public Observer
|
---|
90 | {
|
---|
91 | public:
|
---|
92 | Cacheable(Observable *_owner, boost::function<T()> _recalcMethod);
|
---|
93 | virtual ~Cacheable();
|
---|
94 |
|
---|
95 | const bool isValid() const;
|
---|
96 | const T& operator*() const;
|
---|
97 |
|
---|
98 | // methods implemented for base-class Observer
|
---|
99 | void update(Observable *subject);
|
---|
100 | void subjectKilled(Observable *subject);
|
---|
101 | private:
|
---|
102 |
|
---|
103 | boost::function<T()> recalcMethod;
|
---|
104 | };
|
---|
105 |
|
---|
106 | template<typename T>
|
---|
107 | Cacheable<T>::Cacheable(Observable *_owner, boost::function<T()> _recalcMethod) :
|
---|
108 | recalcMethod(_recalcMethod)
|
---|
109 | {}
|
---|
110 |
|
---|
111 | template<typename T>
|
---|
112 | const T& Cacheable<T>::operator*() const{
|
---|
113 | return recalcMethod();
|
---|
114 | }
|
---|
115 |
|
---|
116 | template<typename T>
|
---|
117 | Cacheable<T>::~Cacheable()
|
---|
118 | {}
|
---|
119 |
|
---|
120 | template<typename T>
|
---|
121 | const bool Cacheable<T>::isValid() const{
|
---|
122 | return true;
|
---|
123 | }
|
---|
124 |
|
---|
125 | template<typename T>
|
---|
126 | void Cacheable<T>::update(Observable *subject) {
|
---|
127 | assert(0 && "Cacheable::update should never be called when caching is disabled");
|
---|
128 | }
|
---|
129 |
|
---|
130 | template<typename T>
|
---|
131 | void Cacheable<T>::subjectKilled(Observable *subject){
|
---|
132 | assert(0 && "Cacheable::subjectKilled should never be called when caching is disabled");
|
---|
133 | }
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | #endif /* CACHEABLE_HPP_ */
|
---|