TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
WeakCache.h
Go to the documentation of this file.
1 
12 #ifndef G3D_WeakCache_h
13 #define G3D_WeakCache_h
14 
15 #include "G3D/ReferenceCount.h"
16 #include "G3D/Table.h"
17 
18 namespace G3D {
19 
50 template<class Key, class ValueRef>
51 class WeakCache {
52  typedef weak_ptr<typename ValueRef::element_type> ValueWeakRef;
53 
54 private:
55 
57 
58 public:
62  ValueRef operator[](const Key& k) {
63  if (table.containsKey(k)) {
64  ValueWeakRef w = table[k];
65  ValueRef s = w.lock();
66  if (! s) {
67  // This object has been collected; clean out its key
68  table.remove(k);
69  }
70  return s;
71  } else {
72  return ValueRef();
73  }
74  }
75 
76 
77 
78  void getValues(Array<ValueRef>& values) {
79  Array<Key> keys;
80  table.getKeys(keys);
81  for (int i = 0; i < keys.size(); ++i) {
82  ValueRef value = (*this)[keys[i]];
83  if(notNull(value)) {
84  values.append(value);
85  }
86  }
87  }
88 
89  void clear() {
90  table.clear();
91  }
92 
93  void set(const Key& k, ValueRef v) {
94  table.set(k, v);
95  }
96 
98  void remove(const Key& k) {
99  if (table.containsKey(k)) {
100  table.remove(k);
101  }
102  }
103 };
104 
105 }
106 #endif
107 
bool notNull(const Pointer< T > &p)
Definition: Pointer.h:350
void clear()
Definition: Table.h:578
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
void set(const Key &key, const Value &value)
Definition: Table.h:599
void clear()
Definition: WeakCache.h:89
Definition: WeakCache.h:51
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
int size() const
Definition: Array.h:430
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56
void set(const Key &k, ValueRef v)
Definition: WeakCache.h:93
ValueRef operator[](const Key &k)
Definition: WeakCache.h:62
weak_ptr< typename ValueRef::element_type > ValueWeakRef
Definition: WeakCache.h:52
Array< Key > getKeys() const
Definition: Table.h:907
const FieldDescriptor value
Definition: descriptor.h:1522
void append(const T &value)
Definition: Array.h:583
void getValues(Array< ValueRef > &values)
Definition: WeakCache.h:78
bool containsKey(const Key &key) const
Definition: Table.h:874