IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
stringid.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/base.hxx>
6
7namespace ice
8{
9
11 template<bool DebugFields>
13
17 >;
18
23 using StringID_Arg = std::conditional_t<
25 StringID const&,
27 >;
28
30 {
31
39
40
41 using StringID_DebugNameHint = char[24];
42
44 {
45 char const* value;
47 };
48
54
58 struct StringID_Tag;
59
60 } // namespace detail::stringid_type_v3
61
63
64
65 template<>
66 struct BaseStringID<false>
67 {
69
71
72 constexpr operator ice::StringID_Hash() const noexcept;
73 constexpr operator ice::BaseStringID<true>() const noexcept;
74 constexpr bool operator==(ice::StringID_Hash strid_hash) const noexcept;
75 };
76
77 template<>
78 struct BaseStringID<true>
79 {
81
84
85 constexpr operator ice::StringID_Hash() const noexcept;
86 constexpr operator ice::BaseStringID<false>() const noexcept;
87 constexpr bool operator==(ice::StringID_Hash strid_hash) const noexcept;
88 };
89
90 static constexpr ice::StringID StringID_Invalid{ .value = StringID_Hash{ } };
91
92 constexpr auto stringid(std::string_view value) noexcept
93 {
94 using namespace ice::detail::murmur2_hash;
96
98 {
99 if (std::is_constant_evaluated())
100 {
101 return BaseStringID<true> {
102 .value = { .value = hash_result.h[0] },
103 .debug_info = { .name_value = { value.data(), char('\xff') } }
104 };
105 }
106 else
107 {
108 BaseStringID<true> result{
109 .value = { .value = hash_result.h[0] },
110 .debug_info = {.name_value = {.consteval_flag = '\0'}}
111 };
112
113 size_t const cstr_size = value.size();
114 size_t const origin_size = std::size(result.debug_info.name_hint);
115
116 size_t const copy_count = std::min(origin_size, cstr_size);
117 size_t const copy_offset = std::max(size_t{ 0 }, cstr_size - copy_count);
118
119
120 ice::i32 i = 0;
121 for (auto& v : result.debug_info.name_hint)
122 {
123 if (i < copy_count)
124 {
125 v = value[copy_offset + i];
126 }
127 else
128 {
129 v = char{};
130 }
131
132 i += 1;
133 }
134
135 if (copy_offset > 0)
136 {
137 result.debug_info.name_hint[0] = '~';
138 }
139 return result;
140 }
141 }
142 else
143 {
144 return BaseStringID<false> {
145 .value = { .value = hash_result.h[0] }
146 };
147 }
148 }
149
150 constexpr auto stringid(const char* string, size_t size) noexcept
151 {
152 return stringid(std::string_view{ string, size });
153 }
154
155 constexpr auto stringid_hint(ice::BaseStringID<false> val) noexcept -> std::string_view
156 {
157 return { };
158 }
159
160 constexpr auto stringid_hint(ice::BaseStringID<true> const& val) noexcept -> std::string_view
161 {
162 if (std::is_constant_evaluated())
163 {
164 return val.debug_info.name_value.value;
165 }
166 else
167 {
168 if (val.debug_info.name_value.consteval_flag == char('\xff'))
169 {
170 return val.debug_info.name_value.value;
171 }
172 else
173 {
174 char const* end = val.debug_info.name_hint + 23;
175 // Need to find the end of the string (capped at 24 characters)
176 while (*end == '\0' && end > val.debug_info.name_hint) --end;
177 return { val.debug_info.name_hint, end + 1 }; // We add +1 here because we can't really test end[24]
178 }
179 }
180 }
181
182 template<bool HasDebugInfo>
184 {
185 return val.value;
186 }
187
188 constexpr auto hash(ice::StringID_Hash sid_hash) noexcept -> ice::u64
189 {
190 return sid_hash.value;
191 }
192
193 constexpr auto hash(ice::StringID_Arg value) noexcept -> ice::u64
194 {
195 return hash(stringid_hash(value));
196 }
197
198
199 constexpr auto operator""_sid(char const* str, size_t len) noexcept
200 {
201 return ice::stringid(str, len);
202 }
203
204 constexpr auto operator""_sid_hash(char const* str, size_t len) noexcept
205 {
206 return ice::stringid_hash(ice::stringid(str, len));
207 }
208
210 {
211 return value;
212 }
213
215 {
216 return { .value = value };
217 }
218
219 constexpr bool BaseStringID<false>::operator==(ice::StringID_Hash strid_hash) const noexcept
220 {
221 return value == strid_hash;
222 }
223
225 {
226 return value;
227 }
228
230 {
231 return { .value = value };
232 }
233
234 constexpr bool BaseStringID<true>::operator==(ice::StringID_Hash strid_hash) const noexcept
235 {
236 return value == strid_hash;
237 }
238
239 constexpr auto operator<=>(ice::StringID_Arg left, ice::StringID_Arg right) noexcept
240 {
241 return left.value.value <=> right.value.value;
242 }
243
244} // namespace ice
static constexpr uint32_t Constant_StringID_DefaultSeed
The seed used to generate hash values for ice::StringID.
Definition constants.hxx:17
static constexpr bool Constant_StringID_DebugInfoEnabled
Switch controling the default implementation used for ice::StringID.
Definition constants.hxx:22
Definition murmur2.hxx:42
constexpr auto cexpr_murmur2_x64_64(std::u8string_view key, ice::u64 seed) noexcept -> mm2_x64_64
Definition murmur2.hxx:107
Definition stringid.hxx:30
char[24] StringID_DebugNameHint
Definition stringid.hxx:41
Definition editable_operations.hxx:9
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr auto operator<=>(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:135
std::conditional_t< ice::build::Constant_StringID_DebugInfoEnabled, StringID const &, StringID > StringID_Arg
Argument type used to pass ice::StringID values to functions.
Definition stringid.hxx:23
std::uint64_t u64
Definition types.hxx:27
std::int32_t i32
Definition types.hxx:21
BaseStringID< ice::build::Constant_StringID_DebugInfoEnabled > StringID
\copy ice::BaseStringID.
Definition stringid.hxx:15
constexpr auto stringid_hash(ice::BaseStringID< HasDebugInfo > val) noexcept -> ice::detail::stringid_type_v3::StringID_Hash
Definition stringid.hxx:183
constexpr auto stringid(ice::StaticString< Capacity, char > value) noexcept -> ice::StringID
Definition static_string.hxx:182
constexpr auto hash(ice::HeapString<> const &value) noexcept -> ice::u64
Definition heap_string.hxx:251
constexpr auto stringid_hint(ice::BaseStringID< false > val) noexcept -> std::string_view
Definition stringid.hxx:155
static constexpr ice::StringID StringID_Invalid
Definition stringid.hxx:90
constexpr operator ice::StringID_Hash() const noexcept
Definition stringid.hxx:209
detail::stringid_type_v3::StringID_Hash value
Definition stringid.hxx:70
ice::TaggedStrongValue< ice::detail::stringid_type_v3::StringID_Tag > TypeTag
Definition stringid.hxx:68
detail::stringid_type_v3::StringID_Hash value
Definition stringid.hxx:82
detail::stringid_type_v3::StringID_DebugInfo debug_info
Definition stringid.hxx:83
ice::TaggedStrongValue< ice::detail::stringid_type_v3::StringID_Tag > TypeTag
Definition stringid.hxx:80
constexpr operator ice::StringID_Hash() const noexcept
Definition stringid.hxx:224
Hashed representation of a ice::String value.
Definition stringid.hxx:12
Type tag to enable utility functions for strongly typed values.
Definition strong_type_value.hxx:11
Type tag to enable utility functions for strongly typed values across multiple types.
Definition strong_type_value.hxx:31
Definition murmur2.hxx:45
ice::u64 h[1]
Definition murmur2.hxx:46
char const * value
Definition stringid.hxx:45
Internal hash type representing the hashed string value.
Definition stringid.hxx:34
ice::u64 value
Definition stringid.hxx:37
ice::StrongValue TypeTag
Definition stringid.hxx:35
StringID_DebugNameValue name_value
Definition stringid.hxx:52
StringID_DebugNameHint name_hint
Definition stringid.hxx:51