| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/HashCombine.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
class JPH_EXPORT CharacterID |
| 13 |
{ |
| 14 |
public: |
| 15 |
JPH_OVERRIDE_NEW_DELETE |
| 16 |
|
| 17 |
static constexpr uint32 cInvalidCharacterID = 0xffffffff; |
| 18 |
|
| 19 |
|
| 20 |
CharacterID() : |
| 21 |
mID(cInvalidCharacterID) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
explicit CharacterID(uint32 inID) : |
| 27 |
mID(inID) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
inline uint32 GetValue() const |
| 33 |
{ |
| 34 |
return mID; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
inline bool IsInvalid() const |
| 39 |
{ |
| 40 |
return mID == cInvalidCharacterID; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
inline bool operator == (const CharacterID &inRHS) const |
| 45 |
{ |
| 46 |
return mID == inRHS.mID; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
inline bool operator != (const CharacterID &inRHS) const |
| 51 |
{ |
| 52 |
return mID != inRHS.mID; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
inline bool operator < (const CharacterID &inRHS) const |
| 57 |
{ |
| 58 |
return mID < inRHS.mID; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
inline bool operator > (const CharacterID &inRHS) const |
| 63 |
{ |
| 64 |
return mID > inRHS.mID; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
inline uint64 GetHash() const |
| 69 |
{ |
| 70 |
return Hash<uint32>{} (mID); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
static CharacterID sNextCharacterID() |
| 75 |
{ |
| 76 |
for (;;) |
| 77 |
{ |
| 78 |
uint32 next = sNextID.fetch_add(1, std::memory_order_relaxed); |
| 79 |
if (next != cInvalidCharacterID) |
| 80 |
return CharacterID(next); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static void sSetNextCharacterID(uint32 inNextValue = 1) |
| 86 |
{ |
| 87 |
sNextID.store(inNextValue, std::memory_order_relaxed); |
| 88 |
} |
| 89 |
|
| 90 |
private: |
| 91 |
|
| 92 |
inline static atomic<uint32> sNextID = 1; |
| 93 |
|
| 94 |
|
| 95 |
uint32 mID; |
| 96 |
}; |
| 97 |
|
| 98 |
JPH_NAMESPACE_END |