-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResManagement.h
More file actions
306 lines (255 loc) · 8.74 KB
/
ResManagement.h
File metadata and controls
306 lines (255 loc) · 8.74 KB
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
#pragma once
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/signals2/signal.hpp>
#include <memory>
#include <mutex>
#include <fstream>
#include <filesystem>
#include <unordered_map>
//---------------------------------------------------------------------------------------------------------------------
// Support
//---------------------------------------------------------------------------------------------------------------------
template <class T>
class Singleton
{
public:
template <typename... Args>
static T& instance(Args&&... args)
{
std::call_once(
get_once_flag(),
[](Args&&... args)
{
instance_.reset(new T(std::forward<Args>(args)...));
}, std::forward<Args>(args)...);
return *instance_.get();
}
protected:
explicit Singleton<T>() {}
~Singleton<T>() {}
Singleton(const Singleton&) = delete;
const Singleton& operator=(const Singleton&) = delete;
private:
static inline std::unique_ptr<T> instance_ = {};
static std::once_flag& get_once_flag()
{
static std::once_flag once_;
return once_;
}
};
//---------------------------------------------------------------------------------------------------------------------
// Resource management
//---------------------------------------------------------------------------------------------------------------------
class IReloadableBase
{
public:
virtual ~IReloadableBase() = default;
virtual void requestReload() = 0;
virtual void reloadDone() = 0;
// scoped for auto disconnect
boost::signals2::scoped_connection requestReloadConnection;
boost::signals2::scoped_connection reloadDoneConnection;
};
template <typename ResourceType, typename IntermediateStateType>
class IReloadable : public IReloadableBase
{
public:
virtual ~IReloadable() = default;
virtual IntermediateStateType prepareReload() = 0;
virtual void reloadFromData(const IntermediateStateType& state, const ResourceType& data) = 0;
};
template <typename Implementation, typename T>
class Factory : public Singleton<Implementation>
{
// declared somewhere else
friend class DebugInterface;
public:
using ValueType = std::decay_t<T>;
using ResourcePathType = std::string;
using TypeSharedPtr = std::shared_ptr<ValueType>;
using TypeWeakPtr = std::weak_ptr<ValueType>;
using TypeUniquePtr = std::unique_ptr<ValueType>;
using RequestReloadSignal = boost::signals2::signal<void()>;
using ReloadDoneSignal = boost::signals2::signal<void()>;
struct Resource
{
TypeWeakPtr resource;
RequestReloadSignal requestReload;
ReloadDoneSignal reloadDone;
};
using CacheType = std::unordered_map<ResourcePathType, Resource>;
static TypeSharedPtr load(const ResourcePathType& resource)
{
return Factory::instance().loadInternal(resource);
}
// Send by value to pin resource while saving
static bool save(const ResourcePathType& resource, TypeSharedPtr data)
{
return Factory::instance().saveInternal(resource, std::move(data));
}
static void registerUser(
const ResourcePathType& resource,
IReloadableBase& user)
{
auto& signals = Factory::instance().m_cache;
auto sig = signals.find(resource);
if (sig != std::end(signals))
{
user.requestReloadConnection = sig->second.requestReload.connect(
std::bind(&IReloadableBase::requestReload, &user));
user.reloadDoneConnection = sig->second.reloadDone.connect(
std::bind(&IReloadableBase::reloadDone, &user));
}
}
virtual ~Factory()
{
std::scoped_lock<std::mutex> guard {m_access};
for (auto& kv : m_cache)
{
if (!kv.second.resource.expired())
{
std::cout << "ERROR: Resource " << kv.first << " leaked!\n";
}
}
}
protected:
virtual bool hasValidExtension(std::string_view resource) = 0;
virtual bool doLoad(std::string_view resource, ValueType& data) = 0;
virtual bool doSave(std::string_view resource, ValueType& data) = 0;
private:
TypeSharedPtr loadInternal(const std::string& path)
{
if (!hasValidExtension(path))
{
return {};
}
{
std::scoped_lock<std::mutex> guard {m_access};
auto cached = getFromCache(path);
if (cached)
return cached;
}
auto unique = std::make_unique<ValueType>();
if (!doLoad(path, *unique))
{
// not loaded correctly
return {};
}
{
std::scoped_lock<std::mutex> guard {m_access};
// try to find again
auto cached = getFromCache(path);
if (cached)
return cached;
// steal to shared and put into cache
auto shared = TypeSharedPtr(unique.release(), destroyData);
auto res = Resource{};
res.resource = TypeWeakPtr {shared};
m_cache[path] = std::move(res);
return shared; // RNVO should handle moving named shared_ptr
}
}
bool saveInternal(const std::string& path, TypeSharedPtr data)
{
if (!hasValidExtension(path))
{
return false;
}
if (!doSave(path, *data))
{
return false;
}
std::scoped_lock<std::mutex> guard {m_access};
// we need somehow notify users about resource changing
// or else they will crash out application
auto res = Resource{};
res.resource = TypeWeakPtr {data};
m_cache[path] = std::move(res);
return true;
}
TypeSharedPtr getFromCache(const std::string& resource)
{
const auto cached = m_cache.find(resource);
if (cached != end(m_cache))
{
if (cached->second.resource.expired())
{
// This is unfortunate we should be destoyed by manager itself
// OK, lets remove it
m_cache.erase(cached);
return {};
}
return cached->second.resource.lock();
}
return {};
}
static void destroyData(ValueType* raw)
{
auto& manager = Factory::instance();
std::lock_guard<std::mutex> guard {manager.m_access};
// first expired is our destroyed pointer
// we could check if we can get internal control block of weak
// but its too hackerish so no
const auto expired = std::find_if(begin(manager.m_cache), end(manager.m_cache), [](auto& p)
{
return p.second.resource.expired();
});
if (expired == end(manager.m_cache))
{
// THIS IS BAD!
// We shouldn't get destroyed by shared_ptr decref
// and could not find any expired weak!
return;
}
manager.m_cache.erase(expired);
}
private:
CacheType m_cache;
std::mutex m_access;
};
template <typename T>
class FstreamFactory : public Factory<FstreamFactory<T>, T>
{
friend class Singleton<FstreamFactory>;
protected:
FstreamFactory()
: Factory<FstreamFactory, T>() {}
explicit FstreamFactory(std::vector<std::string> extensions)
: Factory<FstreamFactory, T>()
, m_supportedExtensions(std::move(extensions)) { }
bool hasValidExtension(std::string_view resource) override
{
const auto extension = std::filesystem::path(resource).extension().string();
return std::find(begin(m_supportedExtensions), end(m_supportedExtensions), extension) != end(
m_supportedExtensions);
}
bool doLoad(std::string_view resource, typename FstreamFactory::ValueType& data) override
{
const auto resourcepath = std::string {resource};
auto file = std::fstream {resourcepath, std::fstream::in | std::fstream::binary};
if (!file.is_open())
{
std::cout << "ERROR: cannot open file!" << resourcepath;
return false;
}
boost::archive::binary_iarchive ar {file};
ar >> data;
return true;
}
bool doSave(std::string_view resource, typename FstreamFactory::ValueType& data) override
{
const auto resourcepath = std::string {resource};
auto file = std::fstream {resourcepath, std::fstream::out | std::fstream::binary};
if (!file.is_open())
{
std::cout << "ERROR: cannot open file!" << resourcepath;
return false;
}
boost::archive::binary_oarchive ar {file};
ar << data;
return true;
}
private:
std::vector<std::string> m_supportedExtensions;
};