aboutsummaryrefslogtreecommitdiff
path: root/src/cache.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/cache.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/cache.cpp b/src/cache.cpp
new file mode 100644
index 0000000..43e69dc
--- /dev/null
+++ b/src/cache.cpp
@@ -0,0 +1,78 @@
1#include "cache.h"
2#include "serializer.h"
3
4Cache::Cache()
5{
6}
7
8Cache::~Cache()
9{
10 for( std::map<std::string, Entry *>::iterator i = mCache.begin();
11 i != mCache.end(); i++ )
12 {
13 delete (*i).second;
14 }
15}
16
17void Cache::serialize( class Serializer &ar )
18{
19 if( ar.isLoading() )
20 {
21 int sCache, sData;
22 ar >> sCache;
23 std::string sTmp;
24
25 for( int i = 0; i < sCache; i++ )
26 {
27 Entry *e = new Entry;
28 ar >> e->tCreated;
29 ar >> sData;
30 std::list<std::string> &lData = e->lData;
31 for( int j = 0; j < sData; j++ )
32 {
33 ar >> sTmp;
34 lData.push_back( sTmp );
35 }
36 ar >> sTmp;
37 mCache[sTmp] = e;
38 }
39 }
40 else
41 {
42 ar << mCache.size();
43 for( std::map<std::string, Entry *>::iterator i = mCache.begin();
44 i != mCache.end(); i++ )
45 {
46 ar << (*i).second->tCreated;
47 std::list<std::string> &lData = (*i).second->lData;
48 ar << lData.size();
49 for( std::list<std::string>::iterator j = lData.begin();
50 j != lData.end(); j++ )
51 {
52 ar << (*j);
53 }
54
55 std::string str = (*i).first;
56 ar << str;
57 }
58 }
59}
60
61Cache::Entry *Cache::get( const std::string &id )
62{
63 std::map<std::string, Entry *>::iterator i = mCache.find( id );
64 if( i != mCache.end() )
65 return (*i).second;
66
67 return NULL;
68}
69
70void Cache::put( const std::string &id, Entry *data )
71{
72 std::map<std::string, Entry *>::iterator i = mCache.find( id );
73 if( i != mCache.end() )
74 delete (*i).second;
75
76 mCache[id] = data;
77}
78