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
31 inline uint32 GetValue() const
32 {
33 return mID;
34 }
35
37 inline bool IsInvalid() const
38 {
39 return mID == cInvalidCharacterID;
40 }
41
43 inline bool operator == (const CharacterID &inRHS) const
44 {
45 return mID == inRHS.mID;
46 }
47
49 inline bool operator != (const CharacterID &inRHS) const
50 {
51 return mID != inRHS.mID;
52 }
53
55 inline bool operator < (const CharacterID &inRHS) const
56 {
57 return mID < inRHS.mID;
58 }
59
61 inline bool operator > (const CharacterID &inRHS) const
62 {
63 return mID > inRHS.mID;
64 }
65
67 inline uint64 GetHash() const
68 {
69 return Hash<uint32>{} (mID);
70 }
71
74 {
75 for (;;)
76 {
77 uint32 next = sNextID.fetch_add(1, std::memory_order_relaxed);
78 if (next != cInvalidCharacterID)
79 return CharacterID(next);
80 }
81 }
82
84 static void sSetNextCharacterID(uint32 inNextValue = 1)
85 {
86 sNextID.store(inNextValue, std::memory_order_relaxed);
87 }
88
89private:
91 inline static atomic<uint32> sNextID = 1;
92
94 uint32 mID;
95};
96
#define JPH_EXPORT
Definition Core.h:271
std::uint64_t uint64
Definition Core.h:485
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
#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:84
CharacterID()
Construct invalid character ID.
Definition CharacterID.h:20
uint64 GetHash() const
Get the hash for this character ID.
Definition CharacterID.h:67
static CharacterID sNextCharacterID()
Generate the next available character ID.
Definition CharacterID.h:73
bool IsInvalid() const
Check if the ID is valid.
Definition CharacterID.h:37
uint32 GetValue() const
Definition CharacterID.h:31
CharacterID(uint32 inID)
Construct from index and sequence number combined in a single uint32 (use with care!...
Definition CharacterID.h:26
Fallback hash function that calls T::GetHash()
Definition HashCombine.h:59