Repository navigation
Expand file tree
/
Copy pathid.hpp
More file actions
344 lines (292 loc) · 6.91 KB
/
Copy pathid.hpp
File metadata and controls
344 lines (292 loc) · 6.91 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
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
/**
* @file liblava/core/id.hpp
* @brief Object Identification
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/core/types.hpp"
#include <atomic>
#include <deque>
#include <memory>
#include <mutex>
#include <set>
namespace lava {
/**
* @brief Identification
*/
struct id {
/// Reference to id
using ref = id const&;
/// Set of ids
using set = std::set<id>;
/// Reference to set of ids
using set_ref = set const&;
/// List if ids
using list = std::vector<id>;
/// Map of ids
using map = std::map<id, id>;
/// Index map by ids
using index_map = std::map<id, index>;
/// String map by ids
using string_map = std::map<id, string>;
/**
* @brief Construct a new id
*/
id() = default;
/**
* @brief Construct a new id
* @param value Value of id
*/
id(index value)
: value(value) {}
/// Value
index value = no_index;
/**
* @brief Check if the id is valid
* @return Id is valid or not
*/
bool valid() const {
return value != no_index;
}
/**
* @brief Convert the id to string
* @return string String representation of id
*/
string to_string() const {
return std::to_string(value);
}
/**
* @brief Invalidate id
*/
void invalidate() {
*this = {};
}
/**
* @brief Compare operator
*/
auto operator<=>(id const&) const = default;
};
/// Map of string ids
using string_id_map = std::map<string, id>;
/// Undefined id
constexpr id const undef_id = id();
/**
* @brief Convert to id
* @param value Source value
* @return id Converted value
*/
inline id to_id(auto value) {
return {static_cast<index>(value)};
}
/**
* @brief Id factory
*/
struct ids {
/**
* @brief Get id factory instance
* @return ids& Id factory
*/
static ids& instance() {
static ids ids;
return ids;
}
/**
* @brief Get next id from factory
* @return id Next id
*/
id next() {
return {++m_next};
}
private:
/// Next id
std::atomic<index> m_next = {no_index};
};
/**
* @brief Add object to id map
* @tparam T Type of object
* @param object Object to add
* @param map Target map
* @return id Id of object in map
*/
template <typename T>
inline id add_id_map(T const& object,
std::map<id, T>& map) {
auto next = ids::instance().next();
map.emplace(next, std::move(object));
return next;
}
/**
* @brief Remove object from id map
* @tparam T Type of object
* @param object_id Object to remove
* @param map Target map
* @return Removed object from map or object not found
*/
template <typename T>
inline bool remove_id_map(id::ref object_id,
std::map<id, T>& map) {
if (!map.count(object_id))
return false;
map.erase(object_id);
return true;
}
/**
* @brief Id listeners
* @tparam T Listener
*/
template <typename T>
struct id_listeners {
/**
* @brief Add listener to map
* @param listener Target listener
* @return id Id of listener
*/
id add(typename T::func const& listener) {
return add_id_map(listener, m_list);
}
/**
* @brief Remove listener from map by id
* @param id Id of listener
*/
void remove(id& id) {
if (remove_id_map(id, m_list))
id.invalidate();
}
/**
* @brief Get the list
* @return T::listeners const& List of listeners
*/
typename T::listeners const& get_list() const {
return m_list;
}
private:
/// List of listeners
typename T::listeners m_list = {};
};
/**
* @brief Entity
*/
struct entity : no_copy_no_move, interface {
/**
* @brief Construct a new entity
*/
entity()
: m_id(ids::instance().next()) {}
/**
* @brief Get the id of entity
* @return id::ref Entity id
*/
id::ref get_id() const {
return m_id;
}
private:
/// Entity id
id m_id;
};
/**
* @brief Id registry
* @tparam T Type of objects hold in registry
* @tparam Meta Meta type for object
*/
template <typename T, typename Meta>
struct id_registry {
/// Shared pointer to id registry
using s_ptr = std::shared_ptr<T>;
/// Map of id registries
using s_map = std::map<id, s_ptr>;
/// Map of ids with meta
using meta_map = std::map<id, Meta>;
/**
* @brief Create a new object in registry
* @param info Meta information
* @return id Object id
*/
id create(Meta info = {}) {
auto object = std::make_shared<T>();
add(object, info);
return object->get_id();
}
/**
* @brief Add a object with meta to registry
* @param object Object to add
* @param info Meta of object
*/
void add(s_ptr object,
Meta info = {}) {
m_objects.emplace(object->get_id(), object);
m_meta.emplace(object->get_id(), info);
}
/**
* @brief Check if object exists in registry
* @param object_id Object to check
* @return Object exists or not
*/
bool exists(id::ref object_id) const {
return m_objects.count(object_id);
}
/**
* @brief Get the object by id
* @param object_id Object id
* @return s_ptr Shared pointer to object
*/
s_ptr get(id::ref object_id) const {
return m_objects.at(object_id);
}
/**
* @brief Get the meta by id
* @param object_id Object id
* @return Meta Meta object
*/
Meta const& get_meta(id::ref object_id) const {
return m_meta.at(object_id);
}
/**
* @brief Get all objects
* @return s_map const& Map with objects
*/
s_map const& get_all() const {
return m_objects;
}
/**
* @brief Get all meta objects
* @return meta_map const& Map with metas
*/
meta_map const& get_all_meta() const {
return m_meta;
}
/**
* @brief Update meta of object
* @param object_id Object id
* @param meta Meta to update
* @return Meta updated or not
*/
bool update(id::ref object_id,
Meta const& meta) {
if (!exists(object_id))
return false;
m_meta.at(object_id) = meta;
return true;
}
/**
* @brief Remove object from registry
* @param object_id Object id
*/
void remove(id::ref object_id) {
m_objects.erase(object_id);
m_meta.erase(object_id);
}
/**
* @brief Clear the registry
*/
void clear() {
m_objects.clear();
m_meta.clear();
}
private:
/// Map of objects
s_map m_objects;
/// Map of metas
meta_map m_meta;
};
} // namespace lava