server/dep/include/g3dlite/G3D/WeakCache.h
Lynx3d ae3ad10bcf [10097] Update G3D up to v8.0b4
+ Got rid of zip lib requirement in G3D...
  Still can re-enable code by defining _HAVE_ZIP...

+ Remove silly X11 lib dependency from G3D
  Code doesn't seem to do anything yet anyway, and even if, we don't want it :p

+ Fix another weird G3D build problem...

+ Remove some __asm usage in g3d, which is not available on Win64
  My editor also decided to remove a ton of trailing white spaces...tss...

+ Reapply G3D fixes for 64bit VC

+ not use SSE specific header when SSE not enabled in *nix

+ Updated project files

+ New vmap_assembler VC90/VC80 Project

+ vmap assembler binaries updates

NOTE: Old vmap fikes expected work (as tests show) with new library version.
      But better use new generated versions. Its different in small parts to bad or good...

(based on Lynx3d's repo commit 44798d3)

Signed-off-by: VladimirMangos <vladimir@getmangos.com>
2010-06-23 06:45:25 +04:00

122 lines
2.7 KiB
C++

/**
@file WeakCache.h
@maintainer Morgan McGuire, graphics3d.com
@created 2007-05-16
@edited 2007-05-16
Copyright 2000-2007, Morgan McGuire.
All rights reserved.
*/
#ifndef G3D_WEAKCACHE_H
#define G3D_WEAKCACHE_H
#include "G3D/ReferenceCount.h"
#include "G3D/Table.h"
namespace G3D {
/**
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:
<pre>
WeakCache<std::string, TextureRef> textureCache;
TextureRef loadTexture(std::string s) {
TextureRef t = textureCache[s];
if (t.isNull()) {
t = Texture::fromFile(s);
textureCache.set(s, t);
}
return t;
}
</pre>
*/
template<class Key, class ValueRef>
class WeakCache {
typedef WeakReferenceCountedPointer<typename ValueRef::element_type> ValueWeakRef;
private:
Table<Key, ValueWeakRef> table;
public:
/**
Returns NULL if the object is not in the cache
*/
ValueRef operator[](const Key& k) {
if (table.containsKey(k)) {
ValueWeakRef w = table[k];
ValueRef s = w.createStrongPtr();
if (s.isNull()) {
// This object has been collected; clean out its key
table.remove(k);
}
return s;
} else {
return NULL;
}
}
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);
}
}
};
#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 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);
}
}
};
#endif
}
#endif