-
Notifications
You must be signed in to change notification settings - Fork 51
/
ResourceManager.cpp
89 lines (71 loc) · 2.03 KB
/
ResourceManager.cpp
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
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
/*
#include "ResourceManager.h"
#include "Logs.h"
ResourceManager::Resources ResourceManager::_resources;
double ResourceManager::_expiry = 5.0f;
double ResourceManager::_lastUpdate = 0.0f;
double ResourceManager::_lastReport = 0.0f;
void ResourceManager::Register(Resource *resource)
{
_resources.emplace_back(resource);
};
void ResourceManager::Unregister(Resource *resource)
{
Resources::iterator iter = std::find(_resources.begin(), _resources.end(), resource);
if (iter != _resources.end())
_resources.erase(iter);
resource->Release();
};
class ResourceExpired
{
public:
ResourceExpired(double time) : _time(time){};
bool operator()(Resource *resource)
{
return (resource->GetLastUsage() < _time);
}
private:
double _time;
};
void ResourceManager::Sweep(double currentTime)
{
if (currentTime - _lastUpdate < _expiry)
return;
Resources::iterator begin = std::remove_if(_resources.begin(), _resources.end(),
ResourceExpired(currentTime - _expiry));
#ifdef RESOURCE_REPORTING
if (begin != _resources.end())
WriteLog("Releasing resources");
#endif
for (Resources::iterator iter = begin; iter != _resources.end(); ++iter)
(*iter)->Release();
#ifdef RESOURCE_REPORTING
if (begin != _resources.end())
{
std::ostringstream msg;
msg << "Released " << (_resources.end() - begin) << " resources";
WriteLog(msg.str().c_str());
};
#endif
_resources.erase(begin, _resources.end());
#ifdef RESOURCE_REPORTING
if (currentTime - _lastReport > 30.0f)
{
std::ostringstream msg;
msg << "Resources count: " << _resources.size();
WriteLog(msg.str().c_str());
_lastReport = currentTime;
};
#endif
_lastUpdate = currentTime;
};
*/