aboutsummaryrefslogtreecommitdiff
path: root/dep/g3dlite/G3D/WeakCache.h
blob: f9fdc4bbd5b7fea9cad35e4e77900e584a6560e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/** 
  @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