-
Notifications
You must be signed in to change notification settings - Fork 0
/
RessourceHandler.h
127 lines (107 loc) · 2.43 KB
/
RessourceHandler.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
#include <future>
#include <string>
#include <queue>
#include <functional>
#include "ThreadManager.h"
#include "Util.h"
#include "Log.h"
NSP_STD
NSP_UTIL
#pragma once
class RessourceLoader //Loader Interface
{
enum State {
PENDING,
PROCESSING,
DONE
};
friend class RessourceHandler;
public:
RessourceLoader() : _ex(new mutex()) , _state(PENDING){}
RessourceLoader(const RessourceLoader& other) = delete;
virtual void load(ifstream&) = 0;
virtual void* get() = 0;
State getCurrentState() { return _state; }
virtual ~RessourceLoader() {};
protected:
mutex* _ex;
State _state;
};
class RessourceHandler
{
class STDTextLoader : public RessourceLoader
{
public:
void load(ifstream&);
void* get() { return (void*)&_buffer; }
private:
vector<string> _buffer;
};
public:
typedef shared_future<void*> Ressource;
explicit RessourceHandler(ThreadManager*);
Ressource getRessource(string& file, RessourceLoader* loader);
Ressource getRessource(string& file);
private:
class RessourceRegistry {
public:
struct Entry
{
explicit Entry(string& s) : _obj(future<void*>()), _name(s), _checksum(-1)
{}
Entry(string& name, promise<void*>* fut) : _obj(future<void*>()), _name(name), _checksum(-1)
{
_obj = fut->get_future();
}
Entry(const Entry& other) : _obj(future<void*>()), _name(other._name), _checksum(-1)
{
_obj = other._obj;
}
void updateChecksum()
{
int k;
unsigned int crc = -1;
char* data = (char*)_obj.get();
unsigned int len = sizeof(data);
while (len--) {
crc ^= *(data++);
for (k = 0; k < 8; k++)
crc = (crc & 1) ? (crc >> 1) ^ 0x82f63b78 : crc >> 1;
}
_checksum = crc;
}
bool operator==(const Entry& entry)
{
return _checksum == entry._checksum;
}
Entry& operator=(const Entry& other)
{
_name = other._name;
_obj = other._obj;
_checksum = other._checksum;
return *this;
}
unsigned int _checksum;
string _name;
Ressource _obj;
};
Ressource find(string&);
void push(Entry);
void push( string, promise<void*>*);
int loading()
{
unsigned counter = 0;
for (Entry& e : _registry)
if ( (e._obj.wait_for(seconds(0)) != _Future_status::ready))
counter++;
return counter;
}
~RessourceRegistry();
private:
size_t find(string&, int, int);
vector<Entry> _registry;
} _registry;
ThreadManager* _taskManager;
unsigned int _loadCounter;
static vector<string> loaderList;
};