Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/CharacterID.h
9912 views
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
7
#include <Jolt/Core/HashCombine.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// ID of a character. Used primarily to identify deleted characters and to sort deterministically.
12
class JPH_EXPORT CharacterID
13
{
14
public:
15
JPH_OVERRIDE_NEW_DELETE
16
17
static constexpr uint32 cInvalidCharacterID = 0xffffffff; ///< The value for an invalid character ID
18
19
/// Construct invalid character ID
20
CharacterID() :
21
mID(cInvalidCharacterID)
22
{
23
}
24
25
/// Construct with specific value, make sure you don't use the same value twice!
26
explicit CharacterID(uint32 inID) :
27
mID(inID)
28
{
29
}
30
31
/// Get the numeric value of the ID
32
inline uint32 GetValue() const
33
{
34
return mID;
35
}
36
37
/// Check if the ID is valid
38
inline bool IsInvalid() const
39
{
40
return mID == cInvalidCharacterID;
41
}
42
43
/// Equals check
44
inline bool operator == (const CharacterID &inRHS) const
45
{
46
return mID == inRHS.mID;
47
}
48
49
/// Not equals check
50
inline bool operator != (const CharacterID &inRHS) const
51
{
52
return mID != inRHS.mID;
53
}
54
55
/// Smaller than operator, can be used for sorting characters
56
inline bool operator < (const CharacterID &inRHS) const
57
{
58
return mID < inRHS.mID;
59
}
60
61
/// Greater than operator, can be used for sorting characters
62
inline bool operator > (const CharacterID &inRHS) const
63
{
64
return mID > inRHS.mID;
65
}
66
67
/// Get the hash for this character ID
68
inline uint64 GetHash() const
69
{
70
return Hash<uint32>{} (mID);
71
}
72
73
/// Generate the next available character ID
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
/// Set the next available character ID, can be used after destroying all character to prepare for a second deterministic run
85
static void sSetNextCharacterID(uint32 inNextValue = 1)
86
{
87
sNextID.store(inNextValue, std::memory_order_relaxed);
88
}
89
90
private:
91
/// Next character ID to be assigned
92
inline static atomic<uint32> sNextID = 1;
93
94
/// ID value
95
uint32 mID;
96
};
97
98
JPH_NAMESPACE_END
99
100