summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlordjaxom <lordjaxom>2005-01-26 20:43:30 +0000
committerlordjaxom <lordjaxom>2005-01-26 20:43:30 +0000
commitdef930a8490618710550731090d8eb5e25cb7de7 (patch)
treee89536ecba80955c50f82d5481f07e5d556b3d7a
parent9e6323df7049a19c5880f13191d98b54db55ca4e (diff)
downloadvdr-plugin-text2skin-def930a8490618710550731090d8eb5e25cb7de7.tar.gz
vdr-plugin-text2skin-def930a8490618710550731090d8eb5e25cb7de7.tar.bz2
- moved tBitmapSpec from cache.h to bitmap.h
- replaced image cache with templated generic cache class
-rw-r--r--cache.h146
1 files changed, 79 insertions, 67 deletions
diff --git a/cache.h b/cache.h
index ee3e81b..964dc11 100644
--- a/cache.h
+++ b/cache.h
@@ -1,99 +1,111 @@
/*
- * $Id: cache.h,v 1.2 2004/12/21 18:24:28 anoncvs Exp $
+ * $Id: cache.h,v 1.3 2005/01/26 20:43:30 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; //XXX
-
-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 {
+template<class key_type, class data_type>
+class cxCache {
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;
+ typedef std::map<key_type,data_type> item_map;
+ typedef item_map::iterator item_iterator;
- name_map mNames;
+ item_map mItems;
usage_list mUsage;
- int mMaxItems;
+ uint mMaxItems;
protected:
- void Delete(const key_type &Key, data_type &Data);
+ void Delete(const key_type &Key, data_type &Data) {}
+ void Reset(data_type &Data) {}
public:
- cText2SkinCache(int MaxItems);
- ~cText2SkinCache();
+ cxCache(uint MaxItems);
+ ~cxCache();
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;
+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()
+{
+ Flush();
+}
+
+/*XXX move
+template<class key_type, class data_type>
+void cxCache<key_type, data_type>::Delete(const key_type &Key, data_type &Data)
+{
+ delete Data;
+}
+*/
+
+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)
+ Delete(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)
+ Reset(it->second);
}
-inline bool cText2SkinCache::Contains(const name_type &Key) {
- return mNames.find(Key) != mNames.end();
+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;
+ }
+
+ if (mUsage.size() == mMaxItems) {
+ item_iterator it = mItems.find(*mUsage.begin());
+ Delete(it->first, it->second);
+ mUsage.erase(mUsage.begin());
+ mItems.erase(it);
+ }
+
+ it = mItems.insert(item_map::value_type(Key, data_type())).first;
+ return it->second;
}
#endif // VDR_TEXT2SKIN_CACHE_HPP