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);
123struct std::formatter<
Guid::string> : std::formatter<std::string_view> {
125 return std::formatter<std::string_view>::format(
static_cast<std::string_view
>(guid), ctx);
132 return os << static_cast<std::string_view>(guid);
145 if(std::is_constant_evaluated() && std::min(sv.find_first_not_of(
' '), sv.size()) > 0){
146 throw std::runtime_error(
"Remove spaces from GUID");
148 sv.remove_prefix(std::min(sv.find_first_not_of(
' '), sv.size()));
149 auto last_non_space = sv.find_last_not_of(
' ');
150 if (last_non_space == std::string_view::npos) {
153 if(std::is_constant_evaluated() && (sv.size() - last_non_space - 1)!=0){
154 throw std::runtime_error(
"Remove spaces from GUID");
156 sv.remove_suffix(sv.size() - last_non_space - 1);
160 if (sv.size() != 36 ||
161 sv[8] !=
'-' || sv[13] !=
'-' || sv[18] !=
'-' || sv[23] !=
'-') {
163 if(std::is_constant_evaluated()){
164 throw std::runtime_error(
"failed to compile time parse GUID");
170 auto parse_hex = [&success](std::string_view part) ->
unsigned long long {
171 unsigned long long val = 0;
172 for (
char c : part) {
174 if (c >=
'0' && c <=
'9') {
175 val +=
static_cast<unsigned long long>(c -
'0');
176 }
else if (c >=
'a' && c <=
'f') {
177 val +=
static_cast<unsigned long long>(10 + c -
'a');
178 }
else if (c >=
'A' && c <=
'F') {
179 val +=
static_cast<unsigned long long>(10 + c -
'A');
189 m_data1 =
static_cast<unsigned int>(parse_hex(sv.substr(0, 8)));
192 m_data2 =
static_cast<unsigned short>(parse_hex(sv.substr(9, 4)));
195 m_data3 =
static_cast<unsigned short>(parse_hex(sv.substr(14, 4)));
200 for(
int i =0; i< 2; i++){
201 auto val = parse_hex(sv.substr(pos, 2));
202 m_data4[i] =
static_cast<unsigned char>(val);
207 for(
int i =2; i< 8; i++){
208 auto val = parse_hex(sv.substr(pos, 2));
209 m_data4[i] =
static_cast<unsigned char>(val);
215 if(std::is_constant_evaluated()){
216 throw std::runtime_error(
"failed to compile time parse GUID");
223constexpr void Guid::toString(std::span<char, 36> buf,
bool uppercase)
const noexcept {
225 constexpr char lowhex[] =
"0123456789abcdef";
226 constexpr char uphex[] =
"0123456789ABCDEF";
227 const char *hex = uppercase ? uphex : lowhex;
230 unsigned int v1 = m_data1;
231 buf[0] = hex[(v1 >> 28) & 0xF];
232 buf[1] = hex[(v1 >> 24) & 0xF];
233 buf[2] = hex[(v1 >> 20) & 0xF];
234 buf[3] = hex[(v1 >> 16) & 0xF];
235 buf[4] = hex[(v1 >> 12) & 0xF];
236 buf[5] = hex[(v1 >> 8) & 0xF];
237 buf[6] = hex[(v1 >> 4) & 0xF];
238 buf[7] = hex[v1 & 0xF];
242 unsigned short v2 = m_data2;
243 buf[9] = hex[(v2 >> 12) & 0xF];
244 buf[10] = hex[(v2 >> 8) & 0xF];
245 buf[11] = hex[(v2 >> 4) & 0xF];
246 buf[12] = hex[v2 & 0xF];
250 unsigned short v3 = m_data3;
251 buf[14] = hex[(v3 >> 12) & 0xF];
252 buf[15] = hex[(v3 >> 8) & 0xF];
253 buf[16] = hex[(v3 >> 4) & 0xF];
254 buf[17] = hex[v3 & 0xF];
259 for(
int i =0; i<2;i++){
260 unsigned char c = m_data4[i];
261 buf[pos] = hex[c >> 4];
262 buf[pos+1] = hex[c & 0xF];
268 for(
int i =2;
i<8;
i++){
269 unsigned char c = m_data4[
i];
284 static_assert(
sizeof(
Guid) == (
sizeof(
unsigned int) +
sizeof(
unsigned short)
285 +
sizeof(
unsigned short) +
sizeof(std::array<unsigned char,8>)));
286 const unsigned char* bytes =
reinterpret_cast<const unsigned char*
>(&g);
287 size_t hash_value = 0;
289 constexpr size_t fnv_prime =
sizeof(size_t) == 8 ? 0x100000001b3ULL : 0x01000193U;
290 constexpr size_t fnv_offset =
sizeof(size_t) == 8 ? 0xcbf29ce484222325ULL : 0x811c9dc5U;
291 hash_value = fnv_offset;
292 for (
size_t i = 0; i <
sizeof(
Guid); ++i) {
293 hash_value ^=
static_cast<size_t>(bytes[i]);
294 hash_value *= fnv_prime;
std::ostream & operator<<(std::ostream &os, const Guid::string &guid)
char data[hepevt_bytes_allocation_ATLAS]
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