Cortex  10.0.0-a4
ComputationCache.h
1 //
3 // Copyright (c) 2013, 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_COMPUTATIONCACHE_H
36 #define IECORE_COMPUTATIONCACHE_H
37 
38 #include "boost/function.hpp"
39 
40 #include "IECore/LRUCache.h"
41 #include "IECore/ObjectPool.h"
42 
43 namespace IECore
44 {
45 
49 template< typename T >
51 {
52  public :
53 
55  typedef boost::function<IECore::ConstObjectPtr ( const T & )> ComputeFn;
56  typedef boost::function<IECore::MurmurHash ( const T & )> HashFn;
57 
58  IE_CORE_DECLAREMEMBERPTR( ComputationCache )
59 
60 
61  ComputationCache( ComputeFn computeFn, HashFn hashFn, size_t maxResults = 10000, ObjectPoolPtr objectPool = ObjectPool::defaultObjectPool() );
66 
67  ~ComputationCache() override;
68 
70  void clear();
71 
73  void erase( const T &args );
74 
76  size_t getMaxComputations() const;
77 
79  void setMaxComputations( size_t maxComputations );
80 
82  size_t cachedComputations() const;
83 
85  typedef enum {
86  ThrowIfMissing = 0,
87  NullIfMissing,
88  ComputeIfMissing
90 
96  ConstObjectPtr get( const T &args, MissingBehaviour missingBehaviour = ComputeIfMissing );
97 
99  void set( const T &args, const Object *obj, StoreMode storeMode );
100 
102  ObjectPool *objectPool() const;
103 
104  private :
105 
106  ComputeFn m_computeFn;
107  HashFn m_hashFn;
108 
110  Cache m_cache;
111 
112  ObjectPoolPtr m_objectPool;
113 
114  static MurmurHash cacheGetter( const MurmurHash &h, size_t &cost );
115 };
116 
117 
118 } // namespace IECore
119 
120 #include "IECore/ComputationCache.inl"
121 
122 #endif // IECORE_COMPUTATIONCACHE_H
123 
size_t getMaxComputations() const
Returns the maximum number of stored computations in the cache.
void clear()
Removes all the stored computation information from the cache.
Definition: ObjectPool.h:60
ObjectPool * objectPool() const
Returns the ObjectPool object used by this computation cache.
Definition: Object.h:104
void setMaxComputations(size_t maxComputations)
Defines the maximum number of stored computations allowed in the cache. May trigger deallocation...
void set(const T &args, const Object *obj, StoreMode storeMode)
Registers the result of a computation explicitly.
Definition: MurmurHash.h:64
Definition: ComputationCache.h:50
StoreMode
Enum used to specify how to store the pointer passed to the store() function.
Definition: ObjectPool.h:92
ComputationCache(ComputeFn computeFn, HashFn hashFn, size_t maxResults=10000, ObjectPoolPtr objectPool=ObjectPool::defaultObjectPool())
static ObjectPool * defaultObjectPool()
MissingBehaviour
Enum used to specify behavior when retrieving computation results from the cache. ...
Definition: ComputationCache.h:85
Definition: RefCounted.h:124
size_t cachedComputations() const
Returns the number of stored computations.
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43
void erase(const T &args)
Removes stored information about a specific computation result.