Support Forum       G3D Web Page     
Public Member Functions | List of all members
G3D::WeakCache< Key, ValueRef > Class Template Reference

A cache that does not prevent its members from being garbage collected. More...

Public Member Functions

void clear ()
 
void getValues (Array< ValueRef > &values)
 
ValueRef operator[] (const Key &k)
 Returns nullptr if the object is not in the cache. More...
 
void remove (const Key &k)
 Removes k from the cache or does nothing if it is not currently in the cache. More...
 
void set (const Key &k, const ValueRef &v)
 

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<String, shared_ptr<Texture>> textureCache;
   shared_ptr<Texture> loadTexture(String s) {
       shared_ptr<Texture> t = textureCache[s];
       if (t.isNull()) {
           t = Texture::fromFile(s);
           textureCache.set(s, t);
       }
       return t;
   }
 

Member Function Documentation

◆ clear()

template<class Key, class ValueRef>
void G3D::WeakCache< Key, ValueRef >::clear ( )
inline

◆ getValues()

template<class Key, class ValueRef>
void G3D::WeakCache< Key, ValueRef >::getValues ( Array< ValueRef > &  values)
inline

◆ operator[]()

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

Returns nullptr if the object is not in the cache.

◆ remove()

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.

◆ set()

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

documentation generated on Wed Nov 24 2021 08:02:02 using doxygen 1.8.15