aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-12-01 17:20:26 +0000
committerMike Buland <eichlan@xagasoft.com>2008-12-01 17:20:26 +0000
commit350b052cfd866e3b3c7c4626551b49f8b75e55a1 (patch)
treeb957f0dea70a7a2527ec78b32cdfc3e00712ff4d /src
parentc1229c50baa4816924e1914f3b4c78177749b73d (diff)
downloadlibbu++-350b052cfd866e3b3c7c4626551b49f8b75e55a1.tar.gz
libbu++-350b052cfd866e3b3c7c4626551b49f8b75e55a1.tar.bz2
libbu++-350b052cfd866e3b3c7c4626551b49f8b75e55a1.tar.xz
libbu++-350b052cfd866e3b3c7c4626551b49f8b75e55a1.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/cachable.cpp15
-rw-r--r--src/cachable.h28
-rw-r--r--src/cache.h34
-rw-r--r--src/cachehandler.cpp8
-rw-r--r--src/cachehandler.h24
-rw-r--r--src/cptr.h12
-rw-r--r--src/tests/cache.cpp52
7 files changed, 87 insertions, 86 deletions
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 @@
1#include "bu/cachable.h"
2
3Bu::Cachable::Cachable()
4{
5}
6
7Bu::Cachable::~Cachable()
8{
9}
10
11template<> long Bu::getCacheId<Bu::Cachable>( const Bu::Cachable *o )
12{
13 return o->getCacheId();
14}
15
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 @@
1#ifndef BU_CACHABLE_H
2#define BU_CACHABLE_H
3
4namespace Bu
5{
6 class Cachable
7 {
8 public:
9 Cachable();
10 virtual ~Cachable();
11
12 virtual long getCacheId() const =0;
13 };
14
15 template<class obtype> long getCacheId( const obtype *o );
16 template<> long getCacheId<Cachable>( const Cachable *o );
17};
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
28#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 @@
11 11
12namespace Bu 12namespace Bu
13{ 13{
14 template<class obtype> class Cache;
15 template<class obtype> long getCacheId( const obtype *o );
16
17 template<class obtype> 14 template<class obtype>
18 class Cache 15 class Cache
19 { 16 {
20 friend class Bu::CPtr<obtype>; 17 friend class Bu::CPtr<obtype>;
21 typedef Bu::CPtr<obtype> Ptr; 18 typedef Bu::CPtr<obtype> Ptr;
19 typedef Bu::CacheHandler<obtype> Handler;
20 typedef Bu::List<Handler *> HandlerList;
22 public: 21 public:
23 typedef long cid; /**< Cache ID type. Unique cache entry ID. */ 22 typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */
24 23
25 public: 24 public:
26 Cache() 25 Cache()
@@ -31,19 +30,19 @@ namespace Bu
31 virtual ~Cache() 30 virtual ~Cache()
32 { 31 {
33 TRACE(); 32 TRACE();
34 for( HandlerList::iterator i = lHandler.begin(); 33/* for( HandlerList::iterator i = lHandler.begin();
35 i != lHandler.end(); i++ ) 34 i != lHandler.end(); i++ )
36 { 35 {
37 delete *i; 36 delete *i;
38 } 37 }
39 } 38*/ }
40 39
41 void appendHandler( CacheHandler *pHand ) 40 void appendHandler( Handler *pHand )
42 { 41 {
43 lHandler.append( pHand ); 42 lHandler.append( pHand );
44 } 43 }
45 44
46 void prependHandler( CacheHandler *pHand ) 45 void prependHandler( Handler *pHand )
47 { 46 {
48 lHandler.prepend( pHand ); 47 lHandler.prepend( pHand );
49 } 48 }
@@ -52,33 +51,33 @@ namespace Bu
52 { 51 {
53 TRACE(); 52 TRACE();
54 CacheEntry e = {pData, 0}; 53 CacheEntry e = {pData, 0};
55 hEnt.insert( getCacheId( pData ), e ); 54 hEnt.insert( /*pData*/ 0 , e );
56 return Ptr( *this, pData ); 55 return Ptr( *this, pData );
57 } 56 }
58 57
59 Ptr get( cid cId ) 58 Ptr get( cid_t cId )
60 { 59 {
61 TRACE(); 60 TRACE();
62 return Ptr( *this, hEnt.get( cId ).pData ); 61 return Ptr( *this, hEnt.get( cId ).pData );
63 } 62 }
64 63
65 int getRefCnt( cid cId ) 64 int getRefCnt( cid_t cId )
66 { 65 {
67 TRACE(); 66 TRACE();
68 return hEnt.get( cId ).iRefs; 67 return hEnt.get( cId ).iRefs;
69 } 68 }
70 69
71 private: 70 private:
72 void incRef( obtype *pData ) 71 void incRef( cid_t cId )
73 { 72 {
74 TRACE(); 73 TRACE();
75 hEnt.get( getCacheId( pData ) ).iRefs++; 74 hEnt.get( cId ).iRefs++;
76 } 75 }
77 76
78 void decRef( obtype *pData ) 77 void decRef( cid_t cId )
79 { 78 {
80 TRACE(); 79 TRACE();
81 CacheEntry &e = hEnt.get( getCacheId( pData ) ); 80 CacheEntry &e = hEnt.get( cId );
82 e.iRefs--; 81 e.iRefs--;
83 } 82 }
84 83
@@ -90,10 +89,9 @@ namespace Bu
90 } CacheEntry; 89 } CacheEntry;
91 90
92 //typedef Bu::Hash<ptrdiff_t, int> RefHash; 91 //typedef Bu::Hash<ptrdiff_t, int> RefHash;
93 typedef Bu::Hash<cid, CacheEntry> CidHash; 92 typedef Bu::Hash<cid_t, CacheEntry> CidHash;
94 //RefHash hRefs; 93 //RefHash hRefs;
95 CidHash hEnt; 94 CidHash hEnt;
96 typedef Bu::List<CacheHandler *> HandlerList;
97 HandlerList lHandler; 95 HandlerList lHandler;
98 }; 96 };
99}; 97};
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 @@
1#include "bu/cachehandler.h" 1#include "bu/cachehandler.h"
2 2
3Bu::CacheHandler::CacheHandler()
4{
5}
6
7Bu::CacheHandler::~CacheHandler()
8{
9}
10
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 @@
1#ifndef BU_CACHE_HANDLER_H 1#ifndef BU_CACHE_HANDLER_H
2#define BU_CACHE_HANDLER_H 2#define BU_CACHE_HANDLER_H
3 3
4#include "bu/cptr.h"
5
4namespace Bu 6namespace Bu
5{ 7{
8 /**
9 * Handles I/O for data in the cache. This also assigns ID's to the newly
10 * created objects that are requested through this system.
11 */
12 template<class T>
6 class CacheHandler 13 class CacheHandler
7 { 14 {
8 public: 15 public:
9 CacheHandler(); 16 CacheHandler()
10 virtual ~CacheHandler(); 17 {
11 virtual void load()=0; 18 }
12 virtual void unload()=0; 19
20 virtual ~CacheHandler()
21 {
22 }
23
24 virtual Bu::CPtr<T> load()=0;
25 virtual void unload( Bu::CPtr<T> pObj )=0;
26 virtual Bu::CPtr<T> create()=0;
27 virtual Bu::CPtr<T> create( T &rSrc )=0;
28 virtual void destroy( Bu::CPtr<T> pObj )=0;
13 29
14 private: 30 private:
15 }; 31 };
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
5{ 5{
6 template<class obtype> class Cache; 6 template<class obtype> class Cache;
7 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 */
8 template<class obtype> 13 template<class obtype>
9 class CPtr 14 class CPtr
10 { 15 {
11 friend class Bu::Cache<obtype>; 16 friend class Bu::Cache<obtype>;
17 public:
18 typedef long cid_t; /**< Cache ID type. Unique cache entry ID. */
12 private: 19 private:
13 CPtr( Cache<obtype> &rCache, obtype *pData ) : 20 CPtr( Cache<obtype> &rCache, obtype *pData ) :
14 rCache( rCache ), 21 rCache( rCache ),
15 pData( pData ) 22 pData( pData )
16 { 23 {
17 rCache.incRef( pData ); 24 rCache.incRef( cId );
18 } 25 }
19 26
20 public: 27 public:
21 virtual ~CPtr() 28 virtual ~CPtr()
22 { 29 {
23 rCache.decRef( pData ); 30 rCache.decRef( cId );
24 } 31 }
25 32
26 obtype &operator*() 33 obtype &operator*()
@@ -36,6 +43,7 @@ namespace Bu
36 private: 43 private:
37 Bu::Cache<obtype> &rCache; 44 Bu::Cache<obtype> &rCache;
38 obtype *pData; 45 obtype *pData;
46 cid_t cId;
39 }; 47 };
40}; 48};
41 49
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 @@
1#include <stdio.h> 1#include <stdio.h>
2 2
3#include "bu/cache.h" 3#include "bu/cache.h"
4#include "bu/cachable.h"
5 4
6class Bob : public Bu::Cachable 5class Bob
7{ 6{
8public: 7public:
9 Bob() 8 Bob()
@@ -36,24 +35,55 @@ public:
36 int iInt; 35 int iInt;
37}; 36};
38 37
39DECL_CACHABLE( Bob ); 38class BobHandler : public Bu::CacheHandler<Bob>
40DEF_CACHABLE( Bob ); 39{
40public:
41 BobHandler() :
42 cLastId( 0 )
43 {
44 }
45
46 ~BobHandler()
47 {
48 }
49
50 virtual Bu::CPtr<Bob> load()
51 {
52 }
53
54 virtual void unload( Bu::CPtr<Bob> pObj )
55 {
56 }
57
58 virtual Bu::CPtr<Bob> create()
59 {
60 }
61
62 virtual Bu::CPtr<Bob> create( Bob &rSrc )
63 {
64 }
65
66 virtual void destroy( Bu::CPtr<Bob> pObj )
67 {
68 }
69
70private:
71 Bu::Cache<Bob>::cid_t cLastId;
72};
41 73
42int main() 74int main()
43{ 75{
44 Bu::Cache<Bob> bobCache; 76 typedef Bu::Cache<Bob> BobCache;
77 typedef Bu::CPtr<Bob> BobPtr;
45 78
46 Bu::CPtr<Bob> pB1 = bobCache.insert( new Bob() ); 79 BobCache bobCache;
47 80
81 BobPtr pB1 = bobCache.insert( new Bob() );
48 (*pB1).setInt( 44 ); 82 (*pB1).setInt( 44 );
49
50 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); 83 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) );
51 84
52 Bu::CPtr<Bob> pB2 = bobCache.get( 0 ); 85 BobPtr pB2 = bobCache.get( 0 );
53
54 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); 86 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) );
55
56 printf("Int = %d\n", pB2->getInt() ); 87 printf("Int = %d\n", pB2->getInt() );
57
58} 88}
59 89