Plasma Engine  2.0
Loading...
Searching...
No Matches
Id.h
1#pragma once
2
4
5#include <Foundation/Basics.h>
6
8#define PL_DECLARE_ID_TYPE(name, instanceIndexBits, generationBits) \
9 static const StorageType MAX_INSTANCES = (1ULL << instanceIndexBits); \
10 static const StorageType INVALID_INSTANCE_INDEX = MAX_INSTANCES - 1; \
11 static const StorageType INDEX_AND_GENERATION_MASK = ((plUInt64) - 1) >> (64 - (instanceIndexBits + generationBits)); \
12 PL_DECLARE_POD_TYPE(); \
13 PL_ALWAYS_INLINE name() \
14 { \
15 m_Data = INVALID_INSTANCE_INDEX; \
16 } \
17 PL_ALWAYS_INLINE explicit name(StorageType internalData) \
18 { \
19 m_Data = internalData; \
20 } \
21 PL_ALWAYS_INLINE bool operator==(const name other) const \
22 { \
23 return m_Data == other.m_Data; \
24 } \
25 PL_ALWAYS_INLINE bool operator!=(const name other) const \
26 { \
27 return m_Data != other.m_Data; \
28 } \
29 PL_ALWAYS_INLINE bool operator<(const name other) const \
30 { \
31 return m_Data < other.m_Data; \
32 } \
33 PL_ALWAYS_INLINE void Invalidate() \
34 { \
35 m_Data = INVALID_INSTANCE_INDEX; \
36 } \
37 PL_ALWAYS_INLINE bool IsInvalidated() const \
38 { \
39 return m_Data == INVALID_INSTANCE_INDEX; \
40 } \
41 PL_ALWAYS_INLINE bool IsIndexAndGenerationEqual(const name other) const \
42 { \
43 return (m_Data & INDEX_AND_GENERATION_MASK) == (other.m_Data & INDEX_AND_GENERATION_MASK); \
44 }
45
46
50template <plUInt32 InstanceIndexBits, plUInt32 GenerationBits>
52{
53 enum
54 {
55 STORAGE_SIZE = ((InstanceIndexBits + GenerationBits - 1) / 8) + 1
56 };
57 using StorageType = typename plSizeToType<STORAGE_SIZE>::Type;
58
59 PL_DECLARE_ID_TYPE(plGenericId, InstanceIndexBits, GenerationBits);
60
61 PL_ALWAYS_INLINE plGenericId(StorageType instanceIndex, StorageType generation)
62 {
63 m_Data = 0;
64 m_InstanceIndex = instanceIndex;
65 m_Generation = generation;
66 }
67
68 union
69 {
70 StorageType m_Data;
71 struct
72 {
73 StorageType m_InstanceIndex : InstanceIndexBits;
74 StorageType m_Generation : GenerationBits;
75 };
76 };
77};
78
79#define PL_DECLARE_HANDLE_TYPE(name, idType) \
80public: \
81 PL_DECLARE_POD_TYPE(); \
82 PL_ALWAYS_INLINE name() {} \
83 PL_ALWAYS_INLINE explicit name(idType internalId) \
84 : m_InternalId(internalId) \
85 { \
86 } \
87 PL_ALWAYS_INLINE bool operator==(const name other) const \
88 { \
89 return m_InternalId == other.m_InternalId; \
90 } \
91 PL_ALWAYS_INLINE bool operator!=(const name other) const \
92 { \
93 return m_InternalId != other.m_InternalId; \
94 } \
95 PL_ALWAYS_INLINE bool operator<(const name other) const \
96 { \
97 return m_InternalId < other.m_InternalId; \
98 } \
99 PL_ALWAYS_INLINE void Invalidate() \
100 { \
101 m_InternalId.Invalidate(); \
102 } \
103 PL_ALWAYS_INLINE bool IsInvalidated() const \
104 { \
105 return m_InternalId.IsInvalidated(); \
106 } \
107 PL_ALWAYS_INLINE idType GetInternalID() const \
108 { \
109 return m_InternalId; \
110 } \
111 using IdType = idType; \
112 \
113protected: \
114 idType m_InternalId; \
115 operator idType() \
116 { \
117 return m_InternalId; \
118 } \
119 operator const idType() const \
120 { \
121 return m_InternalId; \
122 }
A generic id class that holds an id combined of an instance index and a generation counter.
Definition Id.h:52
Helper struct to get a storage type from a size in byte.
Definition Types.h:150