libcommonc++  0.7
AbstractCache.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_AbstractCache_hxx
24 #define __ccxx_AbstractCache_hxx
25 
26 #include <set>
27 #include <utility>
28 
29 #include <commonc++/Common.h++>
30 
31 namespace ccxx {
32 
46 template<typename K, typename T> class AbstractCache
47 {
48  public:
49 
51  virtual ~AbstractCache();
52 
60  virtual T* get(const K& key) = 0;
61 
65  inline T *operator[](const K& key)
66  { return(this->get(key)); }
67 
75  bool contains(const K& key) const;
76 
80  virtual void clear();
81 
86  inline uint_t getMaxSize() const
87  { return(_maxSize); }
88 
93  inline uint_t getSize() const
94  { return(_map.size()); }
95 
99  inline bool isEmpty() const
100  { return(_map.empty()); }
101 
105  inline bool isFull() const
106  { return(_map.size() == _maxSize); }
107 
108  protected:
109 
116  AbstractCache(uint_t maxSize);
117 
128  virtual void onRemove(const K& key, T* item);
129 
131  uint_t _maxSize;
132 
133  struct Link
134  {
135  Link(K key, T* data = NULL)
136  : key(key)
137  , data(data)
138  , prev(NULL)
139  , next(NULL)
140  { }
141 
142  ~Link()
143  {
144  delete data;
145  }
146 
147  K key;
148  T* data;
149  Link* prev;
150  Link* next;
151  };
152 
153  typedef Link LinkType;
154 
155  struct KeyCompare
156  {
157  bool operator()(const Link* key1, const Link* key2) const
158  { return(key1->key < key2->key); }
159  };
160 
161  void _link(Link* link) const;
162  void _unlink(Link* link) const;
163 
164  typedef std::set<Link*, KeyCompare> MapType;
165  mutable MapType _map;
166  mutable Link* _head;
167  mutable Link* _tail;
169 };
170 
171 #include <commonc++/AbstractCacheImpl.h++>
172 
173 } // namespace ccxx
174 
175 #endif // __ccxx_AbstractCache_hxx
bool contains(const K &key) const
Test if the cache contains an item with the specified key.
bool isFull() const
Test if the cache is full.
Definition: AbstractCache.h++:105
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool isEmpty() const
Test if the cache is empty.
Definition: AbstractCache.h++:99
virtual ~AbstractCache()
Destructor.
An abstract base class for a general-purpose LRU-cache with a maximum capacity.
Definition: AbstractCache.h++:46
uint_t getSize() const
Return the current size of the cache, i.e., the number of items in the cache.
Definition: AbstractCache.h++:93
virtual void clear()
Remove all items from the cache.
AbstractCache(uint_t maxSize)
Construct a new cache with the given maximum size.
virtual void onRemove(const K &key, T *item)
A method that is called whenever an item is being removed from the cache, but just before it has been...
Definition: AllocationMap.c++:25
T * operator[](const K &key)
Index operator.
Definition: AbstractCache.h++:65
uint_t getMaxSize() const
Return the maximum size of the cache, i.e., the maximum number of items that the cache can hold...
Definition: AbstractCache.h++:86