Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::Uuid Class Reference

A class representing a universally unique identifier (UUID). More...

#include <uuid.hpp>

Public Member Functions

constexpr Uuid () noexcept=default
 Default constructor creates an invalid UUID (all zeros).
 
 Uuid (std::string_view str)
 Construct a UUID from a string representation.
 
 Uuid (std::span< const std::byte > bytes)
 Construct a UUID from a span of bytes.
 
 Uuid (const Uuid &)=default
 
 Uuid (Uuid &&) noexcept=default
 
 ~Uuid ()=default
 
Uuidoperator= (const Uuid &)=default
 
Uuidoperator= (Uuid &&) noexcept=default
 
std::string ToString () const
 Convert the UUID to its string representation.
 
auto AsBytes () const -> std::span< const std::byte >
 Get the UUID as a span of bytes.
 
bool operator== (const Uuid &other) const noexcept
 
bool operator!= (const Uuid &other) const noexcept
 
bool operator< (const Uuid &other) const noexcept
 
constexpr bool Valid () const noexcept
 Check if the UUID is valid (not all zeros).
 
size_t Hash () const noexcept
 Compute a hash value for the UUID.
 

Static Public Member Functions

static Uuid Generate ()
 Generate a new random UUID.
 

Friends

class UuidGenerator
 
void swap (Uuid &lhs, Uuid &rhs) noexcept
 Swap two UUIDs.
 

Detailed Description

A class representing a universally unique identifier (UUID).

Definition at line 25 of file uuid.hpp.

Constructor & Destructor Documentation

◆ Uuid() [1/5]

constexpr helios::Uuid::Uuid ( )
constexprdefaultnoexcept

Default constructor creates an invalid UUID (all zeros).

◆ Uuid() [2/5]

helios::Uuid::Uuid ( std::string_view  str)
inlineexplicit

Construct a UUID from a string representation.

If the string is not a valid UUID, the resulting UUID will be invalid.

Parameters
strThe string representation of the UUID.

Definition at line 108 of file uuid.hpp.

108 {
109 if (str.empty()) [[unlikely]] {
110 return;
111 }
112
113 auto result = uuids::uuid::from_string(str.data());
114 if (result.has_value()) {
115 uuid_ = result.value();
116 }
117}

◆ Uuid() [3/5]

helios::Uuid::Uuid ( std::span< const std::byte >  bytes)
inlineexplicit

Construct a UUID from a span of bytes.

The span must be exactly 16 bytes long; otherwise, the resulting UUID will be invalid.

Parameters
bytesA span of bytes representing the UUID.

Definition at line 119 of file uuid.hpp.

119 {
120 if (bytes.size() == 16) {
121 const auto* begin = std::bit_cast<const uint8_t*>(bytes.data());
122 const uint8_t* end = begin + bytes.size();
123 uuid_ = uuids::uuid(begin, end);
124 }
125}

◆ Uuid() [4/5]

helios::Uuid::Uuid ( const Uuid )
default

◆ Uuid() [5/5]

helios::Uuid::Uuid ( Uuid &&  )
defaultnoexcept

◆ ~Uuid()

helios::Uuid::~Uuid ( )
default

Member Function Documentation

◆ AsBytes()

auto helios::Uuid::AsBytes ( ) const -> std::span<const std::byte>
inline

Get the UUID as a span of bytes.

Returns
A span of bytes representing the UUID, or an empty span if the UUID is invalid.

Definition at line 149 of file uuid.hpp.

149 {
150 if (!Valid()) {
151 return {};
152 }
153 const auto bytes = uuid_.as_bytes();
154 return {bytes.data(), bytes.size()};
155}
constexpr bool Valid() const noexcept
Check if the UUID is valid (not all zeros).
Definition uuid.hpp:81

◆ Generate()

Uuid helios::Uuid::Generate ( )
inlinestatic

Generate a new random UUID.

Uses a thread_local generator for thread-safety without locks. Each thread maintains its own generator state for optimal performance.

Returns
A new random UUID.

Definition at line 127 of file uuid.hpp.

127 {
128 // Create a thread-local engine and pass it (as an lvalue) to the generator.
129 // This avoids binding a temporary to a non-const reference.
130 static thread_local std::random_device rd;
131 static thread_local std::array<std::random_device::result_type, std::mt19937::state_size> seed_data = []() {
132 std::array<std::random_device::result_type, std::mt19937::state_size> data = {};
133 std::ranges::generate(data, std::ref(rd));
134 return data;
135 }();
136 static thread_local std::seed_seq seq(seed_data.begin(), seed_data.end());
137 static thread_local std::mt19937 engine(seq);
138 static thread_local uuids::uuid_random_generator gen(engine);
139 return Uuid(gen());
140}
constexpr Uuid() noexcept=default
Default constructor creates an invalid UUID (all zeros).

◆ Hash()

size_t helios::Uuid::Hash ( ) const
inlinenoexcept

Compute a hash value for the UUID.

Returns
A hash value for the UUID.

Definition at line 87 of file uuid.hpp.

87{ return std::hash<uuids::uuid>{}(uuid_); }

◆ operator!=()

bool helios::Uuid::operator!= ( const Uuid other) const
inlinenoexcept

Definition at line 73 of file uuid.hpp.

73{ return !(*this == other); }

◆ operator<()

bool helios::Uuid::operator< ( const Uuid other) const
inlinenoexcept

Definition at line 75 of file uuid.hpp.

75{ return uuid_ < other.uuid_; }

◆ operator=() [1/2]

Uuid & helios::Uuid::operator= ( const Uuid )
default

◆ operator=() [2/2]

Uuid & helios::Uuid::operator= ( Uuid &&  )
defaultnoexcept

◆ operator==()

bool helios::Uuid::operator== ( const Uuid other) const
inlinenoexcept

Definition at line 72 of file uuid.hpp.

72{ return uuid_ == other.uuid_; }

◆ ToString()

std::string helios::Uuid::ToString ( ) const
inline

Convert the UUID to its string representation.

Returns
The string representation of the UUID, or an empty string if the UUID is invalid.

Definition at line 142 of file uuid.hpp.

142 {
143 if (!Valid()) {
144 return {};
145 }
146 return uuids::to_string(uuid_);
147}

◆ Valid()

constexpr bool helios::Uuid::Valid ( ) const
inlineconstexprnoexcept

Check if the UUID is valid (not all zeros).

Returns
True if the UUID is valid, false otherwise.

Definition at line 81 of file uuid.hpp.

81{ return !uuid_.is_nil(); }

Friends And Related Symbol Documentation

◆ swap

void swap ( Uuid lhs,
Uuid rhs 
)
friend

Swap two UUIDs.

Parameters
lhsThe first UUID.
rhsThe second UUID.

Definition at line 94 of file uuid.hpp.

94{ lhs.uuid_.swap(rhs.uuid_); }

◆ UuidGenerator

friend class UuidGenerator
friend

Definition at line 105 of file uuid.hpp.