libUTL++
ObjectCache.h
1 #pragma once
2 
4 
5 #include <libutl/Hashtable.h>
6 #include <libutl/List.h>
7 
9 
10 UTL_NS_BEGIN;
11 
13 
18 enum cache_t
19 {
22 };
23 
25 // CachedObject ////////////////////////////////////////////////////////////////////////////////////
27 
38 
40 class CachedObject : public Object
41 {
43 
44 public:
45  void
46  set(const Object* object, const ListNode* node)
47  {
48  _object = const_cast<Object*>(object);
49  _node = const_cast<ListNode*>(node);
50  _dirty = false;
51  }
52 
53  virtual const Object&
54  getKey() const
55  {
56  ASSERTD(_object != nullptr);
57  return _object->getKey();
58  }
59 
60  ListNode*
61  getNode() const
62  {
63  return _node;
64  }
65 
66  Object*
67  getObject() const
68  {
69  return _object;
70  }
71 
72  bool
73  isDirty() const
74  {
75  return _dirty;
76  }
77 
78  void
79  setDirty(bool dirty)
80  {
81  _dirty = dirty;
82  }
83 
84 private:
85  void
86  init()
87  {
88  }
89  void
90  deInit()
91  {
92  delete _object;
93  }
94 
95 private:
96  Object* _object;
97  ListNode* _node;
98  bool _dirty;
99 };
100 
102 // ObjectCache /////////////////////////////////////////////////////////////////////////////////////
104 
131 
133 class ObjectCache : public Collection
134 {
138 
139 public:
144  ObjectCache(size_t size)
145  {
146  init(size);
147  }
148 
150  void access(const Object* object);
151 
157  virtual bool
158  add(const Object* object)
159  {
160  return addCache(object);
161  }
162 
163  ListIt
164  begin() const
165  {
166  return _list.begin();
167  }
168 
169  virtual BidIt*
170  beginNew() const
171  {
172  return _list.beginNew();
173  }
174 
175  virtual BidIt*
177  {
178  return _list.beginNew();
179  }
180 
182  virtual void
184  {
185  _ht.clear();
186  _list.clear();
187  _items = 0;
188  }
189 
190  ListIt
191  end() const
192  {
193  return _list.end();
194  }
195 
196  virtual BidIt*
197  endNew() const
198  {
199  return _list.endNew();
200  }
201 
202  virtual BidIt*
204  {
205  return _list.endNew();
206  }
207 
208  virtual Object*
209  find(const Object& key) const
210  {
211  return find(key, isFrozen());
212  }
213 
220  CachedObject* find(const Object& key, bool frozen) const;
221 
227  ListIt findIt(const Object& key) const;
228 
229  void
230  findIt(const Object& key, BidIt& it) const
231  {
232  const_cast_this->findIt(key, it);
233  IFDEBUG(it.setConst(true));
234  }
235 
236  virtual void findIt(const Object& key, BidIt& it);
237 
239  Object*
240  getLRU() const
241  {
242  return _list.last();
243  }
244 
246  Object*
247  getMRU() const
248  {
249  return _list.first();
250  }
251 
253  size_t
254  getSize() const
255  {
256  return _size;
257  }
258 
260  bool
261  isFrozen() const
262  {
263  return getFlag(flg_frozen);
264  }
265 
267  bool
268  isFull() const
269  {
270  return (_list.items() == _size);
271  }
272 
273  bool
274  remove(const Object* key)
275  {
276  ASSERTD(key != nullptr);
277  return remove(*key);
278  }
279 
280  virtual bool
281  remove(const Object& key)
282  {
283  return removeCache(key);
284  }
285 
286  virtual void removeIt(BidIt& it);
287 
289  void set(size_t size);
290 
292  void
293  setFrozen(bool frozen)
294  {
295  setFlag(flg_frozen, frozen);
296  }
297 
298 public:
299  typedef ListIt iterator;
300 
301 private:
302  void
303  init(size_t size = 1024)
304  {
305  _size = size;
306  _list.setOwner(false);
307  }
308  void
309  deInit()
310  {
311  clear();
312  }
313  void access(ListNode* node) const;
314  bool addCache(const Object* object);
315  bool removeCache(const Object* object);
316 
317 private:
318  size_t _size;
319  Hashtable _ht;
320  mutable List _list;
321  enum flg_t
322  {
323  flg_frozen = 4
324  };
325 };
326 
328 
329 UTL_NS_END;
List node.
Definition: ListNode.h:28
Doubly-linked list.
Definition: List.h:80
virtual BidIt * beginNew()
Return an iterator pointing to the beginning of the collection.
Definition: ObjectCache.h:176
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
Bi-directional List iterator.
Definition: ListIt.h:22
void deInit()
De-initialize UTL++.
write-thru (safest)
Definition: ObjectCache.h:20
LRU object cache.
Definition: ObjectCache.h:133
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Object * getMRU() const
Get the MRU object.
Definition: ObjectCache.h:247
Templated proxy for BidIt.
Definition: TBidIt.h:19
virtual BidIt * beginNew() const
Return a const iterator pointing to the beginning of the collection.
Definition: ObjectCache.h:170
#define IFDEBUG(x)
Do x in DEBUG mode only.
bool isFull() const
Determine whether the cache is full.
Definition: ObjectCache.h:268
Object * getLRU() const
Get the LRU object.
Definition: ObjectCache.h:240
cache_t
Cache write policy.
Definition: ObjectCache.h:18
Cached object.
Definition: ObjectCache.h:40
virtual bool add(const Object *object)
Add the given object to the cache and make it the MRU object.
Definition: ObjectCache.h:158
void setFrozen(bool frozen)
Set the frozen flag.
Definition: ObjectCache.h:293
bool isFrozen() const
Get the frozen flag.
Definition: ObjectCache.h:261
void setConst(bool p_const)
Set the const flag.
Definition: FwdIt.h:68
size_t getSize() const
Get the cache size.
Definition: ObjectCache.h:254
ObjectCache(size_t size)
Constructor.
Definition: ObjectCache.h:144
#define UTL_CLASS_NO_SERIALIZE
Declare that a class cannot do serialize().
Definition: macros.h:384
Bi-directional iterator abstraction.
Definition: BidIt.h:25
virtual BidIt * endNew() const
Return a const iterator pointing to the end of the collection.
Definition: ObjectCache.h:197
write-back (best performance)
Definition: ObjectCache.h:21
Chained hashing collection.
Definition: Hashtable.h:92
#define UTL_CLASS_NO_COPY
Declare that a class cannot do copy().
Definition: macros.h:358
virtual const Object & getKey() const
Get the key for this object.
Definition: ObjectCache.h:54
Root of UTL++ class hierarchy.
Definition: Object.h:52
virtual BidIt * endNew()
Return an iterator pointing to the end of the collection.
Definition: ObjectCache.h:203
virtual void clear()
Clear the cache.
Definition: ObjectCache.h:183
void init()
Initialize UTL++.
virtual Object * find(const Object &key) const
Find an object matching a given key.
Definition: ObjectCache.h:209
#define ASSERTD
Do an assertion in DEBUG mode only.
A collection of objects.
Definition: Collection.h:62