diff options
| -rw-r--r-- | src/cachable.h | 9 | ||||
| -rw-r--r-- | src/cache.h | 31 | ||||
| -rw-r--r-- | src/cachehandler.cpp | 8 | ||||
| -rw-r--r-- | src/cachehandler.h | 16 | ||||
| -rw-r--r-- | src/cachehandlernids.cpp | 0 | ||||
| -rw-r--r-- | src/cachehandlernids.h | 0 | ||||
| -rw-r--r-- | src/cptr.h | 10 | ||||
| -rw-r--r-- | src/tests/cache.cpp | 30 |
8 files changed, 91 insertions, 13 deletions
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 | |||
| 16 | template<> long getCacheId<Cachable>( const Cachable *o ); | 16 | template<> long getCacheId<Cachable>( const Cachable *o ); |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | #define DECL_CACHABLE( name ) \ | ||
| 20 | namespace Bu { template<> long getCacheId<name>( const name *o ); } | ||
| 21 | |||
| 22 | #define DEF_CACHABLE( name ) \ | ||
| 23 | template<> long Bu::getCacheId<name>( const name *o ) \ | ||
| 24 | { \ | ||
| 25 | return getCacheId<Bu::Cachable>( (Bu::Cachable *)o ); \ | ||
| 26 | } | ||
| 27 | |||
| 19 | #endif | 28 | #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 @@ | |||
| 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" | ||
| 7 | #include "bu/cachehandler.h" | ||
| 6 | 8 | ||
| 7 | #define BU_TRACE | 9 | #define BU_TRACE |
| 8 | #include "bu/trace.h" | 10 | #include "bu/trace.h" |
| @@ -29,6 +31,21 @@ namespace Bu | |||
| 29 | virtual ~Cache() | 31 | virtual ~Cache() |
| 30 | { | 32 | { |
| 31 | TRACE(); | 33 | TRACE(); |
| 34 | for( HandlerList::iterator i = lHandler.begin(); | ||
| 35 | i != lHandler.end(); i++ ) | ||
| 36 | { | ||
| 37 | delete *i; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | void appendHandler( CacheHandler *pHand ) | ||
| 42 | { | ||
| 43 | lHandler.append( pHand ); | ||
| 44 | } | ||
| 45 | |||
| 46 | void prependHandler( CacheHandler *pHand ) | ||
| 47 | { | ||
| 48 | lHandler.prepend( pHand ); | ||
| 32 | } | 49 | } |
| 33 | 50 | ||
| 34 | Ptr insert( obtype *pData ) | 51 | Ptr insert( obtype *pData ) |
| @@ -39,6 +56,18 @@ namespace Bu | |||
| 39 | return Ptr( *this, pData ); | 56 | return Ptr( *this, pData ); |
| 40 | } | 57 | } |
| 41 | 58 | ||
| 59 | Ptr get( cid cId ) | ||
| 60 | { | ||
| 61 | TRACE(); | ||
| 62 | return Ptr( *this, hEnt.get( cId ).pData ); | ||
| 63 | } | ||
| 64 | |||
| 65 | int getRefCnt( cid cId ) | ||
| 66 | { | ||
| 67 | TRACE(); | ||
| 68 | return hEnt.get( cId ).iRefs; | ||
| 69 | } | ||
| 70 | |||
| 42 | private: | 71 | private: |
| 43 | void incRef( obtype *pData ) | 72 | void incRef( obtype *pData ) |
| 44 | { | 73 | { |
| @@ -64,6 +93,8 @@ namespace Bu | |||
| 64 | typedef Bu::Hash<cid, CacheEntry> CidHash; | 93 | typedef Bu::Hash<cid, CacheEntry> CidHash; |
| 65 | //RefHash hRefs; | 94 | //RefHash hRefs; |
| 66 | CidHash hEnt; | 95 | CidHash hEnt; |
| 96 | typedef Bu::List<CacheHandler *> HandlerList; | ||
| 97 | HandlerList lHandler; | ||
| 67 | }; | 98 | }; |
| 68 | }; | 99 | }; |
| 69 | 100 | ||
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 @@ | |||
| 1 | #include "bu/cachehandler.h" | 1 | #include "bu/cachehandler.h" |
| 2 | 2 | ||
| 3 | Bu::CacheHandler::CacheHandler() | ||
| 4 | { | ||
| 5 | } | ||
| 6 | |||
| 7 | Bu::CacheHandler::~CacheHandler() | ||
| 8 | { | ||
| 9 | } | ||
| 10 | |||
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 @@ | |||
| 3 | 3 | ||
| 4 | namespace Bu | 4 | namespace Bu |
| 5 | { | 5 | { |
| 6 | template<class obtype> | ||
| 7 | class CacheHandler | 6 | class CacheHandler |
| 8 | { | 7 | { |
| 9 | public: | 8 | public: |
| 10 | CacheHandler() | 9 | CacheHandler(); |
| 11 | { | 10 | virtual ~CacheHandler(); |
| 12 | } | 11 | virtual void load()=0; |
| 13 | 12 | virtual void unload()=0; | |
| 14 | virtual ~CacheHandler() | ||
| 15 | { | ||
| 16 | } | ||
| 17 | |||
| 18 | void forceLoad() | ||
| 19 | { | ||
| 20 | } | ||
| 21 | 13 | ||
| 22 | private: | 14 | private: |
| 23 | }; | 15 | }; |
diff --git a/src/cachehandlernids.cpp b/src/cachehandlernids.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/cachehandlernids.cpp | |||
diff --git a/src/cachehandlernids.h b/src/cachehandlernids.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/cachehandlernids.h | |||
| @@ -23,6 +23,16 @@ namespace Bu | |||
| 23 | rCache.decRef( pData ); | 23 | rCache.decRef( pData ); |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | obtype &operator*() | ||
| 27 | { | ||
| 28 | return *pData; | ||
| 29 | } | ||
| 30 | |||
| 31 | obtype *operator->() | ||
| 32 | { | ||
| 33 | return pData; | ||
| 34 | } | ||
| 35 | |||
| 26 | private: | 36 | private: |
| 27 | Bu::Cache<obtype> &rCache; | 37 | Bu::Cache<obtype> &rCache; |
| 28 | obtype *pData; | 38 | 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 | |||
| 8 | public: | 8 | public: |
| 9 | Bob() | 9 | Bob() |
| 10 | { | 10 | { |
| 11 | TRACE(); | ||
| 11 | } | 12 | } |
| 12 | 13 | ||
| 13 | virtual ~Bob() | 14 | virtual ~Bob() |
| 14 | { | 15 | { |
| 16 | TRACE(); | ||
| 17 | } | ||
| 18 | |||
| 19 | void setInt( int i ) | ||
| 20 | { | ||
| 21 | TRACE(); | ||
| 22 | iInt = i; | ||
| 23 | } | ||
| 24 | |||
| 25 | int getInt() | ||
| 26 | { | ||
| 27 | return iInt; | ||
| 15 | } | 28 | } |
| 16 | 29 | ||
| 17 | long getCacheId() const | 30 | long getCacheId() const |
| 18 | { | 31 | { |
| 32 | TRACE(); | ||
| 19 | return 0; | 33 | return 0; |
| 20 | } | 34 | } |
| 21 | 35 | ||
| 22 | int iInt; | 36 | int iInt; |
| 23 | }; | 37 | }; |
| 24 | 38 | ||
| 39 | DECL_CACHABLE( Bob ); | ||
| 40 | DEF_CACHABLE( Bob ); | ||
| 41 | |||
| 25 | int main() | 42 | int main() |
| 26 | { | 43 | { |
| 27 | Bu::Cache<Bob> bobCache; | 44 | Bu::Cache<Bob> bobCache; |
| 28 | 45 | ||
| 29 | // Bu::CPtr<Bob> pB = bobCache.insert( new Bob() ); | 46 | Bu::CPtr<Bob> pB1 = bobCache.insert( new Bob() ); |
| 47 | |||
| 48 | (*pB1).setInt( 44 ); | ||
| 49 | |||
| 50 | printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); | ||
| 51 | |||
| 52 | Bu::CPtr<Bob> pB2 = bobCache.get( 0 ); | ||
| 53 | |||
| 54 | printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); | ||
| 55 | |||
| 56 | printf("Int = %d\n", pB2->getInt() ); | ||
| 57 | |||
| 30 | } | 58 | } |
| 31 | 59 | ||
