TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::WeakCache< Key, ValueRef > Class Template Reference

#include <WeakCache.h>

Public Member Functions

ValueRef operator[] (const Key &k)
 
void getValues (Array< ValueRef > &values)
 
void clear ()
 
void set (const Key &k, ValueRef v)
 
void remove (const Key &k)
 

Private Types

typedef weak_ptr< typename
ValueRef::element_type > 
ValueWeakRef
 

Private Attributes

Table< Key, ValueWeakReftable
 

Detailed Description

template<class Key, class ValueRef>
class G3D::WeakCache< Key, ValueRef >

A cache that does not prevent its members from being garbage collected. Useful to avoid loading or computing an expression twice. Useful for memoization and dynamic programming.

Maintains a table of weak pointers. Weak pointers do not prevent an object from being garbage collected. If the object is garbage collected, the cache removes its reference.

There are no "contains" or "iterate" methods because elements can be flushed from the cache at any time if they are garbage collected.

Example:

   WeakCache<std::string, shared_ptr<Texture>> textureCache;
   shared_ptr<Texture> loadTexture(std::string s) {
       shared_ptr<Texture> t = textureCache[s];
       if (t.isNull()) {
           t = Texture::fromFile(s);
           textureCache.set(s, t);
       }
       return t;
   }
 

Member Typedef Documentation

template<class Key , class ValueRef >
typedef weak_ptr<typename ValueRef::element_type> G3D::WeakCache< Key, ValueRef >::ValueWeakRef
private

Member Function Documentation

template<class Key , class ValueRef >
void G3D::WeakCache< Key, ValueRef >::clear ( )
inline
89  {
90  table.clear();
91  }
void clear()
Definition: Table.h:578
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56

+ Here is the call graph for this function:

template<class Key , class ValueRef >
void G3D::WeakCache< Key, ValueRef >::getValues ( Array< ValueRef > &  values)
inline
78  {
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  }
bool notNull(const Pointer< T > &p)
Definition: Pointer.h:350
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56
Array< Key > getKeys() const
Definition: Table.h:907
const FieldDescriptor value
Definition: descriptor.h:1522

+ Here is the call graph for this function:

template<class Key , class ValueRef >
ValueRef G3D::WeakCache< Key, ValueRef >::operator[] ( const Key &  k)
inline

Returns NULL if the object is not in the cache

62  {
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  }
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56
weak_ptr< typename ValueRef::element_type > ValueWeakRef
Definition: WeakCache.h:52
bool containsKey(const Key &key) const
Definition: Table.h:874

+ Here is the call graph for this function:

template<class Key , class ValueRef >
void G3D::WeakCache< Key, ValueRef >::remove ( const Key &  k)
inline

Removes k from the cache or does nothing if it is not currently in the cache.

98  {
99  if (table.containsKey(k)) {
100  table.remove(k);
101  }
102  }
bool remove(const Key &key, Key &removedKey, Value &removedValue, bool updateRemoved)
Definition: Table.h:606
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56
bool containsKey(const Key &key) const
Definition: Table.h:874

+ Here is the call graph for this function:

template<class Key , class ValueRef >
void G3D::WeakCache< Key, ValueRef >::set ( const Key &  k,
ValueRef  v 
)
inline
93  {
94  table.set(k, v);
95  }
void set(const Key &key, const Value &value)
Definition: Table.h:599
Table< Key, ValueWeakRef > table
Definition: WeakCache.h:56

+ Here is the call graph for this function:

Member Data Documentation

template<class Key , class ValueRef >
Table<Key, ValueWeakRef> G3D::WeakCache< Key, ValueRef >::table
private

The documentation for this class was generated from the following file: