-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSilva
1086 lines (961 loc) · 29.1 KB
/
Silva
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Silva
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Made by: Mattis DALLEAU
*/
#pragma once
#include <any>
#include <vector>
#include <typeinfo>
#include <optional>
#include <typeindex>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstddef>
#include <functional>
#include <ostream>
#include <string>
#include <exception>
#include "sparse_array"
// Enable debug mode by default so that the user can see the error messages
#ifndef HL_SILVA_ENABLE_DEBUG
#define HL_SILVA_DEBUG 1
#elif HL_SILVA_ENABLE_DEBUG == 1
#define HL_SILVA_DEBUG 1
#elif HL_SILVA_ENABLE_DEBUG == 0
#define HL_SILVA_DEBUG 0
#else
#error "HL_SILVA_ENABLE_DEBUG is defined so must be either 0 or 1"
#endif
namespace hl
{
namespace silva
{
/**
* @brief Alias for the pure type of a type
*/
template<typename T>
using pure_type_t = std::remove_const_t<std::remove_reference_t<T>>;
/**
* @brief Error class
* @details Used to throw errors
*/
class SilvaError : public std::exception
{
private:
const std::string _msg;
public:
/**
* @brief Construct a new Error object
* @param msg: The message of the error
*/
SilvaError(const std::string &msg) : _msg(msg) {}
/**
* @brief Get the message of the error
* @retval The message of the error
*/
inline const char *what() const noexcept override
{
return _msg.c_str();
}
/**
* @brief Destroy the Error object
*/
~SilvaError() = default;
};
/**
* @brief Forward declaration of Registry
*/
class Registry;
/**
* @brief entity class
*/
class Entity
{
public:
using Id = std::size_t;
private:
Registry &_registry;
Id _id;
/**
* @brief Construct a new Entity object
* @param registry: The registry reference
* @param id: The id of the entity
* @retval None
*/
explicit Entity(Registry& registry, const Id &id) : _registry(registry), _id(id) {}
public:
/**
* @brief Copies an entity
* @param entity: The other entity
*/
Entity(const Entity& entity) = default;
/**
* @brief Copies an entity
* @param entity: The other entity
* @retval Entity: The newly copied entity
*/
Entity &operator=(const Entity& entity) = default;
/**
* @brief Move an entity
* @param entity: The other entity
*/
Entity(Entity&& entity)
: _registry(entity._registry), _id(entity._id)
{
entity._id = -1;
}
/**
* @brief Move an entity
* @param entity: The other entity
* @retval Entity: The newly moved entity
*/
Entity &operator=(Entity&& entity);
/**
* @brief Construct a new Entity object from an existing id
* @param registry: The registry that handles the entity
* @param id: The id of the entity
* @retval None
*/
static inline Entity from_id(Registry& registry, const Id &id)
{
return Entity(registry, id);
}
/**
* @brief Destroy the Entity object
*/
~Entity() = default;
/**
* @brief Get the Id of the entity
* @retval The id of the entity
*/
inline const Id &get_id() const
{
return _id;
}
/**
* @brief To string method
*/
inline std::string to_string() const
{
return "hl::silva::Entity(" + std::to_string(_id) + ")";
}
/**
* @brief Print the entity
*/
inline friend std::ostream &operator<<(std::ostream &os, const Entity &e)
{
return os << e.to_string();
}
/**
* @brief Compare two entities
* @param &lhs: The first entity
* @param &rhs: The second entity
* @retval True if the two entities are equal (same id and same registry)
*/
inline friend bool operator==(const Entity &lhs, const Entity &rhs)
{
return (lhs._id == rhs._id) && (&lhs._registry == &rhs._registry);
}
/**
* @brief Compare two entities
* @param &lhs: The first entity
* @param &rhs: The second entity
* @retval True if the two entities are different
*/
inline friend bool operator!=(const Entity &lhs, const Entity &rhs)
{
return !(lhs == rhs);
}
/**
* @brief Kill the entity (implmentation at the end of the file)
*/
inline void kill();
/**
* @brief Insert a component to the entity (implementation at the end of the file)
* @tparam Component: The component
*/
template<typename Component>
inline Entity& insert(Component &&c);
/**
* @brief Emplace a component to the entity (implementation at the end of the file)
* @tparam Component: The component
* @tparam ...Params: The parameters
*/
template<typename Component, typename... Params>
inline Entity& emplace(Params&&... p);
/**
* @brief Remove a component from the entity (implementation at the end of the file)
*/
template<typename Component>
inline Entity& remove();
/**
* @brief Get the component of the entity (implementation at the end of the file)
* @tparam Component: The component
* @retval Component&
*/
template<typename Component>
inline Component& get();
/**
* @brief Get the component of the entity (implementation at the end of the file)
* @tparam Component: The component
* @retval const Component&
*/
template<typename Component>
inline const Component& get() const;
/**
* @brief Is the entity valid (implementation at the end of the file)
* @retval True if the entity is valid
*/
inline bool is_valid() const;
};
/**
* @brief Template Component class zipper (forward declaration)
* @tparam Components: The components
*/
template <class... Components>
class Zipper;
/**
* @brief Template Component class (forward declaration)
* @tparam Components: The components
*/
template <class... Components>
using View = Zipper<Components...>;
/**
* @brief Registry class
*/
class Registry
{
public:
using anonymous_t = std::any;
using dead_entities_t = std::unordered_set<Entity::Id>;
using component_index_t = std::size_t;
using component_registry_t = std::unordered_map<std::type_index, component_index_t>;
template <typename Component>
using component_sparse_array_t = hl::silva::sparse_array<Component>;
using components_dynamic_array_t = std::vector<anonymous_t>;
using component_removal_system_t = std::function<void(const Entity::Id&)>;
using component_removal_system_map_t = std::vector<component_removal_system_t>;
using system_t = std::function<void(Registry &)>;
template<typename ...Components>
using constrained_system_t = std::function<void(Registry &, Entity&)>;
using system_map_t = std::map<std::string, system_t>;
private:
/**
* @brief Return casted component array
* @tparam Component: The component type
* @retval Casted component array
*/
template <class Component>
inline component_sparse_array_t<Component> &_component_cast(anonymous_t &anon)
{
return std::any_cast<component_sparse_array_t<Component> &>(anon);
}
/**
* @brief Return the index of the component
* @tparam Component: The component type
* @retval The index of the component
*/
template <class Component>
#ifdef HL_SILVA_DEBUG
component_index_t _type_index() const
{
auto it = _components_types_index.find(std::type_index(typeid(Component)));
if (it == _components_types_index.end())
{
throw SilvaError("Component " + std::string(typeid(Component).name()) + " is not registered");
}
return it->second;
}
#else
inline component_index_t _type_index() const
{
return _components_types_index.at(std::type_index(typeid(Component)));
}
#endif
public:
/**
* @brief Construct a new registry
*/
Registry() = default;
/**
* @brief Destroy the registry
*/
~Registry() = default;
/**
* @brief Forbid a registry copy
* @param other: The other registry
*/
Registry(const Registry&) = default;
/**
* @brief Forbid a registry assignment
* @param other: The other registry
* @retval Registry&
*/
Registry& operator=(const Registry&) = default;
/**
* @brief Move a registry
* @param other: The other registry
*/
Registry(Registry&& other)
: _last_component_index(other._last_component_index)
, _components_types_index(std::move(other._components_types_index))
, _components(std::move(other._components))
, _remove_system_methods(std::move(other._remove_system_methods))
, _entities_count(other._entities_count)
, _to_kill_entities(std::move(other._to_kill_entities))
, _killed_entities(std::move(other._killed_entities))
, _last_entity_id(other._last_entity_id)
, _systems(std::move(other._systems))
{
other._last_component_index = 0;
other._entities_count = 0;
other._last_entity_id = -1;
}
/**
* @brief Assign a moved registry
* @param other: The other registry
* @retval Registry&
*/
Registry& operator=(Registry&& other)
{
_last_component_index = other._last_component_index;
_components_types_index = std::move(other._components_types_index);
_components = std::move(other._components);
_remove_system_methods = std::move(other._remove_system_methods);
_entities_count = other._entities_count;
_to_kill_entities = std::move(other._to_kill_entities);
_killed_entities = std::move(other._killed_entities);
_last_entity_id = other._last_entity_id;
_systems = std::move(other._systems);
other._last_component_index = 0;
other._entities_count = 0;
other._last_entity_id = -1;
return *this;
}
/**
* @brief Checks if an entity is a valid one within the registry
* The entity is still considered valid until the next update
* if it has been killed during the current one
* @retval true Is valid
* @retval false Is not valid
*/
bool is_entity_valid(const Entity::Id &from) const
{
if (_last_entity_id >= from) // Is in the range of the alive entities
{
return _killed_entities.find(from) == _killed_entities.end(); // Has it been killed ?
}
return false;
}
/**
* @brief Removes a component from an entity
* @param &from: The entity to remove the component
* @tparam Component: The type of the component
*/
template <typename Component>
inline void remove_component(const Entity::Id &from)
{
get_components<Component>().erase(from);
}
/**
* @brief Add a component to an entity
* @param &to: The entity to add the component
* @param &&c: The component to add
*/
template <typename Component>
inline void insert_component(const Entity::Id &to, Component &&c)
{
return get_components<Component>().insert(to, std::move(c));
}
/**
* @brief Emplace a component to an entity
* @param &from: The entity to remove the component
* @param &&p: The parameters to construct the component
* @tparam Component: The type of the component
* @tparam ...Params: The types of the parameters
*/
template <typename Component, typename... Params>
inline void emplace_component(const Entity::Id &to, Params &&...p)
{
Component c = {std::forward<Params>(p)...};
get_components<Component>().insert(to, std::move(c));
}
/**
* @brief Register one component
* @tparam Component: The component type
*/
template <class Component>
void register_component()
{
if (_components_types_index.find(std::type_index(typeid(Component))) != _components_types_index.end())
{
throw SilvaError("Cannot register twice a component. '" + std::string(typeid(Component).name()) + "' is already registered");
}
_components_types_index[std::type_index(typeid(Component))] = _last_component_index++,
_components.push_back(component_sparse_array_t<Component>()),
_remove_system_methods.push_back([this](const Entity::Id &id) { this->remove_component<Component>(id); });
}
/**
* @brief Register one or more components
* @tparam ...Components: All the components types
*/
template <class ...Components>
inline void register_components()
{
(register_component<Components>(), ...);
}
/**
* @brief Get the component array of the template type
* @tparam Component: The component type
* @retval component_sparse_array_t<Component>&: The component array
*/
template <class Component>
inline component_sparse_array_t<Component> &get_components()
{
return _component_cast<Component>(_components[_type_index<Component>()]);
}
/**
* @brief Get the component array of the template type (const)
* @tparam Component: The component type
* @retval component_sparse_array_t<Component>&: The component array
*/
template <class Component>
inline const component_sparse_array_t<Component> &get_components() const
{
return _component_cast<Component>(_components[_type_index<Component>()]);
}
/**
* @brief Create a new entity
* @retval New entity
*/
Entity spawn_entity()
{
if (_killed_entities.empty())
{
return Entity::from_id(*this, _entities_count++);
}
const Entity::Id random_id = *_killed_entities.begin();
_killed_entities.erase(random_id);
return Entity::from_id(*this, random_id);
}
/**
* @brief Destroy an entity (The entity will actually be destroyed at the next call of the update method)
* @param entity: The entity to destroy
*/
inline void kill_entity(const Entity::Id &id)
{
_to_kill_entities.insert(id);
}
/**
* @brief Add a system to the Registry
* @param sys: The system to add
*/
void add_system(const std::string& name, const system_t &sys)
{
if (_systems.find(name) != _systems.end())
{
throw SilvaError("A system '" + name + " has already been registered");
}
_systems.emplace(name, sys);
}
/**
* @brief Add a system to the Registry (rvalue ref version)
* @param sys: The system to add
*/
void add_system(const std::string& name, system_t &&sys)
{
if (_systems.find(name) != _systems.end())
{
throw SilvaError("A system '" + name + " has already been registered");
}
_systems.emplace(name, sys);
}
/**
* @brief Add a constrained system to the Registry
* @tparam Components: The components
* @param sys: The system to add
*/
template <typename... Components>
inline void add_csystem(const std::string& name, constrained_system_t<Components...> &&sys)
{
add_system(name, [sys](Registry &r) {
for (auto it : r.zip<Components...>())
{
sys(r, std::get<Entity>(it));
}
});
}
void remove_system(const std::string& name)
{
auto it = _systems.find(name);
if (it == _systems.end())
{
throw SilvaError("Cannot remove system '" + name + " because it is not registered");
}
_systems.erase(it);
}
/**
* @brief Update all the systems and the components of the Registry
* Kills the entities that were marked as "should be killed" last update
* before updating again
*/
void update()
{
if (_to_kill_entities.empty() == false)
{
for (const auto &e : _to_kill_entities)
{
for (const auto &f : _remove_system_methods)
{
f(e);
}
_killed_entities.insert(e);
}
_to_kill_entities.clear();
}
for (const auto &[_, system] : _systems)
{
system(*this);
}
}
/**
* @brief Return the number of entities (const)
* @retval Number of entities
*/
inline Entity::Id entities_count() const
{
return _entities_count - (_killed_entities.size() + _to_kill_entities.size());
}
/**
* @brief Get the component of an entity
* @param e: The entity
* @tparam Component: The component
* @retval Component&
*/
template<typename Component>
#if HL_SILVA_DEBUG
Component &get_component(const Entity::Id &id)
{
auto& ref = get_components<Component>()[id];
if (ref.has_value() == false)
{
throw SilvaError("Entity " + std::to_string(id) + " does not have the component " + std::string(typeid(Component).name()));
}
return ref.value();
}
#else
inline Component &get_component(const Entity::Id &id)
{
return get_components<Component>()[id].value();
}
#endif
/**
* @brief Get the component of an entity (const)
* @param e: The entity
* @tparam Component: The component
* @retval Component&
*/
template<typename Component>
#if HL_SILVA_DEBUG
const Component &get_component(const Entity::Id &id) const
{
const auto& ref = get_components<Component>()[id];
if (ref.has_value() == false)
{
throw SilvaError("Entity " + std::to_string(id) + " does not have the component " + std::string(typeid(Component).name()));
}
return ref.value();
}
#else
inline const Component &get_component(const Entity::Id &id) const
{
return get_components<Component>()[id].value();
}
#endif
/**
* @brief Makes a class that can be used to iterate on all the specified components
* The class is considered invalid if one of the components is removed after the creation
* @retval zipper<Components...>: The iterator of the components
*/
template <typename... Components>
inline Zipper<Components...> zip()
{
return Zipper<Components...>(*this);
}
/**
* @brief Alias for the zip method because people are used to the view method
*/
template <typename... Components>
inline View<Components...> view()
{
return Zipper<Components...>(*this);
}
inline std::string to_string() const
{
return "hl::silva::Registry(" + std::to_string(entities_count()) + " entities)";
}
/**
* @brief Print the registry info
*/
inline friend std::ostream &operator<<(std::ostream &os, const Registry &r)
{
return os << r.to_string();
}
private:
component_index_t _last_component_index = 0;
component_registry_t _components_types_index;
components_dynamic_array_t _components;
component_removal_system_map_t _remove_system_methods;
Entity::Id _entities_count = 0;
dead_entities_t _to_kill_entities;
dead_entities_t _killed_entities;
Entity::Id _last_entity_id = -1;
system_map_t _systems;
};
/**
* @brief Template Component class zipper
*/
template <class... Containers>
class Zipper
{
public:
using value_type_t = std::tuple<sparse_array<Containers> &...>;
using iterator_result_t = std::tuple<Entity, Containers &...>;
/**
* @brief Iterator class of the zipper
*/
class iterator
{
public:
/**
* @brief Returns the current value of the iterator
* @retval iterator_result: The current value of the iterator
*/
inline iterator_result_t operator*()
{
return iterator_result_t(
Entity::from_id(_registry, _idx),
std::get<sparse_array<Containers> &>(_current)[_idx].value()...);
}
/**
* @brief Returns the current value of the iterator (const)
* @retval iterator_result: The current value of the iterator
*/
inline iterator_result_t operator*() const
{
return iterator_result(
_idx,
std::get<sparse_array<Containers> &>(_current)[_idx].value()...);
}
/**
* @brief Increment the iterator
* @retval Reference of the iterator
*/
inline iterator &operator++()
{
incr_all();
return *this;
}
/**
* @brief Increment the iterator (non const)
* @retval Reference of the iterator
*/
inline const iterator &operator++() const
{
incr_all();
return *this;
}
/**
* @brief Return true if the two iterators are equal
* @param &lhs: The first iterator
* @param &rhs: The second iterator
* @retval True if the two iterators are equal
*/
inline friend bool operator==(iterator const &lhs,
iterator const &rhs)
{
return lhs._idx == rhs._idx;
}
/**
* @brief Return true if the two iterators are different
* @param &lhs: The first iterator
* @param &rhs: The second iterator
* @retval True if the two iterators are different
*/
inline friend bool operator!=(iterator const &lhs,
iterator const &rhs)
{
return !(lhs == rhs);
}
/**
* @brief Construct a new zipper iterator object
* @param &vref: The reference of the zipper
* @param index: The index of the iterator
* @param end: The end of the iterator (number of entities should be specified)
*/
iterator(Zipper::value_type_t &vref,
std::size_t index,
std::size_t end,
Registry &r)
: _current(vref), _idx(index), _end(end), _registry(r)
{
if (!all_set() && index != end)
{
incr_all();
}
}
/**
* @brief Destructor of the iterator
*/
~iterator() = default;
/**
* @brief Copy constructor of the iterator
* @param &it: The iterator to copy
*/
iterator(const iterator &it) = default;
/**
* @brief Copy assignment of the iterator
* @param &it: The iterator to copy
*/
iterator &operator=(const iterator &it) = default;
/**
* @brief Move constructor of the iterator (deleted)
*/
iterator(iterator &&it) = delete;
/**
* @brief Move assignment of the iterator (deleted)
*/
iterator &operator=(iterator &&it) = delete;
/**
* @brief Return the index of the iterator (i.e. the entity id)
*/
inline std::size_t index() const
{
return _idx;
}
private:
/**
* @brief Increment the iterator
*/
inline void incr_all() const
{
for (++_idx; !all_set() && _idx != _end; ++_idx);
}
/**
* @brief Check if all the containers are set at the current index
* @retval True if all the containers are set at the current index
*/
inline bool all_set() const
{
return (std::get<sparse_array<Containers> &>(_current).non_null(_idx) && ...);
}
private:
Zipper::value_type_t &_current;
mutable std::size_t _idx;
const std::size_t _end;
Registry &_registry;
};
/**
* @brief Construct a new zipper object
* @param &r: The registry to use
*/
Zipper(Registry &r)
: _values(r.get_components<Containers>()...), _end(_values, r.entities_count(), r.entities_count(), r), _begin(_values, 0, r.entities_count(), r)
, _registry(r)
{
}
/**
* @brief Destroy the zipper
*/
~Zipper() = default;
/**
* @brief Copy constructor of the zipper
* @param z: The zipper to copy
*/
Zipper(const Zipper &z) = default;
/**
* @brief Copy assignment of the zipper
* @param z: The zipper to copy
*/
Zipper &operator=(const Zipper &z) = default;
/**
* @brief Move constructor of the zipper (deleted)
*/
Zipper(Zipper &&z) = delete;
/**
* @brief Move assignment of the zipper (deleted)
*/
Zipper &operator=(Zipper &&z) = delete;
/**
* @brief Return the begin iterator
* @retval Begin iterator
*/
inline iterator begin()
{
return _begin;
}
/**
* @brief Return the begin iterator (const)
* @retval Begin iterator
*/
inline const iterator begin() const
{
return _begin;
}
/**
* @brief Return the end iterator
* @retval End iterator
*/
inline iterator end()
{
return _end;
}
/**
* @brief Return the end iterator (const)
* @retval End iterator
*/
inline const iterator end() const
{
return _end;
}
using each_t = std::function<void(Containers &...)>;
using each_entity_t = std::function<void(Entity&, Containers &...)>;
template<bool IncludeEntity>
using which_each = typename std::conditional<IncludeEntity, each_entity_t, each_t>::type&&;
/**
* @brief Apply a function to all the entities that the zipper::iterator considers valid
* Use each2 to also get the Entity object passed to the function
* @param f: The function to apply
* @retval Reference of the zipper
*/
template<bool IncludeEntity = false>
void each(which_each<IncludeEntity> f)
{
if constexpr (IncludeEntity)
{
for (auto it : *this)
{
f(std::get<Entity>(it), std::get<Containers &>(it)...);
}
}
else
{
for (auto it : *this)
{
f(std::get<Containers &>(it)...);
}
}
}
/**
* @brief Print the zipper info
* @param &os: The output stream
* @param &z: The zipper
*/
inline friend std::ostream &operator<<(std::ostream &os, const Zipper<Containers...> &z)
{
os << "hl::silva::Zipper<";
return ((os << typeid(Containers).name() << ", "), ...) << ">";
}
private:
value_type_t _values;
const iterator _end;
const iterator _begin;
Registry &_registry;
};
// Entity methods implementation
Entity &Entity::operator=(Entity&& entity)
{
_registry = entity._registry;
_id = entity._id;
entity._id = -1;
return *this;
}