-
Notifications
You must be signed in to change notification settings - Fork 15
/
ResourceManager.h
467 lines (374 loc) · 11.7 KB
/
ResourceManager.h
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#pragma once
#include <string>
#include <map>
#include <list>
#include <vector>
//#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
template<class T>
unsigned int get_size(const T& t);
namespace utility
{
namespace details
{
template<class T, unsigned int buff_size, bool p, class KeyType, class Comparator>
class resource_cache;
template<class T, unsigned int buff_size, class KeyType, class Comparator>
class resource_cache<T, buff_size, true, KeyType, Comparator>
{
public:
typedef std::shared_ptr<T> resource_ptr;
resource_ptr get(const KeyType& name) {return resource_ptr();}
bool add(const KeyType& name, resource_ptr ptr) {return false;}
void clean() {}
int get_used_space() const {return 0;}
};
// T - òèï ðåñóðñà, buff_size - ìàêñèìàëüíûé ðàçìåð áóôåðà
// òðåáóåò íàëè÷èÿ ìåòîäà size_t get_size(const T&)
template<class T, unsigned int buff_size, class KeyType, class Comparator>
class resource_cache<T, buff_size, false, KeyType, Comparator>
{
typedef std::mutex::scoped_lock scoped_lock;
std::mutex m_mutex;
public:
typedef std::shared_ptr<T> resource_ptr;
typedef std::map<KeyType, resource_ptr, Comparator> resource_storage;
typedef typename resource_storage::iterator resource_storage_it;
resource_cache() : m_used_space(0) {}
bool add(const KeyType& name, const resource_ptr& ptr)
{
if(get(name))
{
return true; // óæå â êåøå
}
scoped_lock lock(m_mutex);
int res_size = get_size(*ptr);
bool res = try_clean_space(res_size);
// êàê ïðàâèëî åñëè ðàçìåð ðåñóðñà áîëüøå ÷åì ðàçìåð êåøà
// â ýòîì ñëó÷àå ðåñóðñ áóäåò óäàëåí, ò.ê. åãî íå óäàëîñü
// ñîõðàíèòü
if (!res) return false;
m_used_space += res_size;
m_storage.insert(std::make_pair(name, ptr));
return true;
}
resource_ptr get(const KeyType& name)
{
scoped_lock lock(m_mutex);
resource_storage_it it = m_storage.find(name);
if (it != m_storage.end())
{
resource_ptr res = it->second;
m_storage.erase(it);
m_used_space -= get_size(*res);
return res;
}
return resource_ptr();
}
bool empty() const
{
scoped_lock lock(m_mutex);
return m_storage.empty();
}
void clean()
{
scoped_lock lock(m_mutex);
m_storage.clear();
m_used_space = 0;
}
int get_used_space() const {return m_used_space;}
protected:
bool try_clean_space(size_t bytes)
{
int total_size = buff_size;
if (m_used_space + bytes <= total_size) return true;
if (bytes > total_size) return false;
int bytes_to_free = 0;
resource_storage_it it = m_storage.begin();
while (it != m_storage.end())
{
bytes_to_free += get_size(*it->second);
it = m_storage.erase(it);
if (bytes_to_free >= bytes) break;
}
m_used_space -= bytes_to_free;
return true;
}
protected:
int m_used_space;
resource_storage m_storage;
};
}
// Ïåðå÷èñëåíèå äåéñòâèé ïðè íåíàéäåííîì ðåñóðñå, âîçâðàùàåìûõ callback-ôóíêöèåé
enum NotFoundResponse
{
NFR_RETURN_NULL,
NFR_RETURN_DEFAULT,
NFR_RETRY
};
template<class T, class KeyType/* = std::wstring*/>
class IResourceManager
{
public:
typedef KeyType key_type;
typedef T resource_type;
typedef typename std::shared_ptr<T> ResourcePtr;
// Òèï callback-ôóíêöèè äëÿ îáðàáîòêè ñèòóàöèé íåíàéäåííîãî ðåñóðñà
typedef std::function<NotFoundResponse (const KeyType& name)> NotFoundCallback;
typedef std::function<bool(T&)> predicate;
typedef std::function<void(T&)> func;
virtual void for_each(func f, predicate p = predicate()) = 0;
virtual ~IResourceManager() {}
enum QueryType
{
QUERY_TYPE_NORMAL, // èùåì ðåñóðñ, åñëè íå íàøëè - ñîçäà¸ì
QUERY_TYPE_FIND_ONLY, // èùåì ðåñóðñ, åñëè íå íàøëè - íå ñîçäà¸ì
QUERY_TYPE_CREATE_ONLY, // íå èùåì ðåñóðñ, ïðîñòî ñîçäà¸ì
};
virtual ResourcePtr GetResource(
const KeyType& name,
bool shared = false,
QueryType queryType = QUERY_TYPE_NORMAL) = 0;
virtual void ClearSharedResources() = 0;
virtual void Set_OnNotFound(const NotFoundCallback& callback) = 0;
virtual void Remove_OnNotFound() = 0;
virtual int get_used_space() const = 0;
};
// åñëè óêàçàíî immediate_del òî îáúåêò áóäåò óíè÷òîæåí ñðàçó êàê òîëüêî íà íåãî
// íå ñòàíåò àêòèâíûõ óêàçàòåëåé. èíà÷å îí áóäåò ïîìåùåí â ñïèñîê íà óäàëåíèå, è
// â ñëó÷àå íàäîáíîñòè ìîæåò áûòü áûñòðî çàòðåáîâàí, áåç ïîâòîðíîãî ñîãäàíèÿ
// òàê æå íàäî ñäåëàòü êåø äëÿ îáúåêòîâ îáùåãî íàçíà÷åíèÿ. êîòîðûå áóäóò õðàíèòüñÿ
// â ìåíåäæåðå, ò.ê. îíè ìîãó è ñêîðåé âñåãî èñïîëüçóþòñÿ íà ïðîòÿæåíèè âñåãî æèçíåííîãî
// öèêëà èãðû - ýòî íàïðèìåð òåêñòóðû êíîïî÷åê, àíèìàöèÿ ïåñîíàæåé, è ò.ä.
// ìåíåäæåð ãàðàíòèðóåò ÷òî åñëè óìíûé óêàçàòåëü ïðèøåë ñî ñâîèì äåëåòåðîì, òî îí áóäåò
// âûçâàí ïðè óíè÷òîæåíèè óêàçàòåëÿ.
// â ñëó÷àå immediate_del = false ïðåäïîëàãàåòñÿ íàëè÷èå äëÿ òèïà ô-èè âèäà get_size(const T&)
// êîòîðàÿ âåðíåò ðàçìåð îáúåêòà â áàéòàõ
template<class T,
bool immediate_del = true,
int buff_size = 0,
class KeyType = std::wstring,
class Comparator = std::less<KeyType>,
typename mutex = std::mutex,
typename lock = std::mutex::scoped_lock
>
class ResourceManager : public IResourceManager<T, KeyType>
{
typedef details::resource_cache<T, buff_size, immediate_del, KeyType, Comparator> delete_cache;
typedef typename lock scoped_lock;
mutex m_mutex;
public:
typedef typename std::shared_ptr<T> ResourcePtr;
typedef typename std::weak_ptr<T> ResourceWeakPtr;
typedef IResourceManager<T, KeyType> Base;
typedef std::map<KeyType, ResourceWeakPtr, Comparator> ResourceStorage;
typedef std::map<KeyType, ResourcePtr, Comparator> SharedResourceStorage;
typedef typename SharedResourceStorage::iterator ShaderResIt;
typedef typename ResourceStorage::iterator ResIt;
typedef std::function<ResourcePtr (const KeyType& name)> CreatorFunc;
ResourceManager(const CreatorFunc& func, const KeyType& default_res_name)
: m_creator_func(func),
m_default_resource_name(default_res_name),
m_use_default_resource(true),
m_used_space(0)
{}
ResourceManager(const CreatorFunc& func)
: m_creator_func(func),
m_use_default_resource(false),
m_used_space(0)
{}
virtual ~ResourceManager()
{
scoped_lock lock(m_mutex);
assert(m_storage.empty());
if (!m_storage.empty())
throw std::exception("ResourceManager::~ResourceManager::Some resources are not deleted!");
}
// ïàðàìåòð shared óïðàÿåò ïðèîðèòåòîì ïîèñêà:
// true - èùåòñÿ ñíà÷àëà â shared õðàíèëèùå, false - ñíà÷àëà â îáû÷íîì
// òàê æå ýòîò ïàðàìåòð óïðàâëÿåò òåì, â êàêîå õðàíèëèùå áóäåò ïîìåùåí ðåñóðñ
ResourcePtr GetResource(const KeyType& name, bool shared = false, QueryType queryType = QUERY_TYPE_NORMAL)
{
scoped_lock lock(m_mutex);
if (queryType == QUERY_TYPE_NORMAL || queryType == QUERY_TYPE_FIND_ONLY)
{
if (shared)
{
SharedResourceStorage::iterator shared_it = m_shared_storage.find(name);
if (shared_it != m_shared_storage.end())
{
return shared_it->second;
}
ResourceStorage::iterator it = m_storage.find(name);
if (it != m_storage.end())
{
ResourcePtr res = it->second.lock();
//assert(res);
if (res)
return res;
}
}
else
{
ResourceStorage::iterator it = m_storage.find(name);
if (it != m_storage.end())
{
ResourcePtr res = it->second.lock();
//assert(res);
if(res)
return res;
}
SharedResourceStorage::iterator shared_it = m_shared_storage.find(name);
if (shared_it != m_shared_storage.end())
{
return shared_it->second;
}
}
}
return CreateResource(name, shared, queryType);
}
void ClearSharedResources()
{
scoped_lock lock(m_mutex);
//std::cout << "Cleaning Shared Resources..." << std::endl;
m_shared_storage.clear();
}
const KeyType& get_default_resorce_name() const {return m_default_resource_name;}
void for_each(Base::func f, Base::predicate p = Base::predicate())
{
scoped_lock lock(m_mutex);
if (!immediate_del)
m_delete_cache.clean();
for (ResIt it = m_storage.begin(); it != m_storage.end(); ++it)
{
ResourcePtr res_ptr = (it->second).lock();
assert(res_ptr);
T& res = *res_ptr;
if (!p || (p && p(res)))
f(res);
}
for (ShaderResIt it = m_shared_storage.begin(); it != m_shared_storage.end(); ++it)
{
assert(it->second);
T& res = *(it->second);
if (!p || (p && p(res)))
f(res);
}
}
void Set_OnNotFound(const NotFoundCallback& callback)
{
m_notFoundCallback = callback;
}
void Remove_OnNotFound()
{
m_notFoundCallback = NULL;
}
virtual int get_used_space() const
{
return m_delete_cache.get_used_space() + m_used_space;
}
private:
ResourcePtr get_resource(const KeyType& name, QueryType queryType = QUERY_TYPE_NORMAL)
{
bool do_cache = !immediate_del;
ResourcePtr res;
if (queryType == QUERY_TYPE_NORMAL || queryType == QUERY_TYPE_FIND_ONLY)
{
res = do_cache ? m_delete_cache.get(name) : ResourcePtr();
}
if (!res && (queryType == QUERY_TYPE_NORMAL || queryType == QUERY_TYPE_CREATE_ONLY))
{
res = m_creator_func(name);
}
return res;
}
ResourcePtr CreateResource(const KeyType& name, bool shared, QueryType queryType)
{
//////////////////////////////////////////////////////////////////////////
//if (!m_default_resource && m_use_default_resource)
//{
// m_default_resource = get_resource(m_default_resource_name);
// if (!m_default_resource) m_use_default_resource = false;
//}
//return m_default_resource;
//////////////////////////////////////////////////////////////////////////
ResourcePtr res;
bool repeat;
do
{
repeat = false;
res = get_resource(name, queryType);
if (!res)
{
NotFoundResponse response = m_use_default_resource ? NFR_RETURN_DEFAULT : NFR_RETURN_NULL;
if (!m_notFoundCallback.empty())
response = m_notFoundCallback(name);
if (response == NFR_RETURN_DEFAULT)
{
if (!m_default_resource)
{
m_default_resource = get_resource(m_default_resource_name);
if (!m_default_resource) m_use_default_resource = false;
}
res = m_default_resource;
}
else if (response == NFR_RETRY)
{
repeat = true;
}
}
}
while (repeat);
if (!res) return res;
if (shared)
{
m_shared_storage[name] = res;
}
else
{
typedef std::pair<KeyType, ResourceWeakPtr> value;
ResourceStorage::_Pairib ib = m_storage.insert(value(name, ResourceWeakPtr()));
assert(ib.second);
ResourcePtr packed_res(res.get(), std::bind(&ResourceManager::on_resource_release, this, _1, ib.first, res));
res = packed_res;
if (res != m_default_resource)
{
m_used_space += get_size(*res);
}
m_storage[name] = res;
}
return res;
}
void on_resource_release(T* p, typename ResourceStorage::iterator it, ResourcePtr orig_ptr)
{
scoped_lock lock(m_mutex);
const KeyType& name = it->first;
if (orig_ptr != m_default_resource)
{
int res_size = get_size(*orig_ptr);
//assert(m_used_space >= res_size);
m_used_space -= res_size;
if (!immediate_del)
{
m_delete_cache.add(name, orig_ptr);
}
}
m_storage.erase(it);
}
private:
CreatorFunc m_creator_func;
SharedResourceStorage m_shared_storage;
ResourceStorage m_storage;
delete_cache m_delete_cache;
// Callback OnFileNotFound
NotFoundCallback m_notFoundCallback;
bool m_use_default_resource;
KeyType m_default_resource_name;
ResourcePtr m_default_resource;
int m_used_space;
};
}