summaryrefslogtreecommitdiff
path: root/src/tests/cache.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-12-01 23:10:44 +0000
committerMike Buland <eichlan@xagasoft.com>2008-12-01 23:10:44 +0000
commit6f3b85c5af1855e1695885fb28220c34f6a0673f (patch)
tree07fc5d4f955bc6c58eb7ad88bcce7a8294c53158 /src/tests/cache.cpp
parent350b052cfd866e3b3c7c4626551b49f8b75e55a1 (diff)
downloadlibbu++-6f3b85c5af1855e1695885fb28220c34f6a0673f.tar.gz
libbu++-6f3b85c5af1855e1695885fb28220c34f6a0673f.tar.bz2
libbu++-6f3b85c5af1855e1695885fb28220c34f6a0673f.tar.xz
libbu++-6f3b85c5af1855e1695885fb28220c34f6a0673f.zip
Wow, that's a lot of changes. You can use anything as a key now, as long as it
can be hashed. And we're about to test actually loading and saving persistant cache items. Fun.
Diffstat (limited to 'src/tests/cache.cpp')
-rw-r--r--src/tests/cache.cpp66
1 files changed, 44 insertions, 22 deletions
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp
index 55ec5be..1cc008a 100644
--- a/src/tests/cache.cpp
+++ b/src/tests/cache.cpp
@@ -1,4 +1,7 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <sys/stat.h>
3#include <sys/types.h>
4#include <errno.h>
2 5
3#include "bu/cache.h" 6#include "bu/cache.h"
4 7
@@ -35,55 +38,74 @@ public:
35 int iInt; 38 int iInt;
36}; 39};
37 40
38class BobHandler : public Bu::CacheHandler<Bob> 41class BobStore : public Bu::CacheStore<Bob, long>
39{ 42{
40public: 43public:
41 BobHandler() : 44 BobStore() :
42 cLastId( 0 ) 45 cLastId( 0 )
43 { 46 {
47 TRACE();
44 } 48 }
45 49
46 ~BobHandler() 50 ~BobStore()
47 {
48 }
49
50 virtual Bu::CPtr<Bob> load()
51 { 51 {
52 TRACE();
52 } 53 }
53 54
54 virtual void unload( Bu::CPtr<Bob> pObj ) 55 virtual Bob *load( const long &key )
55 { 56 {
57 TRACE();
58 return NULL;
56 } 59 }
57 60
58 virtual Bu::CPtr<Bob> create() 61 virtual void unload( Bob *pObj )
59 { 62 {
63 TRACE();
64 delete pObj;
60 } 65 }
61 66
62 virtual Bu::CPtr<Bob> create( Bob &rSrc ) 67 virtual long create( Bob *rSrc )
63 { 68 {
69 TRACE();
70 return ++cLastId;
64 } 71 }
65 72
66 virtual void destroy( Bu::CPtr<Bob> pObj ) 73 virtual void destroy( Bob *pObj, const long &key )
67 { 74 {
75 TRACE();
76 delete pObj;
68 } 77 }
69 78
70private: 79private:
71 Bu::Cache<Bob>::cid_t cLastId; 80 long cLastId;
72}; 81};
73 82
74int main() 83int main( int argc, char *argv[] )
75{ 84{
76 typedef Bu::Cache<Bob> BobCache; 85 TRACE();
77 typedef Bu::CPtr<Bob> BobPtr; 86 if( argc < 3 )
87 {
88 printf("Try: %s [icufd] [<id/value>]\n\n", argv[0] );
89 return 0;
90 }
91
92 switch( argv[1][0] )
93 {
94 case 'i':
95 mkdir("bobcache", 0755 );
96 printf("Initialized cache: %s\n", strerror( errno ) );
97 return 0;
98
99 case 'c':
100 typedef Bu::Cache<Bob, long> BobCache;
101 typedef BobCache::Ptr BobPtr;
78 102
79 BobCache bobCache; 103 BobCache cBob;
80 104
81 BobPtr pB1 = bobCache.insert( new Bob() ); 105 cBob.appendStore( new BobStore() );
82 (*pB1).setInt( 44 ); 106
83 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) ); 107 return 0;
108 }
84 109
85 BobPtr pB2 = bobCache.get( 0 );
86 printf("RefCnt = %d\n", bobCache.getRefCnt( 0 ) );
87 printf("Int = %d\n", pB2->getInt() );
88} 110}
89 111