| 1 |
#ifndef HEADER_RESCACHE_H |
| 2 |
#define HEADER_RESCACHE_H |
| 3 |
|
| 4 |
#include "NoCopy.h" |
| 5 |
#include "ResourcePack.h" |
| 6 |
|
| 7 |
#include "Log.h" |
| 8 |
|
| 9 |
template <class T> |
| 10 |
class CacheEntry { |
| 11 |
public: |
| 12 |
std::string name; |
| 13 |
T value; |
| 14 |
int refcount; |
| 15 |
|
| 16 |
CacheEntry() { |
| 17 |
name = ""; |
| 18 |
value = NULL; |
| 19 |
refcount = 0; |
| 20 |
} |
| 21 |
}; |
| 22 |
|
| 23 |
/** |
| 24 |
* A fixed size cache for any resources. |
| 25 |
*/ |
| 26 |
template <class T> |
| 27 |
class ResCache : public NoCopy { |
| 28 |
private: |
| 29 |
typedef std::vector<class CacheEntry<T>*> t_entries; |
| 30 |
t_entries m_entries; |
| 31 |
ResourcePack<T> *m_unloader; |
| 32 |
unsigned int m_next_pos; |
| 33 |
public: |
| 34 |
/** |
| 35 |
* Creates a cache with the given capacity. |
| 36 |
* The given unloader has to have disabled caching |
| 37 |
* to prevent an infinite loop. |
| 38 |
*/ |
| 39 |
ResCache(int capacity, ResourcePack<T> *new_unloader) { |
| 40 |
m_entries.resize(capacity); |
| 41 |
for (unsigned int i = 0; i < m_entries.size(); i++) { |
| 42 |
m_entries[i] = new CacheEntry<T>(); |
| 43 |
} |
| 44 |
|
| 45 |
m_next_pos = 0; |
| 46 |
m_unloader = new_unloader; |
| 47 |
} |
| 48 |
|
| 49 |
~ResCache() { |
| 50 |
for (unsigned int i = 0; i < m_entries.size(); i++) { |
| 51 |
delete m_entries[i]; |
| 52 |
} |
| 53 |
delete m_unloader; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns a found value or NULL. |
| 58 |
* The returned item should be released via release(). |
| 59 |
*/ |
| 60 |
T get(const std::string &name) { |
| 61 |
CacheEntry<T> *entry = getEntry(name); |
| 62 |
if (!entry) { |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
entry->refcount++; |
| 67 |
return entry->value; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Notes a new value. |
| 72 |
* The caller should release it later via release(). |
| 73 |
*/ |
| 74 |
void put(const std::string &name, T value) { |
| 75 |
CacheEntry<T>* entry = findNextUnusedEntry(); |
| 76 |
if (!entry) { |
| 77 |
LOG_DEBUG(ExInfo("cannot fit into cache") |
| 78 |
.addInfo("name", name)); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if (entry->value != NULL) { |
| 83 |
m_unloader->unloadRes(entry->value); |
| 84 |
} |
| 85 |
entry->name = name; |
| 86 |
entry->value = value; |
| 87 |
entry->refcount = 1; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Releases or takes responsibility for the given value. |
| 92 |
*/ |
| 93 |
void release(T value) { |
| 94 |
CacheEntry<T> *found = getByValue(value); |
| 95 |
if (found) { |
| 96 |
found->refcount -= 1; |
| 97 |
if (found->refcount < 0) { |
| 98 |
LOG_WARNING(ExInfo("extra release of a cache entry")); |
| 99 |
found->refcount = 0; |
| 100 |
} |
| 101 |
} else { |
| 102 |
m_unloader->unloadRes(value); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private: |
| 107 |
/** |
| 108 |
* Returns the matching CacheEntry or NULL. |
| 109 |
*/ |
| 110 |
CacheEntry<T> *getEntry(const std::string &name) { |
| 111 |
for (unsigned int i = 0; i < m_entries.size(); i++) { |
| 112 |
CacheEntry<T> *entry = m_entries[i]; |
| 113 |
if (entry->value != NULL && entry->name == name) { |
| 114 |
return entry; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns the matching CacheEntry or NULL. |
| 123 |
*/ |
| 124 |
CacheEntry<T> *getByValue(T value) { |
| 125 |
for (unsigned int i = 0; i < m_entries.size(); i++) { |
| 126 |
CacheEntry<T> *entry = m_entries[i]; |
| 127 |
if (entry->value == value) { |
| 128 |
return entry; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Returns a next unused CacheEntry or NULL when all entries are full. |
| 137 |
*/ |
| 138 |
CacheEntry<T>* findNextUnusedEntry() { |
| 139 |
for (unsigned int attempt = 0; attempt < m_entries.size(); attempt++) { |
| 140 |
CacheEntry<T> *entry = m_entries[m_next_pos]; |
| 141 |
m_next_pos = (m_next_pos + 1) % m_entries.size(); |
| 142 |
|
| 143 |
if (entry->refcount <= 0) { |
| 144 |
return entry; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return NULL; |
| 149 |
} |
| 150 |
}; |
| 151 |
|
| 152 |
#endif |
| 153 |
|