diff options
Diffstat (limited to 'dep/g3dlite/include/G3D/WeakCache.h')
-rw-r--r-- | dep/g3dlite/include/G3D/WeakCache.h | 65 |
1 files changed, 25 insertions, 40 deletions
diff --git a/dep/g3dlite/include/G3D/WeakCache.h b/dep/g3dlite/include/G3D/WeakCache.h index f9fdc4bbd5b..b2f43818ba3 100644 --- a/dep/g3dlite/include/G3D/WeakCache.h +++ b/dep/g3dlite/include/G3D/WeakCache.h @@ -1,16 +1,16 @@ /** - @file WeakCache.h + \file G3D/WeakCache.h - @maintainer Morgan McGuire, graphics3d.com + \maintainer Morgan McGuire, graphics3d.com - @created 2007-05-16 - @edited 2007-05-16 + \created 2007-05-16 + \edited 2012-01-02 - Copyright 2000-2007, Morgan McGuire. + Copyright 2000-2012, Morgan McGuire. All rights reserved. */ -#ifndef G3D_WEAKCACHE_H -#define G3D_WEAKCACHE_H +#ifndef G3D_WeakCache_h +#define G3D_WeakCache_h #include "G3D/ReferenceCount.h" #include "G3D/Table.h" @@ -31,10 +31,10 @@ namespace G3D { Example: <pre> - WeakCache<std::string, TextureRef> textureCache; + WeakCache<std::string, shared_ptr<Texture>> textureCache; - TextureRef loadTexture(std::string s) { - TextureRef t = textureCache[s]; + shared_ptr<Texture> loadTexture(std::string s) { + shared_ptr<Texture> t = textureCache[s]; if (t.isNull()) { t = Texture::fromFile(s); @@ -49,7 +49,7 @@ namespace G3D { */ template<class Key, class ValueRef> class WeakCache { - typedef WeakReferenceCountedPointer<typename ValueRef::element_type> ValueWeakRef; + typedef weak_ptr<typename ValueRef::element_type> ValueWeakRef; private: @@ -62,46 +62,32 @@ public: ValueRef operator[](const Key& k) { if (table.containsKey(k)) { ValueWeakRef w = table[k]; - ValueRef s = w.createStrongPtr(); - if (s.isNull()) { + ValueRef s = w.lock(); + if (! s) { // This object has been collected; clean out its key table.remove(k); } return s; } else { - return NULL; + return ValueRef(); } } - void set(const Key& k, ValueRef v) { - table.set(k, v); - } - /** Removes k from the cache or does nothing if it is not currently in the cache.*/ - void remove(const Key& k) { - if (table.containsKey(k)) { - table.remove(k); + + void getValues(Array<ValueRef>& values) { + Array<Key> keys; + table.getKeys(keys); + for (int i = 0; i < keys.size(); ++i) { + ValueRef value = (*this)[keys[i]]; + if(notNull(value)) { + values.append(value); + } } } -}; - -#if 0 // To turn off all WeakCaching -template<class Key, class ValueRef> -class WeakCache { -private: - - Table<Key, ValueRef> table; -public: - /** - Returns NULL if the object is not in the cache - */ - ValueRef operator[](const Key& k) { - if (table.containsKey(k)) { - return table[k]; - } else { - return NULL; - } + void clear() { + table.clear(); } void set(const Key& k, ValueRef v) { @@ -115,7 +101,6 @@ public: } } }; -#endif } #endif |