Cortex  10.0.0-a4
LRUCache.h
1 //
3 // Copyright (c) 2007-2014, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IECORE_LRUCACHE_H
36 #define IECORE_LRUCACHE_H
37 
38 #include "boost/noncopyable.hpp"
39 #include "boost/function.hpp"
40 #include "boost/variant.hpp"
41 
42 namespace IECore
43 {
44 
45 namespace LRUCachePolicy
46 {
47 
52 template<typename LRUCache>
53 class Serial;
54 
58 template<typename LRUCache>
59 class Parallel;
60 
61 } // namespace LRUCachePolicy
62 
81 template<typename Key, typename Value, template <typename> class Policy=LRUCachePolicy::Parallel, typename GetterKey=Key>
82 class LRUCache : private boost::noncopyable
83 {
84  public:
85 
86  typedef size_t Cost;
87  typedef Key KeyType;
88 
92  typedef boost::function<Value ( const GetterKey &key, Cost &cost )> GetterFunction;
94  typedef boost::function<void ( const Key &key, const Value &data )> RemovalCallback;
95 
96  LRUCache( GetterFunction getter );
97  LRUCache( GetterFunction getter, Cost maxCost );
98  LRUCache( GetterFunction getter, RemovalCallback removalCallback, Cost maxCost );
99  virtual ~LRUCache();
100 
106  Value get( const GetterKey &key );
107 
113  bool set( const Key &key, const Value &value, Cost cost );
114 
118  bool cached( const Key &key ) const;
119 
122  bool erase( const Key &key );
123 
127  void clear();
128 
131  void setMaxCost( Cost maxCost );
132 
134  Cost getMaxCost() const;
135 
137  Cost currentCost() const;
138 
139  private :
140 
141  // Data
143 
144  // Give Policy access to CacheEntry definitions.
145  friend class Policy<LRUCache>;
146 
147  // A function for computing values, and one for notifying of removals.
148  GetterFunction m_getter;
149  RemovalCallback m_removalCallback;
150 
151  // Status of each item in the cache.
152  enum Status
153  {
154  Uncached, // entry without valid value
155  Cached, // entry with valid value
156  Failed // m_getter failed when computing entry
157  };
158 
159  // The type used to store a single cached item.
160  struct CacheEntry
161  {
162  CacheEntry();
163 
164  // We use a boost::variant to compactly store
165  // a union of the data needed for each Status.
166  //
167  // - Uncached : A boost::blank instance
168  // - Cached : The Value itself
169  // - Failed ; The exception thrown by the GetterFn
170  typedef boost::variant<boost::blank, Value, std::exception_ptr> State;
171 
172  State state;
173  Cost cost; // the cost for this item
174 
175  Status status() const;
176 
177  };
178 
179  // Policy. This is responsible for
180  // the internal storage for the cache.
181  Policy<LRUCache> m_policy;
182 
183  Cost m_maxCost;
184 
185  // Methods
186  // =======
187 
188  // Updates the cached value and updates the current
189  // total cost.
190  bool setInternal( const Key &key, CacheEntry &cacheEntry, const Value &value, Cost cost );
191 
192  // Removes any cached value and updates the current total
193  // cost.
194  bool eraseInternal( const Key &key, CacheEntry &cacheEntry );
195 
196  // Removes items from the cache until the current cost is
197  // at or below the specified limit.
198  void limitCost( Cost cost );
199 
200  static void nullRemovalCallback( const Key &key, const Value &value );
201 
202 };
203 
204 } // namespace IECore
205 
206 #include "IECore/LRUCache.inl"
207 
208 #endif // IECORE_LRUCACHE_H
Definition: LRUCache.h:82
bool cached(const Key &key) const
bool erase(const Key &key)
void setMaxCost(Cost maxCost)
Cost currentCost() const
Returns the current cost of all cached items.
Cost getMaxCost() const
Returns the maximum cost.
bool set(const Key &key, const Value &value, Cost cost)
boost::function< Value(const GetterKey &key, Cost &cost)> GetterFunction
Definition: LRUCache.h:92
Definition: LRUCache.h:53
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43
boost::function< void(const Key &key, const Value &data)> RemovalCallback
The optional RemovalCallback is called whenever an item is discarded from the cache.
Definition: LRUCache.h:94
Definition: LRUCache.h:59