Cache.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Cache_hxx
00024 #define __ccxx_Cache_hxx
00025
00026 #include <map>
00027 #include <utility>
00028
00029 #include <commonc++/Common.h++>
00030
00031 namespace ccxx {
00032
00046 template<typename K, typename T> class Cache
00047 {
00048 public:
00049
00056 Cache(uint_t maxSize);
00057
00061 virtual ~Cache();
00062
00069 void put(K key, T* item);
00070
00078 T *get(K key) const;
00079
00083 inline T *operator[](K key) const
00084 { return(this->get(key)); }
00085
00094 T *take(K key);
00095
00103 bool remove(K key);
00104
00112 bool contains(K key) const;
00113
00117 void clear();
00118
00123 inline uint_t getMaxSize() const
00124 { return(_maxSize); }
00125
00130 inline uint_t getSize() const
00131 { return(_size); }
00132
00136 inline bool isEmpty() const
00137 { return(_size == 0); }
00138
00139 protected:
00140
00151 virtual void onRemove(K key, T *item);
00152
00153 private:
00154
00155 uint_t _maxSize;
00156 uint_t _size;
00157
00158 struct Link
00159 {
00160 Link(K key)
00161 : key(key),
00162 prev(NULL),
00163 next(NULL)
00164 { }
00165
00166 K key;
00167 Link *prev;
00168 Link *next;
00169 };
00170
00171 struct KeyCompare
00172 {
00173 bool operator()(const Link *key1, const Link *key2) const
00174 { return(key1->key < key2->key); }
00175 };
00176
00177 void _unlink(Link *link) const;
00178 void _link(Link *link) const;
00179
00180 typedef std::map<Link *, T *, KeyCompare> MapType;
00181 mutable MapType _map;
00182 mutable Link *_head;
00183 mutable Link *_tail;
00184 };
00185
00186 #include <commonc++/CacheImpl.h++>
00187
00188 }
00189
00190 #endif // __ccxx_Cache_hxx
00191
00192