From d96fe229e79f9b1947cbd24ff52d6bf7bb9bf80d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 5 Jan 2009 15:58:22 +0000 Subject: I mergered Bu::CPtr into Bu::Cache as Bu::Cache::Ptr. This makes more sense to me, is much less messy, and makes the syntax work a little better for me as well. What the hell was a CPtr? Who knows, but a Cache::Ptr, that makes sense. Also, fewer includes to deal with now, just need Cache and you're set. Oh, also, made Cache::Ptr behave much more like a regular pointer, they can be assigned now, as well as created empty (NULL). --- src/cache.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/cptr.cpp | 1 - src/cptr.h | 55 -------------------------------------- 3 files changed, 81 insertions(+), 62 deletions(-) delete mode 100644 src/cptr.cpp delete mode 100644 src/cptr.h 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 @@ #ifndef BU_CACHE_H #define BU_CACHE_H -#include "bu/cptr.h" +// #include "bu/cptr.h" #include "bu/hash.h" #include "bu/list.h" #include "bu/cachestore.h" @@ -14,9 +14,84 @@ namespace Bu template class Cache { - friend class Bu::CPtr; + //friend class Bu::CPtr; public: - typedef Bu::CPtr Ptr; + // typedef Bu::CPtr Ptr; + + /** + * Cache Pointer - Provides access to data that is held within the + * cache. This provides safe, refcounting access to data stored in + * the cache, with support for lazy loading. + */ + //template + class Ptr + { + friend class Bu::Cache; + private: + Ptr( Cache *pCache, obtype *pData, + const keytype &kId ) : + pCache( pCache ), + pData( pData ), + kId( kId ) + { + if( pCache ) + pCache->incRef( kId ); + } + + public: + Ptr( const Ptr &rSrc ) : + pCache( rSrc.pCache ), + pData( rSrc.pData ), + kId( rSrc.kId ) + { + if( pCache ) + pCache->incRef( kId ); + } + + Ptr() : + pCache( 0 ), + pData( 0 ) + { + } + + virtual ~Ptr() + { + if( pCache ) + pCache->decRef( kId ); + } + + obtype &operator*() + { + return *pData; + } + + obtype *operator->() + { + return pData; + } + + const keytype &getKey() + { + return kId; + } + + Ptr &operator=( const Ptr &rRhs ) + { + if( pCache ) + pCache->decRef( kId ); + pCache = rRhs.pCache; + pData = rRhs.pData; + kId = rRhs.kId; + if( pCache ) + pCache->incRef( kId ); + } + + private: + Bu::Cache *pCache; + obtype *pData; + keytype kId; + }; + private: typedef Bu::CacheStore Store; typedef Bu::List StoreList; @@ -87,20 +162,20 @@ namespace Bu rCalc.onLoad( pData, k ); - return Ptr( *this, pData, k ); + return Ptr( this, pData, k ); } Ptr get( const keytype &cId ) { TRACE( cId ); try { - return Ptr( *this, hEnt.get( cId ).pData, cId ); + return Ptr( this, hEnt.get( cId ).pData, cId ); } catch( Bu::HashException &e ) { CacheEntry e = {lStore.first()->load( cId ), 0}; rCalc.onLoad( e.pData, cId ); hEnt.insert( cId, e ); - return Ptr( *this, e.pData, cId ); + return Ptr( this, e.pData, cId ); } } 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 @@ -#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 @@ -#ifndef BU_C_PTR_H -#define BU_C_PTR_H - -namespace Bu -{ - template class Cache; - - /** - * Cache Pointer - Provides access to data that is held within the cache. - * This provides safe, refcounting access to data stored in the cache, with - * support for lazy loading. - */ - template - class CPtr - { - friend class Bu::Cache; - private: - CPtr( Cache &rCache, obtype *pData, - const keytype &kId ) : - rCache( rCache ), - pData( pData ), - kId( kId ) - { - rCache.incRef( kId ); - } - - public: - virtual ~CPtr() - { - rCache.decRef( kId ); - } - - obtype &operator*() - { - return *pData; - } - - obtype *operator->() - { - return pData; - } - - const keytype &getKey() - { - return kId; - } - - private: - Bu::Cache &rCache; - obtype *pData; - keytype kId; - }; -}; - -#endif -- cgit v1.2.3