summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2008-12-13 09:26:03 +0000
committerMike Buland <eichlan@xagasoft.com>2008-12-13 09:26:03 +0000
commit5db154c3fb36a677c551e39c3c6661207eb51778 (patch)
tree3240993cb3bd2ef07dfc7f3b82956bf9b6173145
parent7c8e31fc057859d08610f17f669fdfcaf2fc2956 (diff)
downloadlibbu++-5db154c3fb36a677c551e39c3c6661207eb51778.tar.gz
libbu++-5db154c3fb36a677c551e39c3c6661207eb51778.tar.bz2
libbu++-5db154c3fb36a677c551e39c3c6661207eb51778.tar.xz
libbu++-5db154c3fb36a677c551e39c3c6661207eb51778.zip
The Calc should be functional, and is being called by the Cache itself. The
Bu::CacheCalc still needs to reference the Bu::Cache so that it can make the changes it needs to.
-rw-r--r--src/cache.h34
-rw-r--r--src/cachecalc.h6
2 files changed, 36 insertions, 4 deletions
diff --git a/src/cache.h b/src/cache.h
index 9488044..cc13fc8 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -5,6 +5,7 @@
5#include "bu/hash.h" 5#include "bu/hash.h"
6#include "bu/list.h" 6#include "bu/list.h"
7#include "bu/cachestore.h" 7#include "bu/cachestore.h"
8#include "bu/cachecalc.h"
8 9
9#define BU_TRACE 10#define BU_TRACE
10#include "bu/trace.h" 11#include "bu/trace.h"
@@ -20,6 +21,7 @@ namespace Bu
20 private: 21 private:
21 typedef Bu::CacheStore<obtype, keytype> Store; 22 typedef Bu::CacheStore<obtype, keytype> Store;
22 typedef Bu::List<Store *> StoreList; 23 typedef Bu::List<Store *> StoreList;
24 typedef Bu::CacheCalc<obtype, keytype> Calc;
23 25
24 typedef struct CacheEntry 26 typedef struct CacheEntry
25 { 27 {
@@ -30,7 +32,8 @@ namespace Bu
30 typedef Bu::Hash<keytype, CacheEntry> CidHash; 32 typedef Bu::Hash<keytype, CacheEntry> CidHash;
31 33
32 public: 34 public:
33 Cache() 35 Cache() :
36 pCalc( NULL )
34 { 37 {
35 TRACE(); 38 TRACE();
36 } 39 }
@@ -47,6 +50,10 @@ namespace Bu
47 __tracer_format( i.getKey() ); 50 __tracer_format( i.getKey() );
48 printf("!\n"); 51 printf("!\n");
49 } 52 }
53 if( pCalc ) pCalc->onUnload(
54 i.getValue().pData,
55 i.getKey()
56 );
50 lStore.first()->unload( 57 lStore.first()->unload(
51 i.getValue().pData, 58 i.getValue().pData,
52 i.getKey() 59 i.getKey()
@@ -71,6 +78,16 @@ namespace Bu
71 lStore.prepend( pHand ); 78 lStore.prepend( pHand );
72 } 79 }
73 80
81 void setCalc( Calc *pCalc )
82 {
83 TRACE();
84 if( this->pCalc )
85 {
86 delete this->pCalc;
87 }
88 this->pCalc = pCalc;
89 }
90
74 Ptr insert( obtype *pData ) 91 Ptr insert( obtype *pData )
75 { 92 {
76 TRACE( pData ); 93 TRACE( pData );
@@ -78,10 +95,12 @@ namespace Bu
78 keytype k = lStore.first()->create( pData ); 95 keytype k = lStore.first()->create( pData );
79 hEnt.insert( k, e ); 96 hEnt.insert( k, e );
80 97
98 if( pCalc ) pCalc->onLoad( pData, k );
99
81 return Ptr( *this, pData, k ); 100 return Ptr( *this, pData, k );
82 } 101 }
83 102
84 Ptr get( keytype cId ) 103 Ptr get( const keytype &cId )
85 { 104 {
86 TRACE( cId ); 105 TRACE( cId );
87 try { 106 try {
@@ -94,7 +113,12 @@ namespace Bu
94 } 113 }
95 } 114 }
96 115
97 void erase( keytype cId ) 116 int getRefCount( const keytype &cId )
117 {
118 return hEnt.get( cId ).iRefs;
119 }
120
121 void erase( const keytype &cId )
98 { 122 {
99 TRACE( cId ); 123 TRACE( cId );
100 try { 124 try {
@@ -107,6 +131,9 @@ namespace Bu
107 catch( Bu::HashException &e ) { 131 catch( Bu::HashException &e ) {
108 get( cId ); 132 get( cId );
109 } 133 }
134
135 if( pCalc ) pCalc->onUnload( hEnt.get( cId ).pData, cId );
136
110 lStore.first()->destroy( hEnt.get( cId ).pData, cId ); 137 lStore.first()->destroy( hEnt.get( cId ).pData, cId );
111 hEnt.erase( cId ); 138 hEnt.erase( cId );
112 } 139 }
@@ -134,6 +161,7 @@ namespace Bu
134 private: 161 private:
135 CidHash hEnt; 162 CidHash hEnt;
136 StoreList lStore; 163 StoreList lStore;
164 Calc *pCalc;
137 }; 165 };
138}; 166};
139 167
diff --git a/src/cachecalc.h b/src/cachecalc.h
index 9e83b06..289c4ac 100644
--- a/src/cachecalc.h
+++ b/src/cachecalc.h
@@ -3,7 +3,7 @@
3 3
4namespace Bu 4namespace Bu
5{ 5{
6 template<class type> 6 template<class obtype, class keytype>
7 class CacheCalc 7 class CacheCalc
8 { 8 {
9 public: 9 public:
@@ -15,6 +15,10 @@ namespace Bu
15 { 15 {
16 } 16 }
17 17
18 virtual void onLoad( obtype *pSrc, const keytype &key )=0;
19 virtual void onUnload( obtype *pSrc, const keytype &key )=0;
20 virtual void onTick() { };
21
18 private: 22 private:
19 }; 23 };
20}; 24};