diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cache.h | 87 | ||||
| -rw-r--r-- | src/cptr.cpp | 1 | ||||
| -rw-r--r-- | src/cptr.h | 55 |
3 files changed, 81 insertions, 62 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 | ||
diff --git a/src/cptr.cpp b/src/cptr.cpp deleted file mode 100644 index 65d668b..0000000 --- a/src/cptr.cpp +++ /dev/null | |||
| @@ -1 +0,0 @@ | |||
| 1 | #include "bu/cptr.h" | ||
diff --git a/src/cptr.h b/src/cptr.h deleted file mode 100644 index 97f5e17..0000000 --- a/src/cptr.h +++ /dev/null | |||
| @@ -1,55 +0,0 @@ | |||
| 1 | #ifndef BU_C_PTR_H | ||
| 2 | #define BU_C_PTR_H | ||
| 3 | |||
| 4 | namespace Bu | ||
| 5 | { | ||
| 6 | template<class obtype, class keytype> class Cache; | ||
| 7 | |||
| 8 | /** | ||
| 9 | * Cache Pointer - Provides access to data that is held within the cache. | ||
| 10 | * This provides safe, refcounting access to data stored in the cache, with | ||
| 11 | * support for lazy loading. | ||
| 12 | */ | ||
| 13 | template<class obtype, class keytype> | ||
| 14 | class CPtr | ||
| 15 | { | ||
| 16 | friend class Bu::Cache<obtype, keytype>; | ||
| 17 | private: | ||
| 18 | CPtr( Cache<obtype, keytype> &rCache, obtype *pData, | ||
| 19 | const keytype &kId ) : | ||
| 20 | rCache( rCache ), | ||
| 21 | pData( pData ), | ||
| 22 | kId( kId ) | ||
| 23 | { | ||
| 24 | rCache.incRef( kId ); | ||
| 25 | } | ||
| 26 | |||
| 27 | public: | ||
| 28 | virtual ~CPtr() | ||
| 29 | { | ||
| 30 | rCache.decRef( kId ); | ||
| 31 | } | ||
| 32 | |||
| 33 | obtype &operator*() | ||
| 34 | { | ||
| 35 | return *pData; | ||
| 36 | } | ||
| 37 | |||
| 38 | obtype *operator->() | ||
| 39 | { | ||
| 40 | return pData; | ||
| 41 | } | ||
| 42 | |||
| 43 | const keytype &getKey() | ||
| 44 | { | ||
| 45 | return kId; | ||
| 46 | } | ||
| 47 | |||
| 48 | private: | ||
| 49 | Bu::Cache<obtype, keytype> &rCache; | ||
| 50 | obtype *pData; | ||
| 51 | keytype kId; | ||
| 52 | }; | ||
| 53 | }; | ||
| 54 | |||
| 55 | #endif | ||
