diff options
Diffstat (limited to 'src/cache.h')
-rw-r--r-- | src/cache.h | 87 |
1 files changed, 81 insertions, 6 deletions
diff --git a/src/cache.h b/src/cache.h index 313d9fa..3ca1802 100644 --- a/src/cache.h +++ b/src/cache.h | |||
@@ -1,7 +1,7 @@ | |||
1 | #ifndef BU_CACHE_H | 1 | #ifndef BU_CACHE_H |
2 | #define BU_CACHE_H | 2 | #define BU_CACHE_H |
3 | 3 | ||
4 | #include "bu/cptr.h" | 4 | // #include "bu/cptr.h" |
5 | #include "bu/hash.h" | 5 | #include "bu/hash.h" |
6 | #include "bu/list.h" | 6 | #include "bu/list.h" |
7 | #include "bu/cachestore.h" | 7 | #include "bu/cachestore.h" |
@@ -14,9 +14,84 @@ namespace Bu | |||
14 | template<class obtype, class keytype> | 14 | template<class obtype, class keytype> |
15 | class Cache | 15 | class Cache |
16 | { | 16 | { |
17 | friend class Bu::CPtr<obtype, keytype>; | 17 | //friend class Bu::CPtr<obtype, keytype>; |
18 | public: | 18 | public: |
19 | typedef Bu::CPtr<obtype, keytype> Ptr; | 19 | // typedef Bu::CPtr<obtype, keytype> Ptr; |
20 | |||
21 | /** | ||
22 | * Cache Pointer - Provides access to data that is held within the | ||
23 | * cache. This provides safe, refcounting access to data stored in | ||
24 | * the cache, with support for lazy loading. | ||
25 | */ | ||
26 | //template<class obtype, class keytype> | ||
27 | class Ptr | ||
28 | { | ||
29 | friend class Bu::Cache<obtype, keytype>; | ||
30 | private: | ||
31 | Ptr( Cache<obtype, keytype> *pCache, obtype *pData, | ||
32 | const keytype &kId ) : | ||
33 | pCache( pCache ), | ||
34 | pData( pData ), | ||
35 | kId( kId ) | ||
36 | { | ||
37 | if( pCache ) | ||
38 | pCache->incRef( kId ); | ||
39 | } | ||
40 | |||
41 | public: | ||
42 | Ptr( const Ptr &rSrc ) : | ||
43 | pCache( rSrc.pCache ), | ||
44 | pData( rSrc.pData ), | ||
45 | kId( rSrc.kId ) | ||
46 | { | ||
47 | if( pCache ) | ||
48 | pCache->incRef( kId ); | ||
49 | } | ||
50 | |||
51 | Ptr() : | ||
52 | pCache( 0 ), | ||
53 | pData( 0 ) | ||
54 | { | ||
55 | } | ||
56 | |||
57 | virtual ~Ptr() | ||
58 | { | ||
59 | if( pCache ) | ||
60 | pCache->decRef( kId ); | ||
61 | } | ||
62 | |||
63 | obtype &operator*() | ||
64 | { | ||
65 | return *pData; | ||
66 | } | ||
67 | |||
68 | obtype *operator->() | ||
69 | { | ||
70 | return pData; | ||
71 | } | ||
72 | |||
73 | const keytype &getKey() | ||
74 | { | ||
75 | return kId; | ||
76 | } | ||
77 | |||
78 | Ptr &operator=( const Ptr &rRhs ) | ||
79 | { | ||
80 | if( pCache ) | ||
81 | pCache->decRef( kId ); | ||
82 | pCache = rRhs.pCache; | ||
83 | pData = rRhs.pData; | ||
84 | kId = rRhs.kId; | ||
85 | if( pCache ) | ||
86 | pCache->incRef( kId ); | ||
87 | } | ||
88 | |||
89 | private: | ||
90 | Bu::Cache<obtype, keytype> *pCache; | ||
91 | obtype *pData; | ||
92 | keytype kId; | ||
93 | }; | ||
94 | |||
20 | private: | 95 | private: |
21 | typedef Bu::CacheStore<obtype, keytype> Store; | 96 | typedef Bu::CacheStore<obtype, keytype> Store; |
22 | typedef Bu::List<Store *> StoreList; | 97 | typedef Bu::List<Store *> StoreList; |
@@ -87,20 +162,20 @@ namespace Bu | |||
87 | 162 | ||
88 | rCalc.onLoad( pData, k ); | 163 | rCalc.onLoad( pData, k ); |
89 | 164 | ||
90 | return Ptr( *this, pData, k ); | 165 | return Ptr( this, pData, k ); |
91 | } | 166 | } |
92 | 167 | ||
93 | Ptr get( const keytype &cId ) | 168 | Ptr get( const keytype &cId ) |
94 | { | 169 | { |
95 | TRACE( cId ); | 170 | TRACE( cId ); |
96 | try { | 171 | try { |
97 | return Ptr( *this, hEnt.get( cId ).pData, cId ); | 172 | return Ptr( this, hEnt.get( cId ).pData, cId ); |
98 | } | 173 | } |
99 | catch( Bu::HashException &e ) { | 174 | catch( Bu::HashException &e ) { |
100 | CacheEntry e = {lStore.first()->load( cId ), 0}; | 175 | CacheEntry e = {lStore.first()->load( cId ), 0}; |
101 | rCalc.onLoad( e.pData, cId ); | 176 | rCalc.onLoad( e.pData, cId ); |
102 | hEnt.insert( cId, e ); | 177 | hEnt.insert( cId, e ); |
103 | return Ptr( *this, e.pData, cId ); | 178 | return Ptr( this, e.pData, cId ); |
104 | } | 179 | } |
105 | } | 180 | } |
106 | 181 | ||