Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
CharacterID.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2025 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
13{
14public:
16
17 static constexpr uint32 cInvalidCharacterID = 0xffffffff;
18
21 mID(cInvalidCharacterID)
22 {
23 }
24
26 explicit CharacterID(uint32 inID) :
27 mID(inID)
28 {
29 }
30
32 inline uint32 GetValue() const
33 {
34 return mID;
35 }
36
38 inline bool IsInvalid() const
39 {
40 return mID == cInvalidCharacterID;
41 }
42
44 inline bool operator == (const CharacterID &inRHS) const
45 {
46 return mID == inRHS.mID;
47 }
48
50 inline bool operator != (const CharacterID &inRHS) const
51 {
52 return mID != inRHS.mID;
53 }
54
56 inline bool operator < (const CharacterID &inRHS) const
57 {
58 return mID < inRHS.mID;
59 }
60
62 inline bool operator > (const CharacterID &inRHS) const
63 {
64 return mID > inRHS.mID;
65 }
66
68 inline uint64 GetHash() const
69 {
70 return Hash<uint32>{} (mID);
71 }
72
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
85 static void sSetNextCharacterID(uint32 inNextValue = 1)
86 {
87 sNextID.store(inNextValue, std::memory_order_relaxed);
88 }
89
90private:
92 inline static atomic<uint32> sNextID = 1;
93
95 uint32 mID;
96};
97
#define JPH_EXPORT
Definition Core.h:275
std::uint64_t uint64
Definition Core.h:490
#define JPH_NAMESPACE_END
Definition Core.h:418
std::uint32_t uint32
Definition Core.h:489
#define JPH_NAMESPACE_BEGIN
Definition Core.h:412
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
ID of a character. Used primarily to identify deleted characters and to sort deterministically.
Definition CharacterID.h:13
static void sSetNextCharacterID(uint32 inNextValue=1)
Set the next available character ID, can be used after destroying all character to prepare for a seco...
Definition CharacterID.h:85
CharacterID()
Construct invalid character ID.
Definition CharacterID.h:20
uint64 GetHash() const
Get the hash for this character ID.
Definition CharacterID.h:68
static CharacterID sNextCharacterID()
Generate the next available character ID.
Definition CharacterID.h:74
bool IsInvalid() const
Check if the ID is valid.
Definition CharacterID.h:38
uint32 GetValue() const
Get the numeric value of the ID.
Definition CharacterID.h:32
CharacterID(uint32 inID)
Construct with specific value, make sure you don't use the same value twice!
Definition CharacterID.h:26
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59