summaryrefslogtreecommitdiff
path: root/cache.h
blob: 5c62d6450416512b5240689e9f0da30be0ced997 (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
/*
 * $Id: cache.h,v 1.1 2004/12/19 22:03:09 lordjaxom Exp $
 */

#ifndef VDR_TEXT2SKIN_CACHE_HPP
#define VDR_TEXT2SKIN_CACHE_HPP

#include "common.h"
#include "xml/object.h"
#include <vdr/tools.h>
#include <map>
#include <vector>
#include <string>

class cText2SkinBitmap;

struct tBitmapSpec {
	std::string Filename;
	int         Alpha;
	int         Width;
	int         Height;
	int         Colors;

	tBitmapSpec(const std::string &filename, int alpha, int width, int height, int colors):
		Filename(filename), Alpha(alpha), Width(width), Height(height), Colors(colors) {}

	bool operator<(const tBitmapSpec &Src) const;
	bool operator==(const tBitmapSpec &Src) const;
};

inline bool tBitmapSpec::operator<(const tBitmapSpec &Src) const 
{
	if (Filename == Src.Filename) {
		if (Alpha == Src.Alpha) {
			if (Width == Src.Width) {
				if (Height == Src.Height)
					return Colors < Src.Colors;
				return Height < Src.Height;
			}
			return Width < Src.Width;
		}
		return Alpha < Src.Alpha;
	}
	return Filename < Src.Filename;
}
	
inline bool tBitmapSpec::operator==(const tBitmapSpec &Src) const
{
	return Filename == Src.Filename
	    && Alpha == Src.Alpha
		&& Width == Src.Width
		&& Height == Src.Height
		&& Colors == Src.Colors;
}

class cText2SkinCache {
private:
	typedef std::string       name_type;
	typedef tBitmapSpec       key_type;
	typedef cText2SkinBitmap* data_type;

	typedef std::map<key_type,data_type> item_map;
	typedef item_map::iterator item_iterator;
	typedef std::map<name_type,item_map> name_map;
	typedef name_map::iterator name_iterator;
	typedef std::vector<key_type> usage_list;
	typedef usage_list::iterator usage_iterator;

	name_map   mNames;
	usage_list mUsage;
	int        mMaxItems;

protected:
	void Delete(const key_type &Key, data_type &Data);

public:
	cText2SkinCache(int MaxItems);
	~cText2SkinCache();

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

inline bool cText2SkinCache::Contains(const key_type &Key) {
	name_iterator it = mNames.find(Key.Filename);
	if (it != mNames.end())
		return (*it).second.find(Key) != (*it).second.end();
	return false;
}

inline bool cText2SkinCache::Contains(const name_type &Key) {
	return mNames.find(Key) != mNames.end();
}

#endif // VDR_TEXT2SKIN_CACHE_HPP