5#ifndef PERSISTENTDATAMODEL_GUID_H
6#define PERSISTENTDATAMODEL_GUID_H
31 class string :
public std::array<char, StrLen> {
33 constexpr operator std::string_view()
const {
34 return std::string_view(data(),
size());
36 constexpr std::string_view
sv()
const {
return std::string_view(data(),
size());}
64 constexpr void toString(std::span<char, StrLen> buf,
bool uppercase =
true)
const noexcept;
75 static bool isGuid(std::string_view)
noexcept;
97 unsigned char data4(
unsigned int i)
const {
if (i < 8)
return m_data4[i];
return 0; }
110 friend std::ostream&
operator<<(std::ostream& os,
const Guid& rhs);
122struct std::formatter<
Guid::string> : std::formatter<std::string_view> {
124 return std::formatter<std::string_view>::format(
static_cast<std::string_view
>(guid), ctx);
130 return os << static_cast<std::string_view>(guid);
143 if(std::is_constant_evaluated() && std::min(sv.find_first_not_of(
' '), sv.size()) > 0){
144 throw std::runtime_error(
"Remove spaces from GUID");
146 sv.remove_prefix(std::min(sv.find_first_not_of(
' '), sv.size()));
147 auto last_non_space = sv.find_last_not_of(
' ');
148 if (last_non_space == std::string_view::npos) {
151 if(std::is_constant_evaluated() && (sv.size() - last_non_space - 1)!=0){
152 throw std::runtime_error(
"Remove spaces from GUID");
154 sv.remove_suffix(sv.size() - last_non_space - 1);
158 if (sv.size() != 36 ||
159 sv[8] !=
'-' || sv[13] !=
'-' || sv[18] !=
'-' || sv[23] !=
'-') {
161 if(std::is_constant_evaluated()){
162 throw std::runtime_error(
"failed to compile time parse GUID");
168 auto parse_hex = [&success](std::string_view part) ->
unsigned long long {
169 unsigned long long val = 0;
170 for (
char c : part) {
172 if (c >=
'0' && c <=
'9') {
173 val +=
static_cast<unsigned long long>(c -
'0');
174 }
else if (c >=
'a' && c <=
'f') {
175 val +=
static_cast<unsigned long long>(10 + c -
'a');
176 }
else if (c >=
'A' && c <=
'F') {
177 val +=
static_cast<unsigned long long>(10 + c -
'A');
187 m_data1 =
static_cast<unsigned int>(parse_hex(sv.substr(0, 8)));
190 m_data2 =
static_cast<unsigned short>(parse_hex(sv.substr(9, 4)));
193 m_data3 =
static_cast<unsigned short>(parse_hex(sv.substr(14, 4)));
198 for(
int i =0; i< 2; i++){
199 auto val = parse_hex(sv.substr(pos, 2));
200 m_data4[i] =
static_cast<unsigned char>(val);
205 for(
int i =2; i< 8; i++){
206 auto val = parse_hex(sv.substr(pos, 2));
207 m_data4[i] =
static_cast<unsigned char>(val);
213 if(std::is_constant_evaluated()){
214 throw std::runtime_error(
"failed to compile time parse GUID");
221constexpr void Guid::toString(std::span<char, 36> buf,
bool uppercase)
const noexcept {
223 constexpr char lowhex[] =
"0123456789abcdef";
224 constexpr char uphex[] =
"0123456789ABCDEF";
225 const char *hex = uppercase ? uphex : lowhex;
228 unsigned int v1 = m_data1;
229 buf[0] = hex[(v1 >> 28) & 0xF];
230 buf[1] = hex[(v1 >> 24) & 0xF];
231 buf[2] = hex[(v1 >> 20) & 0xF];
232 buf[3] = hex[(v1 >> 16) & 0xF];
233 buf[4] = hex[(v1 >> 12) & 0xF];
234 buf[5] = hex[(v1 >> 8) & 0xF];
235 buf[6] = hex[(v1 >> 4) & 0xF];
236 buf[7] = hex[v1 & 0xF];
240 unsigned short v2 = m_data2;
241 buf[9] = hex[(v2 >> 12) & 0xF];
242 buf[10] = hex[(v2 >> 8) & 0xF];
243 buf[11] = hex[(v2 >> 4) & 0xF];
244 buf[12] = hex[v2 & 0xF];
248 unsigned short v3 = m_data3;
249 buf[14] = hex[(v3 >> 12) & 0xF];
250 buf[15] = hex[(v3 >> 8) & 0xF];
251 buf[16] = hex[(v3 >> 4) & 0xF];
252 buf[17] = hex[v3 & 0xF];
257 for(
int i =0; i<2;i++){
258 unsigned char c = m_data4[i];
259 buf[pos] = hex[c >> 4];
260 buf[pos+1] = hex[c & 0xF];
266 for(
int i =2;
i<8;
i++){
267 unsigned char c = m_data4[
i];
282 static_assert(
sizeof(
Guid) == (
sizeof(
unsigned int) +
sizeof(
unsigned short)
283 +
sizeof(
unsigned short) +
sizeof(std::array<unsigned char,8>)));
284 const unsigned char* bytes =
reinterpret_cast<const unsigned char*
>(&g);
285 size_t hash_value = 0;
287 constexpr size_t fnv_prime =
sizeof(size_t) == 8 ? 0x100000001b3ULL : 0x01000193U;
288 constexpr size_t fnv_offset =
sizeof(size_t) == 8 ? 0xcbf29ce484222325ULL : 0x811c9dc5U;
289 hash_value = fnv_offset;
290 for (
size_t i = 0; i <
sizeof(
Guid); ++i) {
291 hash_value ^=
static_cast<size_t>(bytes[i]);
292 hash_value *= fnv_prime;
std::ostream & operator<<(std::ostream &os, const Guid::string &guid)
size_t size() const
Number of registered mappings.
A class designed to facilitate exchange of Guid::string objects without additional allocations.
static constexpr int stringSize()
constexpr std::string_view sv() const
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
constexpr void fromString(std::string_view s)
Automatic conversion from string representation.
static const Guid & null() noexcept
NULL-Guid: static class method.
void setData3(unsigned short data)
bool operator==(const Guid &) const =default
static void create(Guid &guid, GuidGenMethod method=GuidGenDefault)
Create a new Guid default method is currently Random, can be changed by param, API or environment.
constexpr Guid::string to_fixed_string(bool uppercase=true) const
static constexpr size_t StrLen
friend bool operator==(std::string_view str, const Guid &rhs)
Equality operator.
std::array< unsigned char, 8 > m_data4
unsigned short data2() const
void setData4(unsigned char data, unsigned int i)
unsigned int data1() const
Allow accessors to member data.
Guid(bool assign)
Standard constructor (With possible initialization).
constexpr Guid()
Standard constructor.
void setData2(unsigned short data)
unsigned char data4(unsigned int i) const
Guid(const Guid &c)=default
Copy constructor.
constexpr void setToNull() noexcept
Guid(const std::string &s, FallBack)
friend bool operator!=(std::string_view str, const Guid &rhs)
Non-equality operator.
static GuidGenMethod initGuidGenMethod()
Checks for POOL_GUID_TIME or POOL_GUID_RANDOM env variables.
constexpr void toString(std::span< char, StrLen > buf, bool uppercase=true) const noexcept
Automatic conversion to string representation.
auto operator<=>(const Guid &) const =default
Magic spaceship operator.
static constexpr int stringSize()
Guid & operator=(const Guid &c)=default
static const GuidGenMethod m_guidGenMethod
friend std::ostream & operator<<(std::ostream &os, const Guid &rhs)
Extraction operators.
constexpr Guid(const char *s)
void setData1(unsigned int data)
Allow modifiers for member data.
constexpr Guid(std::string_view s)
Constructor for Guid from string_view.
void fromStringFallBack(const std::string &)
static bool isGuid(std::string_view) noexcept
unsigned short data3() const
size_t operator()(const Guid &g) const noexcept