diff options
Diffstat (limited to 'src/tests/cache.cpp')
-rw-r--r-- | src/tests/cache.cpp | 66 |
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 | ||
38 | class BobHandler : public Bu::CacheHandler<Bob> | 41 | class BobStore : public Bu::CacheStore<Bob, long> |
39 | { | 42 | { |
40 | public: | 43 | public: |
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 | ||
70 | private: | 79 | private: |
71 | Bu::Cache<Bob>::cid_t cLastId; | 80 | long cLastId; |
72 | }; | 81 | }; |
73 | 82 | ||
74 | int main() | 83 | int 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 | ||