// -*- c++ -*- #ifndef VDR_TEXT2SKIN_CACHE_HPP #define VDR_TEXT2SKIN_CACHE_HPP #include #include #include #include template class cxCache { private: typedef std::vector usage_list; typedef typename usage_list::iterator usage_iterator; typedef std::map 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 inline bool cxCache::Contains(const key_type &Key) { return mItems.find(Key) != mItems.end(); } template cxCache::cxCache(uint MaxItems) { mMaxItems = MaxItems; } template cxCache::~cxCache() { } template void cxCache::Flush(void) { item_iterator it = mItems.begin(); for (; it != mItems.end(); ++it) DeleteObject(it->first, it->second); mUsage.clear(); mItems.clear(); } template void cxCache::Reset(void) { item_iterator it = mItems.begin(); for (; it != mItems.end(); ++it) ResetObject(it->second); } template data_type &cxCache::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