From 4bbeb6208ac33f71a09701cc5251f0412977946e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 10 Nov 2008 23:10:38 +0000 Subject: Ok, the cache-id officiation is being delegated to the CacheHandlers, this is just fine, since they can do it any way they want. The Congo CacheHandlers will all have to be specialized versions of the generic ones, but they'll use all the general functionality, just make up IDs differently. It'll rock. --- src/cachable.h | 9 +++++++++ src/cache.h | 31 +++++++++++++++++++++++++++++++ src/cachehandler.cpp | 8 ++++++++ src/cachehandler.h | 16 ++++------------ src/cachehandlernids.cpp | 0 src/cachehandlernids.h | 0 src/cptr.h | 10 ++++++++++ src/tests/cache.cpp | 30 +++++++++++++++++++++++++++++- 8 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 src/cachehandlernids.cpp create mode 100644 src/cachehandlernids.h diff --git a/src/cachable.h b/src/cachable.h index 529da6f..a54c55b 100644 --- a/src/cachable.h +++ b/src/cachable.h @@ -16,4 +16,13 @@ namespace Bu 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 344d1c6..8ab0659 100644 --- a/src/cache.h +++ b/src/cache.h @@ -3,6 +3,8 @@ #include "bu/cptr.h" #include "bu/hash.h" +#include "bu/list.h" +#include "bu/cachehandler.h" #define BU_TRACE #include "bu/trace.h" @@ -29,6 +31,21 @@ namespace Bu virtual ~Cache() { TRACE(); + for( HandlerList::iterator i = lHandler.begin(); + i != lHandler.end(); i++ ) + { + delete *i; + } + } + + void appendHandler( CacheHandler *pHand ) + { + lHandler.append( pHand ); + } + + void prependHandler( CacheHandler *pHand ) + { + lHandler.prepend( pHand ); } Ptr insert( obtype *pData ) @@ -39,6 +56,18 @@ namespace Bu return Ptr( *this, pData ); } + Ptr get( cid cId ) + { + TRACE(); + return Ptr( *this, hEnt.get( cId ).pData ); + } + + int getRefCnt( cid cId ) + { + TRACE(); + return hEnt.get( cId ).iRefs; + } + private: void incRef( obtype *pData ) { @@ -64,6 +93,8 @@ namespace Bu 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 d208e98..efd12e7 100644 --- a/src/cachehandler.cpp +++ b/src/cachehandler.cpp @@ -1,2 +1,10 @@ #include "bu/cachehandler.h" +Bu::CacheHandler::CacheHandler() +{ +} + +Bu::CacheHandler::~CacheHandler() +{ +} + diff --git a/src/cachehandler.h b/src/cachehandler.h index 72fb2b2..6ce1471 100644 --- a/src/cachehandler.h +++ b/src/cachehandler.h @@ -3,21 +3,13 @@ namespace Bu { - template class CacheHandler { public: - CacheHandler() - { - } - - virtual ~CacheHandler() - { - } - - void forceLoad() - { - } + CacheHandler(); + virtual ~CacheHandler(); + virtual void load()=0; + virtual void unload()=0; private: }; diff --git a/src/cachehandlernids.cpp b/src/cachehandlernids.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/cachehandlernids.h b/src/cachehandlernids.h new file mode 100644 index 0000000..e69de29 diff --git a/src/cptr.h b/src/cptr.h index 138ace8..5aafbd3 100644 --- a/src/cptr.h +++ b/src/cptr.h @@ -23,6 +23,16 @@ namespace Bu rCache.decRef( pData ); } + obtype &operator*() + { + return *pData; + } + + obtype *operator->() + { + return pData; + } + private: Bu::Cache &rCache; obtype *pData; diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index e51c31b..f05de57 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -8,24 +8,52 @@ class Bob : public Bu::Cachable public: Bob() { + TRACE(); } virtual ~Bob() { + TRACE(); + } + + void setInt( int i ) + { + TRACE(); + iInt = i; + } + + int getInt() + { + return iInt; } long getCacheId() const { + TRACE(); return 0; } int iInt; }; +DECL_CACHABLE( Bob ); +DEF_CACHABLE( Bob ); + int main() { Bu::Cache bobCache; -// Bu::CPtr pB = bobCache.insert( new Bob() ); + Bu::CPtr pB1 = bobCache.insert( new Bob() ); + + (*pB1).setInt( 44 ); + + printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); + + Bu::CPtr pB2 = bobCache.get( 0 ); + + printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); + + printf("Int = %d\n", pB2->getInt() ); + } -- cgit v1.2.3