Skip to main content

StrongId.ixx File

Strongly-typed identifier template. More...

Included Headers

#include <cstdint> #include <string_view> #include <helios.core.types> #include <helios.core.algorithms>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespacedata

Core data structures for type indexing. More...

Classes Index

structStrongId<Tag, Underlying>

A strongly-typed identifier using tag-based type safety. More...

structhash<helios::core::data::StrongId< Tag, Underlying >>

Description

Strongly-typed identifier template.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file StrongId.ixx
3 * @brief Strongly-typed identifier template.
4 */
5module;
6
7#include <cstdint>
8#include <string_view>
9
10export module helios.core.data.StrongId;
11
12import helios.core.algorithms;
13import helios.core.types;
14
15export namespace helios::core::data {
16
17 /**
18 * @brief A strongly-typed identifier using tag-based type safety.
19 *
20 * @details Provides compile-time type safety for identifiers by
21 * associating each ID type with a unique tag struct. Supports
22 * compile-time string hashing via FNV-1a algorithm.
23 *
24 * @tparam Tag Empty struct used to distinguish different ID types.
25 * @tparam Underlying The underlying integral type (default: uint32_t).
26 */
27 template<typename Tag, typename Underlying = uint32_t>
28 struct StrongId {
29 static_assert(std::is_integral_v<Underlying>);
30
31 private:
32
33 /**
34 * @brief The underlying ID value.
35 */
36 Underlying id_{};
37
38 /**
39 * @brief Constructs from a raw underlying value.
40 *
41 * @param id The raw ID value.
42 */
43 explicit constexpr StrongId(const Underlying id) noexcept
44 : id_(id) {
45 }
46
47 public:
48
49 /**
50 * @brief Constructs from a compile-time string.
51 *
52 * @details Uses FNV-1a hashing to convert the string to an ID.
53 *
54 * @param str The string to hash.
55 */
56 explicit constexpr StrongId(const std::string_view str) noexcept
58
59 /**
60 * @brief Constructs an uninitialized ID.
61 *
62 * @param no_init_t Tag type indicating no initialization.
63 */
65
66 /**
67 * @brief Default constructor creating an uninitialized ID.
68 */
69 explicit constexpr StrongId() : StrongId(helios::core::types::no_init){};
70
71 /**
72 * @brief Returns the underlying ID value.
73 *
74 * @return The raw underlying value.
75 */
76 [[nodiscard]] Underlying value() const noexcept {
77 return id_;
78 }
79
80 /**
81 * @brief Checks if the ID is valid (non-zero).
82 *
83 * @return True if the ID is non-zero.
84 */
85 [[nodiscard]] explicit operator bool() const noexcept {
86 return id_ != 0;
87 }
88
89 /**
90 * @brief Equality comparison.
91 */
92 friend constexpr bool operator==(StrongId, StrongId) noexcept = default;
93
94 /**
95 * @brief Less-than comparison for ordering.
96 *
97 * @param other The other ID to compare.
98 *
99 * @return True if this ID is less than the other.
100 */
101 constexpr bool operator<(const StrongId& other) const noexcept {
102 return id_ < other.id_;
103 }
104
105 /**
106 * @brief Greater-than comparison for ordering.
107 *
108 * @param other The other ID to compare.
109 *
110 * @return True if this ID is greater than the other.
111 */
112 constexpr bool operator>(const StrongId& other) const noexcept {
113 return id_ > other.id_;
114 }
115
116 };
117
118}
119
120
121template<typename Tag, typename Underlying>
122struct std::hash<helios::core::data::StrongId<Tag, Underlying>> {
123 std::size_t operator()(const helios::core::data::StrongId<Tag, Underlying>& id) const noexcept {
124 return id.value();
125 }
126
127};

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.