summaryrefslogtreecommitdiff
path: root/cache.h
blob: 69f17c07cecb0da4bcabceae56539288517325bd (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//								-*- c++ -*-

#ifndef VDR_TEXT2SKIN_CACHE_HPP
#define VDR_TEXT2SKIN_CACHE_HPP

#include <cstdlib>
#include <map>
#include <vector>
#include <string>

template<class key_type, class data_type>
class cxCache {
private:
	typedef std::vector<key_type> usage_list;
	typedef typename usage_list::iterator usage_iterator;
	typedef std::map<key_type,data_type> item_map;
	typedef typename item_map::iterator item_iterator;

	item_map   mItems;
	usage_list mUsage;
	uint       mMaxItems;

protected:
	virtual void DeleteObject(const key_type &Key, data_type &Data) = 0;
	virtual void ResetObject(data_type &Data) = 0;

public:
	cxCache(uint MaxItems);
	virtual ~cxCache();

	void Reset(void);
	void Flush(void);
	bool Contains(const key_type &Key);
	data_type &operator[](const key_type &Key);
	uint Count(void) { return mUsage.size(); }
	void SetMaxItems(uint MaxItems) { mMaxItems = MaxItems; }
};

template<class key_type, class data_type>
inline bool cxCache<key_type, data_type>::Contains(const key_type &Key) 
{
	return mItems.find(Key) != mItems.end();
}

template<class key_type, class data_type>
cxCache<key_type, data_type>::cxCache(uint MaxItems) 
{
	mMaxItems = MaxItems;
}

template<class key_type, class data_type>
cxCache<key_type, data_type>::~cxCache() 
{
}

template<class key_type, class data_type>
void cxCache<key_type, data_type>::Flush(void) 
{
	item_iterator it = mItems.begin();
	for (; it != mItems.end(); ++it)
		DeleteObject(it->first, it->second);

	mUsage.clear();
	mItems.clear();
}

template<class key_type, class data_type>
void cxCache<key_type, data_type>::Reset(void) 
{
	item_iterator it = mItems.begin();
	for (; it != mItems.end(); ++it)
		ResetObject(it->second);
}

template<class key_type, class data_type>
data_type &cxCache<key_type, data_type>::operator[](const key_type &Key) 
{
	item_iterator it = mItems.find(Key);
	if (it != mItems.end()) {
		usage_iterator ut = mUsage.begin();
		for (; ut != mUsage.end(); ++ut) {
			if (*ut == Key) {
				mUsage.erase(ut);
				break;
			}
		}
		mUsage.push_back(Key);
		return it->second;
	} 

	while (mUsage.size() >= mMaxItems) {
		item_iterator it = mItems.find(*mUsage.begin());
		DeleteObject(it->first, it->second);
		mUsage.erase(mUsage.begin());
		mItems.erase(it);
	}

	it = mItems.insert(typename item_map::value_type(Key, data_type())).first;
	return it->second;
}

#endif // VDR_TEXT2SKIN_CACHE_HPP