-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom_without_signal.h
1266 lines (1044 loc) · 49.5 KB
/
dom_without_signal.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
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
/*
* This is the dom-library - an implementation of the Entity-Component-System Pattern.
* Copyright (C) 2016 Felix Becker - [email protected]
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
#ifndef DOM_LIBRARY_H
#define DOM_LIBRARY_H
#include <queue>
#include <list>
#include <vector>
#include <memory>
#include <functional>
#include <bitset>
#include <array>
#include <unordered_map>
#include <stdexcept>
#include <tuple>
#include <limits>
#include <iostream>
using EntityID = uint64_t;
using SubID = uint16_t;
namespace dom
{
constexpr unsigned short DEFAULT_COMPONENT_COUNT = 256;
template <typename CINDEX, CINDEX COMP_TOTAL> class Universe;
/** \brief A handle object to access an element in the ChunkedArray.
* Works with double indexing (block,index). */
struct ChunkedArrayHandle
{
SubID block;
SubID index;
ChunkedArrayHandle() {}
ChunkedArrayHandle(unsigned short b, unsigned short i) : block(b), index(i) {}
/** \brief Returns a handle that compares false to every other handle. */
static ChunkedArrayHandle null()
{
static ChunkedArrayHandle nullHandle(std::numeric_limits<unsigned short>::max(), std::numeric_limits<unsigned short>::max());
return nullHandle;
}
/** \brief Implements comparison. */
bool operator==(const ChunkedArrayHandle& other) const { return block == other.block && index == other.index; }
/** \brief Implements comparison. */
bool operator!=(const ChunkedArrayHandle& other) const { return !(*this == other); }
/** \brief Implements bool checking. */
explicit operator bool() const { return *this != null(); }
};
class BaseChunkedArray
{
public: virtual void destroy(ChunkedArrayHandle h) = 0;
};
/**
* \brief A datastructure that stores its elements in semi-continous memory.
* Elements are arranged in memory-blocks of a fixed size. The datastructure attempts
* to place that memory blocks close together.
* Add and destroy operations are cheap and wont require reallocation.
* Note that destruction of the objects in the ChunkedArray cant be done by the
* array itself. The user must take care of destroying all created objects itself
* by call destroy(handle). The reason behind this is, that the array does not hold an
* list of the slots with constructed object in itself. But the user of the array should
* have.
* Template types:
* T ... Type of the object to be stored
* BLOCK_SIZE ... number of objects with type T, that can be stored in a continous block
* REUSE_C ... minimum stack-size for free slots. Choose value > 0 only if you want to
* avoid, that single slots are reused too often.
*/
template<typename T, std::size_t BLOCK_SIZE = 8192, std::size_t REUSE_C = 0>
class ChunkedArray : public BaseChunkedArray
{
private:
class MemoryBlock;
std::allocator<T> alloc;
std::vector< MemoryBlock > mBlocks;
std::queue<ChunkedArrayHandle> mFreeSlots;
public:
/** \brief Constructs a new array with a single block allocated. */
ChunkedArray();
/**
* \brief Adds a new element and constructs it in place. This may
* triggers the creation of a new block.
*/
template<typename ...PARAM>
ChunkedArrayHandle add(PARAM&&... param);
/**
* \brief Accesses an element through an ChunkedArrayHandle.
*/
T& get(ChunkedArrayHandle h);
const T& get(ChunkedArrayHandle h) const;
/**
* \brief Destroys an element through an ChunkedArrayHandle.
*/
virtual void destroy(ChunkedArrayHandle h) override;
/** \brief Returns the number of memory blocks currently allocated. */
std::size_t blockCount() const;
/** \brief Returns the number of elements in the list. */
std::size_t size() const;
~ChunkedArray();
private:
class MemoryBlock
{
friend class ChunkedArray<T, BLOCK_SIZE, REUSE_C>;
private:
std::size_t contentCount;
std::size_t endIndex; //one index past the last occupied index
T* ptr;
public:
MemoryBlock() : contentCount(0), endIndex(0), ptr(nullptr) {}
};
};
/**
* \brief A base class for all component base structs.
* CINDEX is a index type for components, rather small. COMP_TOTAL is the total number of components allowed.
*/
template<typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
struct ComponentTraitsBase
{
protected:
/** \brief Returns a new, unique ID for a component-type, but doesnt give
* more than COMP_TOTAL different IDs. */
static CINDEX newID();
};
/**
* \brief A traits class for a specific component type C.
* Provides a unique ID for that type.
* Throws an exception, if a new ID is requested, when there are already
* COMP_TOTAL different component-types registered.
*/
template<typename C, typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
struct ComponentTraits : public ComponentTraitsBase<CINDEX, COMP_TOTAL>
{
public:
static CINDEX getID();
};
/**
* \brief An error thrown by ComponentTraitsBase, if there are more then
* COMP_TOTAL ids requested.
*/
struct ComponentCountError : public std::runtime_error
{
ComponentCountError();
};
using EntityArrayHandle = ChunkedArrayHandle;
using ComponentHandle = ChunkedArrayHandle;
template<typename CINDEX, CINDEX COMP_TOTAL> class EntityData;
template<typename CINDEX, CINDEX COMP_TOTAL> class Universe;
template<typename C> class ComponentInstantiator;
/**
* \brief A helper class for components that can be attached multiple times to an entity.
* The component set is implemented as a vector and the components can be accessed by index.
* Note that MultiComponent-sets must not persist continously in memory,
* but the chances are good that they are.
*/
template<typename C, typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
class MultiComponent
{
private:
std::vector<ComponentHandle> mHandles;
Universe<CINDEX, COMP_TOTAL>* mUniverse; //the universe that holds this component, needed for correct destruction
public:
template<typename ... PARAM>
MultiComponent(std::size_t num, Universe<CINDEX, COMP_TOTAL>& universe, PARAM&& ... param);
MultiComponent();
template<typename ... PARAM>
void init(std::size_t num, Universe<CINDEX, COMP_TOTAL>& universe, PARAM&& ... param);
C& getComponent(std::size_t index);
const C& getComponent(std::size_t index) const;
std::size_t getComponentCount() const;
void cleanup();
virtual ~MultiComponent();
};
/** \brief A Utility struct used to construct a component with parameters. */
template<typename C>
struct ComponentInstantiator
{
public:
using COMP_TYPE = C;
template<typename CINDEX, CINDEX COMP_TOTAL, typename ... PARAM>
ComponentInstantiator(Universe<CINDEX, COMP_TOTAL>& universe, PARAM&& ... param);
template<typename CINDEX, CINDEX COMP_TOTAL>
void clear(Universe<CINDEX, COMP_TOTAL>& universe);
ComponentHandle handle;
};
/**
* \brief A handle class acts as a pointer to an entity.
* Entities are never accessed directly, always through handles.
* The entity-data itself in stored in the Universe in
* semi-continous storage. The universe pointer is bound to
* the type of the handle.
* Bool tesing is implemented following the safe-bool-idiom.
* This object is farily lightweight, not much bigger than a pointer type.
*/
template<typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
class EntityHandle
{
friend class Universe<CINDEX, COMP_TOTAL>;
public:
using Data = EntityData<CINDEX, COMP_TOTAL>;
public:
/** \brief Public constructor creates an invalid entity-handle (like a nullptr). */
EntityHandle();
/** \brief Returns true, if the entity is still valid, that means the underlying data was not deleted. */
bool valid() const;
explicit operator bool() const { return valid(); }
bool operator==(const EntityHandle<CINDEX, COMP_TOTAL>& other) const { return mHandle == other.mHandle &&
mGeneration == other.mGeneration; }
bool operator!=(const EntityHandle<CINDEX, COMP_TOTAL>& other) const { return !(*this == other); }
/**
* \brief Test if the entity has a specific component of type C. Returns true if and only if the
* component is assigned. O(1), just a single bit check.
*/
template<typename C>
bool has() const;
/**
* \brief Destroys the underlying entity through the handle. This will automatically invalidate all other handles.
*/
void destroy() const;
/**
* \brief Adds a new component of type C to the entity, if no other component of that type was added before.
* Returns success. O(k), where k is the number of omponents of fthe entity.
* Adds a set of components at once. This is typically more efficient than adding the components one by one.
* Component types must be default constructible when using this method.
*/
template<typename ... C>
void add() const;
/** \brief same as add, but uses dom::instantiate to construct components with parameters. */
template<typename ... C>
void add(ComponentInstantiator<C>... ci) const;
/**
* \brief Gets a const reference to the requested component. Assumes that the component exists.
* Throws an "No-Component-Found"-error if has<C>() is false when this method is called. O(1).
*/
template<typename C>
const C& get() const;
/**
* \brief Gets a const reference to the requested component. Assumes that the component exists.
* Throws an "No-Component-Found"-error if has<C>() is false when this method is called. O(1).
*/
template<typename C>
C& modify() const;
/**
* \brief Removes the component of type C from the entity. If the component doesnt exists, the method does nothing.
*/
template<typename C>
void rem() const;
/**
* \brief Creates a copy of the entity that shares the given set of components with the same contents.
*/
template<typename ... C>
EntityHandle copy() const;
/** \brief Returns the unique id of the entity. */
EntityID getID() const;
/** \brief Returns a handle that will never be valid. Equivalent to what nullptr is for normal pointers. */
static EntityHandle nullEntity();
/** \brief Instantiates a component with parameters. */
template<typename C, typename ... PARAM>
ComponentInstantiator<C> instantiate(PARAM&&... param);
/** \brief Returns a handle to the underlying universe. */
Universe<CINDEX, COMP_TOTAL>& getUniverse() { return *mUniverse; }
const Universe<CINDEX, COMP_TOTAL>& getUniverse() const { return *mUniverse; }
private:
EntityArrayHandle mHandle;
SubID mGeneration;
Universe<CINDEX, COMP_TOTAL> * mUniverse;
private:
EntityHandle(Universe<CINDEX, COMP_TOTAL>* cUniverse,
EntityArrayHandle cHandle,
SubID cGeneration);
};
/**
* \brief A struct that holds meta information for several entities.
* All entities with the same components assigned share the same metadata.
* Delivers information that can tell for each component type i what index it
* has in the componentList of the entity.
*/
template<typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
struct MetaData
{
friend class EntityData<CINDEX, COMP_TOTAL>;
friend class Universe<CINDEX, COMP_TOTAL>;
private:
std::bitset< COMP_TOTAL > mComponentMask;
std::array<CINDEX, COMP_TOTAL> mMetaData;
unsigned mSharedCount;
MetaData(std::bitset< COMP_TOTAL > initialMask);
};
/**
* \brief Class that models a specific object in the "world". Entities are simply collections of
* components. This class can be seen as an interface to manage the components of the
* entity (add, remove, get, has).
*
* The entity class relies on two template arguments. They can be used to control the space (in bits)
* an entity will require.
*
* CIndex - Entities rely on a specific integer type CIndex for their components storage. Note that this
* type should be always as small as possible since it is the factor that entities grow in size with.
* The default value is the smallest std integer type unsigned char.
*
* CCount - The total number of components of type CIndex. Rising this number will also result in
* rising entitiy size. The default value is 64.
*/
template<typename CINDEX = unsigned short, CINDEX COMP_TOTAL = DEFAULT_COMPONENT_COUNT>
class EntityData
{
friend class Universe<CINDEX, COMP_TOTAL>;
private:
MetaData<CINDEX, COMP_TOTAL>* mMetaData; ///<points to metadata that all entities with the same bitset share
std::vector< ComponentHandle > mComponentHandles; ///<stores indices of assigned component in their managers
};
/**
* \brief Factory- and master-object for entities and their components.
* Components of the same type C are stored in semi-continous space.
* This class provides functionality to create, destroy and retrieve
* components given a type C and a ComponentHandle.
* Metadata for entities is also stored semi-continous.
*
* Template parameters:
* CINDEX ... index type for components
* COMP_TOTAL ... total number of components allowed in the application
*/
template<typename CINDEX = unsigned short, CINDEX COMP_TOTAL = 256>
class Universe
{
friend class EntityHandle<CINDEX, COMP_TOTAL>;
template <class C, typename CI, CI CT> friend class MultiComponent;
template <class C> friend class ComponentInstantiator;
public:
static constexpr std::size_t ENTITY_BLOCK_SIZE = 8192; ///<number of entities in a single, continous memory block
static constexpr std::size_t COMPONENT_BLOCK_SIZE = 8192; ///<number of components in a single, continous memory block
static constexpr std::size_t ENTITY_REUSE_C = 1024; ///<minimum stack size until an entity slot is reused
Universe();
public:
/** \brief Creates an empty entity with a new, unique id and returns a handle to it.*/
EntityHandle<CINDEX, COMP_TOTAL> create();
/** \brief Creates an entity with a new, unique id and assigns all given components to it.
* This is faster then adding the components one by one to an empty entity. */
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> create();
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> create(ComponentInstantiator<C>... ci);
/** \brief Creates n entities with the given components. This is even faster then calling create<C...>()
* n times and should be the typical way of construction n entities that share the same bitfield.
* The function f is called for each created entity. */
template<typename ... C>
void create(std::size_t n, std::function<void(EntityHandle<CINDEX, COMP_TOTAL> e)> f);
private:
/** \brief Checks if entityData belonging to the given handle is still valid. */
bool valid( const EntityHandle<CINDEX, COMP_TOTAL>& e ) const;
/** \brief Destroys the given entity and all components assigned to it. */
void destroyEntity( const EntityHandle<CINDEX, COMP_TOTAL>& e );
/** \brief Creates an instantiate of component type C by copying the component from a given entity. */
template<typename C>
ComponentInstantiator<C> instantiateCopy(const EntityHandle<CINDEX, COMP_TOTAL>& e);
/** \brief Makes a copy of the given entity. The new entity will share all
* Components and component-contents with the given entity. */
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> copyEntity( const EntityHandle<CINDEX, COMP_TOTAL>& e );
/**
* \brief Test if the entity has a specific component of type C. Returns true if and only if the
* component is assigned. O(1), just a single bit check.
*/
template<typename C>
bool hasComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e ) const;
/**
* \brief Adds a new component of type C to the entity, if no other component of that type was added before.
* Returns success. O(k), where k is the number of assigned components.
* Adds a set of components at once. This is typically more efficient than adding the components one by one.
*/
template<typename ... C>
void addComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e, ComponentInstantiator<C>... ci );
/**
* \brief Gets a const reference to the requested component. Assumes that the component exists.
* Throws an "No-Component-Found"-error if has<C>() is false when this method is called. O(1).
*/
template<typename C>
const C& getComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e ) const;
template<typename C>
const C& getComponent( ComponentHandle c ) const;
/**
* \brief Gets a const reference to the requested component. Assumes that the component exists.
* Throws an "No-Component-Found"-error if has<C>() is false when this method is called. O(1).
*/
template<typename C>
C& modifyComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e );
template<typename C>
C& modifyComponent( ComponentHandle c );
/**
* \brief Removes the component of type C from the entity. If the component doesnt exists, the method does nothing.
*/
template<typename C>
void removeComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e );
/** \brief Computes and returns the id of the given entity. */
EntityID getID( const EntityHandle<CINDEX, COMP_TOTAL>& e );
/** \brief After calling this method for an entity, its ensured that the internal
* datastructes are capable of the (new) entity. */
void accommodateEntity( const EntityArrayHandle& e );
/** \brief Called when connected to an EntityData. */
void connect(EntityData<CINDEX, COMP_TOTAL>& data, std::bitset<COMP_TOTAL> mask);
/** \brief Called when disconnected from an EntityData. */
void disconnect(const EntityData<CINDEX, COMP_TOTAL>& data);
public:
/** \brief Helper method to instantiate components with parameters. */
template<typename C, typename ... PARAM>
ComponentInstantiator<C> instantiate(PARAM&&... param);
/** \brief Size method mostly useful for debugging. May useful for something else. */
std::size_t getEntityCount() const;
/** \brief Size method mostly useful for debugging. May useful for something else. */
template<typename C>
std::size_t getComponentCount() const;
private:
std::array< std::unique_ptr<BaseChunkedArray>, COMP_TOTAL> mManagers;
ChunkedArray<EntityData<CINDEX, COMP_TOTAL>, ENTITY_BLOCK_SIZE, ENTITY_REUSE_C> mEntityData;
std::vector<SubID> mGenerations; ///<generation counter for each entity id (mapping is COMPONENT_BLOCK_SIZE*block + index)
std::unordered_map< unsigned long, std::unique_ptr<MetaData<CINDEX, COMP_TOTAL>> > mComponentMetadata; ///<maps bitset to Metadata
MetaData<CINDEX, COMP_TOTAL> mEmptyMeta;
template <typename... C>
struct ComponentUnpacker;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////TEMPLATE_UNPACKING//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename CINDEX, CINDEX COMP_TOTAL>
template < typename C1, typename... C>
struct Universe<CINDEX, COMP_TOTAL>::ComponentUnpacker<C1, C...>
{
static void prepare(std::bitset< COMP_TOTAL >& mask)
{
mask.set(ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID());
ComponentUnpacker<C...>::prepare(mask);
}
static void unpack(Universe<CINDEX, COMP_TOTAL>& universe,
std::vector< ComponentHandle >& handles,
std::bitset< COMP_TOTAL >& oldMask,
std::array< CINDEX, COMP_TOTAL >& positions,
ComponentInstantiator<C1> c1,
ComponentInstantiator<C>... c)
{
if (!oldMask.test(ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID())) //dont add if such a component was already assigned
{
//store the handle at the correct position
if (positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()] < handles.size())
handles.emplace(handles.begin() + positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()], c1.handle);
else
handles.emplace(handles.end(), c1.handle);
}
else //clear the created component if the user made a mistake and added it twice
{
c1.clear(universe);
}
//recursive...
ComponentUnpacker<C...>::unpack(universe, handles, oldMask, positions, c...);
}
static void unpack(std::vector< ComponentHandle >& handles,
std::array< CINDEX, COMP_TOTAL >& positions,
ComponentInstantiator<C1> c1,
ComponentInstantiator<C>... c)
{
//store the handle at the correct position
if (positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()] < handles.size())
handles.emplace(handles.begin() + positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()], c1.handle);
else
handles.emplace(handles.end(), c1.handle);
//recursive...
ComponentUnpacker<C...>::unpack(handles, positions, c...);
}
};
template <typename CINDEX, CINDEX COMP_TOTAL>
template <typename C1>
struct Universe<CINDEX, COMP_TOTAL>::ComponentUnpacker<C1>
{
static void prepare(std::bitset< COMP_TOTAL >& mask)
{
mask.set(ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID());
}
static void unpack(Universe<CINDEX, COMP_TOTAL>& universe,
std::vector< ComponentHandle >& handles,
std::bitset< COMP_TOTAL >& oldMask,
std::array< CINDEX, COMP_TOTAL >& positions,
ComponentInstantiator<C1> c1)
{
if (!oldMask.test(ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID())) //dont add if such a component is already assigned
{
if (positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()] < handles.size())
handles.emplace(handles.begin() + positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()], c1.handle);
else
handles.emplace(handles.end(), c1.handle);
}
else //clear the created component if the user made a mistake and added it twice
{
c1.clear(universe);
}
}
static void unpack(std::vector< ComponentHandle >& handles,
std::array< CINDEX, COMP_TOTAL >& positions,
ComponentInstantiator<C1> c1)
{
if (positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()] < handles.size())
handles.emplace(handles.begin() + positions[ComponentTraits<C1, CINDEX, COMP_TOTAL>::getID()], c1.handle);
else
handles.emplace(handles.end(), c1.handle);
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////IMPLEMENTATION//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
ChunkedArray<T, BLOCK_SIZE, REUSE_C>::ChunkedArray()
{
mBlocks.emplace_back();
mBlocks.back().ptr = alloc.allocate(BLOCK_SIZE);
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
template<typename ...PARAM>
ChunkedArrayHandle ChunkedArray<T, BLOCK_SIZE, REUSE_C>::add(PARAM&&... param)
{
ChunkedArrayHandle h;
if (mFreeSlots.size() > REUSE_C) //reuse a previously abadoned slot
{
h = mFreeSlots.front();
mFreeSlots.pop();
}
else if (mBlocks.back().contentCount >= BLOCK_SIZE) //need to create a new block
{
T* hint = mBlocks.back().ptr + BLOCK_SIZE;
mBlocks.emplace_back();
mBlocks.back().ptr = alloc.allocate(BLOCK_SIZE, hint); //allocate new memory possibly near the existing blocks
h.block = mBlocks.size() -1;
h.index = mBlocks.back().endIndex++;
}
else
{
h.block = mBlocks.size() -1;
h.index = mBlocks.back().endIndex++;
}
++(mBlocks[h.block].contentCount);
std::allocator_traits<std::allocator<T>>::construct(
alloc,
mBlocks[h.block].ptr + h.index,
std::forward<PARAM>(param)... ); //construct component C in place
return h;
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
T& ChunkedArray<T, BLOCK_SIZE, REUSE_C>::get(ChunkedArrayHandle h)
{
return (mBlocks[h.block].ptr)[h.index];
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
const T& ChunkedArray<T, BLOCK_SIZE, REUSE_C>::get(ChunkedArrayHandle h) const
{
return (mBlocks[h.block].ptr)[h.index];
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
void ChunkedArray<T, BLOCK_SIZE, REUSE_C>::destroy(ChunkedArrayHandle h)
{
mFreeSlots.push(h);
--(mBlocks[h.block].contentCount);
std::allocator_traits<std::allocator<T>>::destroy( alloc,
mBlocks[h.block].ptr + h.index);
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
std::size_t ChunkedArray<T, BLOCK_SIZE, REUSE_C>::blockCount() const
{
return mBlocks.size();
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
std::size_t ChunkedArray<T, BLOCK_SIZE, REUSE_C>::size() const
{
std::size_t sum = 0;
for (const auto& block : mBlocks)
sum += block.contentCount;
return sum;
}
template<typename T, std::size_t BLOCK_SIZE, std::size_t REUSE_C>
ChunkedArray<T, BLOCK_SIZE, REUSE_C>::~ChunkedArray()
{
for (const auto& block : mBlocks)
{
alloc.deallocate(block.ptr, BLOCK_SIZE);
}
}
template<typename CINDEX, CINDEX COMP_TOTAL>
CINDEX ComponentTraitsBase<CINDEX, COMP_TOTAL>::newID()
{
static CINDEX idCounter = 0;
if (idCounter < COMP_TOTAL)
{
return idCounter++;
}
else
{
throw(ComponentCountError());
}
}
template<typename C, typename CINDEX, CINDEX COMP_TOTAL>
CINDEX ComponentTraits<C, CINDEX, COMP_TOTAL>::getID()
{
const static CINDEX id = ComponentTraitsBase<CINDEX, COMP_TOTAL>::newID();
return id;
}
inline ComponentCountError::ComponentCountError() :
std::runtime_error("Attempt to create more than the maximum number of components.") {}
template<typename CINDEX, CINDEX COMP_TOTAL>
EntityHandle<CINDEX, COMP_TOTAL>::EntityHandle(Universe<CINDEX, COMP_TOTAL>* cUniverse,
EntityArrayHandle cHandle,
unsigned short cGeneration)
: mHandle(cHandle), mGeneration(cGeneration), mUniverse(cUniverse) {}
template<typename CINDEX, CINDEX COMP_TOTAL>
EntityHandle<CINDEX, COMP_TOTAL>::EntityHandle()
: mHandle(EntityArrayHandle::null()), mGeneration(0), mUniverse(nullptr) {}
template<typename CINDEX, CINDEX COMP_TOTAL>
bool EntityHandle<CINDEX, COMP_TOTAL>::valid() const
{
return mUniverse && mUniverse->valid(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
void EntityHandle<CINDEX, COMP_TOTAL>::destroy() const
{
return mUniverse->destroyEntity(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
bool EntityHandle<CINDEX, COMP_TOTAL>::has() const
{
return mUniverse->template hasComponent<C>(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
void EntityHandle<CINDEX, COMP_TOTAL>::add() const
{
mUniverse->template addComponent<C...>(*this, ComponentInstantiator<C>(*mUniverse)...);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
void EntityHandle<CINDEX, COMP_TOTAL>::add(ComponentInstantiator<C>... ci) const
{
mUniverse->template addComponent<C...>(*this, ci...);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
const C& EntityHandle<CINDEX, COMP_TOTAL>::get() const
{
return mUniverse->template getComponent<C>(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
C& EntityHandle<CINDEX, COMP_TOTAL>::modify() const
{
return mUniverse->template modifyComponent<C>(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
void EntityHandle<CINDEX, COMP_TOTAL>::rem() const
{
mUniverse->template removeComponent<C>(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> EntityHandle<CINDEX, COMP_TOTAL>::copy() const
{
return mUniverse->template copyEntity<C...>(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
EntityID EntityHandle<CINDEX, COMP_TOTAL>::getID() const
{
return mUniverse->getID(*this);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
EntityHandle<CINDEX, COMP_TOTAL> EntityHandle<CINDEX, COMP_TOTAL>::nullEntity()
{
return EntityHandle<CINDEX, COMP_TOTAL>(nullptr, {0,0}, 0);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C, typename ... PARAM>
ComponentInstantiator<C> EntityHandle<CINDEX, COMP_TOTAL>::instantiate(PARAM&&... param)
{
return mUniverse->template instantiate<C>( std::forward<PARAM>(param)... );
}
template<typename CINDEX, CINDEX COMP_TOTAL>
MetaData<CINDEX, COMP_TOTAL>::MetaData(std::bitset< COMP_TOTAL > initialMask) : mComponentMask(initialMask), mSharedCount(0)
{
CINDEX bitc = 0;
for (CINDEX i = 0; i < COMP_TOTAL; ++i)
{
if (initialMask.test(i))
mMetaData[i] = bitc++;
}
}
template<typename CINDEX, CINDEX COMP_TOTAL>
Universe<CINDEX, COMP_TOTAL>::Universe() : mEmptyMeta(std::bitset<COMP_TOTAL>()) {}
template<typename CINDEX, CINDEX COMP_TOTAL>
bool Universe<CINDEX, COMP_TOTAL>::valid( const EntityHandle<CINDEX, COMP_TOTAL>& e ) const
{
return mGenerations[ e.mHandle.block*ENTITY_BLOCK_SIZE + e.mHandle.index ] == e.mGeneration;
}
template<typename CINDEX, CINDEX COMP_TOTAL>
EntityHandle<CINDEX, COMP_TOTAL> Universe<CINDEX, COMP_TOTAL>::create()
{
EntityArrayHandle e = mEntityData.add();
mEntityData.get(e).mMetaData = &mEmptyMeta;
accommodateEntity(e);
return EntityHandle<CINDEX, COMP_TOTAL>(this, e, mGenerations[ e.block*ENTITY_BLOCK_SIZE + e.index ]);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> Universe<CINDEX, COMP_TOTAL>::create()
{
return create<C...>(ComponentInstantiator<C>(*this)...);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> Universe<CINDEX, COMP_TOTAL>::create(ComponentInstantiator<C>... ci)
{
auto e = create(); //create empty entity
e.template add<C...>(ci...);
return e;
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
void Universe<CINDEX, COMP_TOTAL>::create(std::size_t n,
std::function<void(EntityHandle<CINDEX, COMP_TOTAL> e)> f)
{
if (n > 0)
{
std::size_t num = sizeof...(C); //get the number of components
//accomodate a new entity
EntityArrayHandle ehandle = mEntityData.add();
accommodateEntity(ehandle);
EntityData<CINDEX, COMP_TOTAL>& data = mEntityData.get(ehandle);
//setup a sample mask for the components
std::bitset<COMP_TOTAL> sampleMask;
ComponentUnpacker<C...>::prepare(sampleMask);
connect(data, sampleMask);
//create the components and insert them to their correct positions in data.mComponentHandles
data.mComponentHandles.reserve(num);
ComponentUnpacker<C...>::unpack(data.mComponentHandles, data.mMetaData->mMetaData, ComponentInstantiator<C>(*this)...);
EntityHandle<CINDEX, COMP_TOTAL> esample = EntityHandle<CINDEX, COMP_TOTAL>(this, ehandle, mGenerations[ ehandle.block*ENTITY_BLOCK_SIZE + ehandle.index ]);
//invoke the callback for the first entity
f(esample);
for(std::size_t i = 1; i < n; ++i)
{
//invoke the callback all other entities
f(copyEntity<C...>(esample));
}
}
}
template<typename CINDEX, CINDEX COMP_TOTAL>
void Universe<CINDEX, COMP_TOTAL>::destroyEntity( const EntityHandle<CINDEX, COMP_TOTAL>& e )
{
if (!valid(e)) return;
EntityData<CINDEX, COMP_TOTAL>& data = mEntityData.get(e.mHandle);
for(CINDEX i = 0; i < COMP_TOTAL; ++i) //remove all components
{
if (data.mMetaData->mComponentMask.test(i))
{
auto handleIndex = data.mComponentHandles[data.mMetaData->mMetaData[ i ]];
mManagers[ i ].get()->destroy(handleIndex);
}
}
mEntityData.destroy(e.mHandle);
mGenerations[ e.mHandle.block*ENTITY_BLOCK_SIZE + e.mHandle.index ] ++; //invalidates all handles pointing to the deleted entity
}
//helper function
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
ComponentInstantiator<C> Universe<CINDEX, COMP_TOTAL>::instantiateCopy(const EntityHandle<CINDEX, COMP_TOTAL>& e)
{
return ComponentInstantiator<C>(*this, getComponent<C>(e));
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
EntityHandle<CINDEX, COMP_TOTAL> Universe<CINDEX, COMP_TOTAL>::copyEntity( const EntityHandle<CINDEX, COMP_TOTAL>& e )
{
EntityArrayHandle ehandle = mEntityData.add();
accommodateEntity(ehandle);
EntityData<CINDEX, COMP_TOTAL>& data = mEntityData.get(ehandle);
//quick connect, just copy pointer
data.mMetaData = mEntityData.get(e.mHandle).mMetaData;
data.mMetaData->mSharedCount++;
//as above
data.mComponentHandles.reserve(sizeof...(C));
ComponentUnpacker<C...>::unpack(data.mComponentHandles, data.mMetaData->mMetaData, instantiateCopy<C>(e)...);
return EntityHandle<CINDEX, COMP_TOTAL>(this, ehandle, mGenerations[ ehandle.block*ENTITY_BLOCK_SIZE + ehandle.index ]);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>
bool Universe<CINDEX, COMP_TOTAL>::hasComponent( const EntityHandle<CINDEX, COMP_TOTAL>& e ) const
{
return mEntityData.get(e.mHandle).mMetaData->mComponentMask.test(ComponentTraits<C, CINDEX, COMP_TOTAL>::getID());
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename ... C>
void Universe<CINDEX, COMP_TOTAL>::addComponent(const EntityHandle<CINDEX, COMP_TOTAL>& e, ComponentInstantiator<C>... ci)
{
EntityData<CINDEX, COMP_TOTAL>& data = mEntityData.get(e.mHandle);
std::bitset<COMP_TOTAL> mask = data.mMetaData->mComponentMask;
std::bitset<COMP_TOTAL> oldmask = data.mMetaData->mComponentMask;
ComponentUnpacker<C...>::prepare(mask);
disconnect(data);
connect(data, mask);
ComponentUnpacker<C...>::unpack(*this, data.mComponentHandles, oldmask, data.mMetaData->mMetaData, ci...);
}
template<typename CINDEX, CINDEX COMP_TOTAL>
template<typename C>