From 350b052cfd866e3b3c7c4626551b49f8b75e55a1 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 1 Dec 2008 17:20:26 +0000 Subject: Woo! Very nearly there cache-wise, I'm about to change the name of the handler, do a few more tests, and hopefully get something loading/saving. --- src/cachable.cpp | 15 --------------- src/cachable.h | 28 ---------------------------- src/cache.h | 34 ++++++++++++++++------------------ src/cachehandler.cpp | 8 -------- src/cachehandler.h | 24 ++++++++++++++++++++---- src/cptr.h | 12 ++++++++++-- src/tests/cache.cpp | 52 +++++++++++++++++++++++++++++++++++++++++----------- 7 files changed, 87 insertions(+), 86 deletions(-) delete mode 100644 src/cachable.cpp delete mode 100644 src/cachable.h (limited to 'src') diff --git a/src/cachable.cpp b/src/cachable.cpp deleted file mode 100644 index 7fa2d23..0000000 --- a/src/cachable.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "bu/cachable.h" - -Bu::Cachable::Cachable() -{ -} - -Bu::Cachable::~Cachable() -{ -} - -template<> long Bu::getCacheId( const Bu::Cachable *o ) -{ - return o->getCacheId(); -} - diff --git a/src/cachable.h b/src/cachable.h deleted file mode 100644 index a54c55b..0000000 --- a/src/cachable.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef BU_CACHABLE_H -#define BU_CACHABLE_H - -namespace Bu -{ - class Cachable - { - public: - Cachable(); - virtual ~Cachable(); - - virtual long getCacheId() const =0; - }; - - template long getCacheId( const obtype *o ); - template<> long getCacheId( const Cachable *o ); -}; - -#define DECL_CACHABLE( name ) \ - namespace Bu { template<> long getCacheId( const name *o ); } - -#define DEF_CACHABLE( name ) \ - template<> long Bu::getCacheId( const name *o ) \ - { \ - return getCacheId( (Bu::Cachable *)o ); \ - } - -#endif diff --git a/src/cache.h b/src/cache.h index 8ab0659..f7b71ce 100644 --- a/src/cache.h +++ b/src/cache.h @@ -11,16 +11,15 @@ namespace Bu { - template class Cache; - template long getCacheId( const obtype *o ); - template class Cache { friend class Bu::CPtr; - typedef Bu::CPtr Ptr; + typedef Bu::CPtr Ptr; + typedef Bu::CacheHandler Handler; + typedef Bu::List HandlerList; public: - typedef long cid; /**< Cache ID type. Unique cache entry ID. */ + typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ public: Cache() @@ -31,19 +30,19 @@ namespace Bu virtual ~Cache() { TRACE(); - for( HandlerList::iterator i = lHandler.begin(); +/* for( HandlerList::iterator i = lHandler.begin(); i != lHandler.end(); i++ ) { delete *i; } - } +*/ } - void appendHandler( CacheHandler *pHand ) + void appendHandler( Handler *pHand ) { lHandler.append( pHand ); } - void prependHandler( CacheHandler *pHand ) + void prependHandler( Handler *pHand ) { lHandler.prepend( pHand ); } @@ -52,33 +51,33 @@ namespace Bu { TRACE(); CacheEntry e = {pData, 0}; - hEnt.insert( getCacheId( pData ), e ); + hEnt.insert( /*pData*/ 0 , e ); return Ptr( *this, pData ); } - Ptr get( cid cId ) + Ptr get( cid_t cId ) { TRACE(); return Ptr( *this, hEnt.get( cId ).pData ); } - int getRefCnt( cid cId ) + int getRefCnt( cid_t cId ) { TRACE(); return hEnt.get( cId ).iRefs; } private: - void incRef( obtype *pData ) + void incRef( cid_t cId ) { TRACE(); - hEnt.get( getCacheId( pData ) ).iRefs++; + hEnt.get( cId ).iRefs++; } - void decRef( obtype *pData ) + void decRef( cid_t cId ) { TRACE(); - CacheEntry &e = hEnt.get( getCacheId( pData ) ); + CacheEntry &e = hEnt.get( cId ); e.iRefs--; } @@ -90,10 +89,9 @@ namespace Bu } CacheEntry; //typedef Bu::Hash RefHash; - typedef Bu::Hash CidHash; + typedef Bu::Hash CidHash; //RefHash hRefs; CidHash hEnt; - typedef Bu::List HandlerList; HandlerList lHandler; }; }; diff --git a/src/cachehandler.cpp b/src/cachehandler.cpp index efd12e7..d208e98 100644 --- a/src/cachehandler.cpp +++ b/src/cachehandler.cpp @@ -1,10 +1,2 @@ #include "bu/cachehandler.h" -Bu::CacheHandler::CacheHandler() -{ -} - -Bu::CacheHandler::~CacheHandler() -{ -} - diff --git a/src/cachehandler.h b/src/cachehandler.h index 6ce1471..3b26970 100644 --- a/src/cachehandler.h +++ b/src/cachehandler.h @@ -1,15 +1,31 @@ #ifndef BU_CACHE_HANDLER_H #define BU_CACHE_HANDLER_H +#include "bu/cptr.h" + namespace Bu { + /** + * Handles I/O for data in the cache. This also assigns ID's to the newly + * created objects that are requested through this system. + */ + template class CacheHandler { public: - CacheHandler(); - virtual ~CacheHandler(); - virtual void load()=0; - virtual void unload()=0; + CacheHandler() + { + } + + virtual ~CacheHandler() + { + } + + virtual Bu::CPtr load()=0; + virtual void unload( Bu::CPtr pObj )=0; + virtual Bu::CPtr create()=0; + virtual Bu::CPtr create( T &rSrc )=0; + virtual void destroy( Bu::CPtr pObj )=0; private: }; diff --git a/src/cptr.h b/src/cptr.h index 5aafbd3..6e54fdd 100644 --- a/src/cptr.h +++ b/src/cptr.h @@ -5,22 +5,29 @@ 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; + public: + typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */ private: CPtr( Cache &rCache, obtype *pData ) : rCache( rCache ), pData( pData ) { - rCache.incRef( pData ); + rCache.incRef( cId ); } public: virtual ~CPtr() { - rCache.decRef( pData ); + rCache.decRef( cId ); } obtype &operator*() @@ -36,6 +43,7 @@ namespace Bu private: Bu::Cache &rCache; obtype *pData; + cid_t cId; }; }; diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index f05de57..55ec5be 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -1,9 +1,8 @@ #include #include "bu/cache.h" -#include "bu/cachable.h" -class Bob : public Bu::Cachable +class Bob { public: Bob() @@ -36,24 +35,55 @@ public: int iInt; }; -DECL_CACHABLE( Bob ); -DEF_CACHABLE( Bob ); +class BobHandler : public Bu::CacheHandler +{ +public: + BobHandler() : + cLastId( 0 ) + { + } + + ~BobHandler() + { + } + + virtual Bu::CPtr load() + { + } + + virtual void unload( Bu::CPtr pObj ) + { + } + + virtual Bu::CPtr create() + { + } + + virtual Bu::CPtr create( Bob &rSrc ) + { + } + + virtual void destroy( Bu::CPtr pObj ) + { + } + +private: + Bu::Cache::cid_t cLastId; +}; int main() { - Bu::Cache bobCache; + typedef Bu::Cache BobCache; + typedef Bu::CPtr BobPtr; - Bu::CPtr pB1 = bobCache.insert( new Bob() ); + BobCache bobCache; + BobPtr pB1 = bobCache.insert( new Bob() ); (*pB1).setInt( 44 ); - printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); - Bu::CPtr pB2 = bobCache.get( 0 ); - + BobPtr pB2 = bobCache.get( 0 ); printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); - printf("Int = %d\n", pB2->getInt() ); - } -- cgit v1.2.3