Skip to main content

Guid.ixx File

Lightweight GUID (globally unique identifier) implementation. More...

Included Headers

#include <atomic> #include <cstdint> #include <helios.core.types>

Namespaces Index

namespacehelios
namespaceutil

Utility functions and helper classes. More...

Classes Index

classGuid

Representative of a Globally Unique Identifier. More...

structhash<helios::util::Guid>

Description

Lightweight GUID (globally unique identifier) implementation.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Guid.ixx
3 * @brief Lightweight GUID (globally unique identifier) implementation.
4 */
5module;
6
7#include <atomic>
8#include <cstdint>
9
10export module helios.util.Guid;
11
12import helios.core.types;
13
14
15export namespace helios::util {
16
17
18 /**
19 * @brief Representative of a Globally Unique Identifier.
20 *
21 * Generating GUIDs with this class is considered thread-safe.
22 */
23 class Guid final {
24 private:
25 explicit Guid(uint64_t value) noexcept : value_(value) {}
26 uint64_t value_{};
27
28 public:
29
30 /**
31 * @brief Unsafe Guid initializer for (local) var initialization.
32 */
34
35 /**
36 * @brief Compares two Guid instances for equality.
37 *
38 * @param guid The Guid to compare against.
39 *
40 * @return `true` if both Guids have the same underlying value, `false` otherwise.
41 */
42 constexpr bool operator==(const Guid& guid) const = default;
43
44 /**
45 * @brief Compares two Guid instances for inequality.
46 *
47 * @param guid The Guid to compare against.
48 *
49 * @return `true` if the Guids have different underlying values, `false` otherwise.
50 */
51 constexpr bool operator!=(const Guid& guid) const = default;
52
53 /**
54 * @brief Less-than comparison operator, enabling ordering of Guid instances.
55 *
56 * @param guid The Guid to compare against.
57 *
58 * @return `true` if this Guid's value is less than the other, `false` otherwise.
59 */
60 constexpr bool operator<(const Guid& guid) const noexcept {
61 return value_ < guid.value();
62 }
63
64 /**
65 * @brief Greater-than comparison operator, enabling ordering of Guid instances.
66 *
67 * @param guid The Guid to compare against.
68 *
69 * @return `true` if this Guid's value is greater than the other, `false` otherwise.
70 */
71 constexpr bool operator>(const Guid& guid) const noexcept {
72 return value_ > guid.value();
73 }
74
75
76 /**
77 * @brief Generates a new Guid.
78 *
79 * This function produces a new, unique Guid value. It is safe to call from
80 * multiple threads.
81 *
82 * @return A newly generated `Guid` instance.
83 */
84 static Guid generate() noexcept {
85 static std::atomic<uint64_t> counter{1};
86 return Guid(counter.fetch_add(1, std::memory_order_relaxed));
87 }
88
89 /**
90 * @brief Returns the raw 64-bit value of this Guid.
91 *
92 * @return The underlying uint64_t value representing this Guid.
93 */
94 [[nodiscard]] constexpr uint64_t value() const noexcept {
95 return value_;
96 }
97
98 /**
99 * @brief Calculates a hash fot this GUID.
100 *
101 * The hash simply defaults to the value_ of this Guid.
102 *
103 * @return The hash value for this Guid.
104 */
105 [[nodiscard]] std::size_t hash() const noexcept {
106 return std::hash<uint64_t>{}(value_);
107 }
108
109 };
110
111} // namespace helios::util
112
113
114template<>
115struct std::hash<helios::util::Guid> {
116 std::size_t operator()(const helios::util::Guid& guid) const noexcept {
117 return guid.hash();
118 }
119};

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.