ezEngine  Milestone 9
Id.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <Foundation/Basics.h>
6 
8 #define EZ_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 = (1ULL << (instanceIndexBits + generationBits)) - 1; \
12  EZ_DECLARE_POD_TYPE(); \
13  EZ_ALWAYS_INLINE name() { m_Data = INVALID_INSTANCE_INDEX; } \
14  EZ_ALWAYS_INLINE explicit name(StorageType internalData) { m_Data = internalData; } \
15  EZ_ALWAYS_INLINE bool operator==(const name other) const { return m_Data == other.m_Data; } \
16  EZ_ALWAYS_INLINE bool operator!=(const name other) const { return m_Data != other.m_Data; } \
17  EZ_ALWAYS_INLINE bool operator<(const name other) const { return m_Data < other.m_Data; } \
18  EZ_ALWAYS_INLINE void Invalidate() { m_Data = INVALID_INSTANCE_INDEX; } \
19  EZ_ALWAYS_INLINE bool IsInvalidated() const { return m_Data == INVALID_INSTANCE_INDEX; } \
20  EZ_ALWAYS_INLINE bool IsIndexAndGenerationEqual(const name other) const \
21  { \
22  return (m_Data & INDEX_AND_GENERATION_MASK) == (other.m_Data & INDEX_AND_GENERATION_MASK); \
23  }
24 
25 
29 template <ezUInt32 InstanceIndexBits, ezUInt32 GenerationBits>
31 {
32  enum
33  {
34  STORAGE_SIZE = ((InstanceIndexBits + GenerationBits - 1) / 8) + 1
35  };
36  typedef typename ezSizeToType<STORAGE_SIZE>::Type StorageType;
37 
38  EZ_DECLARE_ID_TYPE(ezGenericId, InstanceIndexBits, GenerationBits);
39 
40  EZ_ALWAYS_INLINE ezGenericId(StorageType instanceIndex, StorageType generation)
41  {
42  m_Data = 0;
43  m_InstanceIndex = instanceIndex;
44  m_Generation = generation;
45  }
46 
47  union {
48  StorageType m_Data;
49  struct
50  {
51  StorageType m_InstanceIndex : InstanceIndexBits;
52  StorageType m_Generation : GenerationBits;
53  };
54  };
55 };
56 
57 #define EZ_DECLARE_HANDLE_TYPE(name, idType) \
58 public: \
59  EZ_DECLARE_POD_TYPE(); \
60  EZ_ALWAYS_INLINE name() {} \
61  EZ_ALWAYS_INLINE explicit name(idType internalId) \
62  : m_InternalId(internalId) \
63  { \
64  } \
65  EZ_ALWAYS_INLINE bool operator==(const name other) const { return m_InternalId == other.m_InternalId; } \
66  EZ_ALWAYS_INLINE bool operator!=(const name other) const { return m_InternalId != other.m_InternalId; } \
67  EZ_ALWAYS_INLINE bool operator<(const name other) const { return m_InternalId < other.m_InternalId; } \
68  EZ_ALWAYS_INLINE void Invalidate() { m_InternalId.Invalidate(); } \
69  EZ_ALWAYS_INLINE bool IsInvalidated() const { return m_InternalId.IsInvalidated(); } \
70  EZ_ALWAYS_INLINE idType GetInternalID() const { return m_InternalId; } \
71  typedef idType IdType; \
72  \
73 protected: \
74  idType m_InternalId; \
75  operator idType() { return m_InternalId; } \
76  operator const idType() const { return m_InternalId; }
77 
A generic id class that holds an id combined of an instance index and a generation counter...
Definition: Id.h:30
Helper struct to get a storage type from a size in byte.
Definition: Types.h:187