aboutsummaryrefslogtreecommitdiff
path: root/src/cache.cpp
blob: 43e69dc8e090ed660256a7a41260148e737cc59c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "cache.h"
#include "serializer.h"

Cache::Cache()
{
}

Cache::~Cache()
{
	for( std::map<std::string, Entry *>::iterator i = mCache.begin();
		 i != mCache.end(); i++ )
	{
		delete (*i).second;
	}
}

void Cache::serialize( class Serializer &ar )
{
	if( ar.isLoading() )
	{
		int sCache, sData;
		ar >> sCache;
		std::string sTmp;

		for( int i = 0; i < sCache; i++ )
		{
			Entry *e = new Entry;
			ar >> e->tCreated;
			ar >> sData;
			std::list<std::string> &lData = e->lData;
			for( int j = 0; j < sData; j++ )
			{
				ar >> sTmp;
				lData.push_back( sTmp );
			}
			ar >> sTmp;
			mCache[sTmp] = e;
		}
	}
	else
	{
		ar << mCache.size();
		for( std::map<std::string, Entry *>::iterator i = mCache.begin();
			 i != mCache.end(); i++ )
		{
			ar << (*i).second->tCreated;
			std::list<std::string> &lData = (*i).second->lData;
			ar << lData.size();
			for( std::list<std::string>::iterator j = lData.begin();
				 j != lData.end(); j++ )
			{
				ar << (*j);
			}

			std::string str = (*i).first;
			ar << str;
		}
	}
}

Cache::Entry *Cache::get( const std::string &id )
{
	std::map<std::string, Entry *>::iterator i = mCache.find( id );
	if( i != mCache.end() )
		return (*i).second;

	return NULL;
}

void Cache::put( const std::string &id, Entry *data )
{
	std::map<std::string, Entry *>::iterator i = mCache.find( id );
	if( i != mCache.end() )
		delete (*i).second;

	mCache[id] = data;
}