-
Notifications
You must be signed in to change notification settings - Fork 0
/
RessourceHandler.cpp
108 lines (96 loc) · 2.4 KB
/
RessourceHandler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "RessourceHandler.h"
#include <fstream>
vector<string> RessourceHandler::loaderList = vector<string>();
RessourceHandler::RessourceHandler(ThreadManager* manager) : _loadCounter(0), _taskManager(manager)
{
}
RessourceHandler::Ressource RessourceHandler::getRessource(string & file, RessourceLoader * loader)
{
Ressource fut = _registry.find(file);
if (!fut.valid())
{
_loadCounter++;
loader->_state = RessourceLoader::State::PROCESSING;
promise<void*>* p = new promise<void*>(); //mem Leak?
_registry.push(file, p);
LOG << string("Started Loading ") + file + " \n";
_taskManager->addTask([this, file, loader, p]() {
LOG << string("Started Custom Loader ") + file + " \n";
lock_guard<mutex>(*loader->_ex);
loader->load(ifstream(file));
p->set_value((void**)loader->get());
_loadCounter--;
loader->_state = RessourceLoader::State::DONE;
return 0;
});
}
return _registry.find(file);
}
RessourceHandler::Ressource RessourceHandler::getRessource(string& file)
{
STDTextLoader _loader;
return (Ressource)getRessource(file, &_loader);
}
RessourceHandler::Ressource RessourceHandler::RessourceRegistry::find(string& name)
{
unsigned int pos = find(name, 0, _registry.size());
if (pos >= _registry.size())
return Ressource();
if (_registry[pos]._name == name)
{
_registry[pos].updateChecksum();
return _registry[pos]._obj;
}
return Ressource();
}
size_t RessourceHandler::RessourceRegistry::find(string& name, int min, int max)
{
if (min > max) {
return min;
}
else if (_registry.size() == 0)
return 0;
else {
unsigned int mid = (min + max) / 2;
if (mid >= _registry.size())
return _registry.size();
int comp = _registry[mid]._name.compare(name);
if (comp == 0) {
return mid;
}
else if (comp < 0) {
return find(name, mid + 1, max);
}
else {
return find(name, min, mid - 1);
}
}
return 0;
}
void RessourceHandler::RessourceRegistry::push(Entry e)
{
_registry.insert(_registry.begin() + find(e._name, 0, _registry.size()), e);
}
void RessourceHandler::RessourceRegistry::push( string name, promise<void*>* fut)
{
return push(Entry(name, fut));
}
RessourceHandler::RessourceRegistry::~RessourceRegistry()
{
for (Entry& e : _registry)
{
e._obj.~shared_future();
}
}
void RessourceHandler::STDTextLoader::load(ifstream &f)
{
_buffer.clear();
if (f.is_open())
{
string Line = "";
while (getline(f, Line))
{
_buffer.push_back(Line);
}
}
}