Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Character/CharacterID.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2025 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/HashCombine.h>78JPH_NAMESPACE_BEGIN910/// ID of a character. Used primarily to identify deleted characters and to sort deterministically.11class JPH_EXPORT CharacterID12{13public:14JPH_OVERRIDE_NEW_DELETE1516static constexpr uint32 cInvalidCharacterID = 0xffffffff; ///< The value for an invalid character ID1718/// Construct invalid character ID19CharacterID() :20mID(cInvalidCharacterID)21{22}2324/// Construct with specific value, make sure you don't use the same value twice!25explicit CharacterID(uint32 inID) :26mID(inID)27{28}2930/// Get the numeric value of the ID31inline uint32 GetValue() const32{33return mID;34}3536/// Check if the ID is valid37inline bool IsInvalid() const38{39return mID == cInvalidCharacterID;40}4142/// Equals check43inline bool operator == (const CharacterID &inRHS) const44{45return mID == inRHS.mID;46}4748/// Not equals check49inline bool operator != (const CharacterID &inRHS) const50{51return mID != inRHS.mID;52}5354/// Smaller than operator, can be used for sorting characters55inline bool operator < (const CharacterID &inRHS) const56{57return mID < inRHS.mID;58}5960/// Greater than operator, can be used for sorting characters61inline bool operator > (const CharacterID &inRHS) const62{63return mID > inRHS.mID;64}6566/// Get the hash for this character ID67inline uint64 GetHash() const68{69return Hash<uint32>{} (mID);70}7172/// Generate the next available character ID73static CharacterID sNextCharacterID()74{75for (;;)76{77uint32 next = sNextID.fetch_add(1, std::memory_order_relaxed);78if (next != cInvalidCharacterID)79return CharacterID(next);80}81}8283/// Set the next available character ID, can be used after destroying all character to prepare for a second deterministic run84static void sSetNextCharacterID(uint32 inNextValue = 1)85{86sNextID.store(inNextValue, std::memory_order_relaxed);87}8889private:90/// Next character ID to be assigned91inline static atomic<uint32> sNextID = 1;9293/// ID value94uint32 mID;95};9697JPH_NAMESPACE_END9899100