ATLAS Offline Software
Loading...
Searching...
No Matches
Guid Class Reference

This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number). More...

#include <Guid.h>

Collaboration diagram for Guid:

Classes

struct  FallBack
class  string
 A class designed to facilitate exchange of Guid::string objects without additional allocations. More...

Public Types

enum  GuidGenMethod { GuidGenDefault , GuidGenRandom , GuidGenByTime }

Public Member Functions

constexpr Guid ()
 Standard constructor.
 Guid (bool assign)
 Standard constructor (With possible initialization)
 Guid (const std::string &s, FallBack)
constexpr Guid (std::string_view s)
 Constructor for Guid from string_view.
constexpr Guid (const char *s)
 Guid (const Guid &c)=default
 Copy constructor.
Guidoperator= (const Guid &c)=default
auto operator<=> (const Guid &) const =default
 Magic spaceship operator.
bool operator== (const Guid &) const =default
bool operator== (std::string_view str) const
constexpr void toString (std::span< char, StrLen > buf, bool uppercase=true) const noexcept
 Automatic conversion to string representation.
constexpr std::string toString (bool uppercase=true) const
constexpr Guid::string to_fixed_string (bool uppercase=true) const
constexpr void fromString (std::string_view s)
 Automatic conversion from string representation.
void fromStringFallBack (const std::string &)
unsigned int data1 () const
 Allow accessors to member data.
unsigned short data2 () const
unsigned short data3 () const
unsigned char data4 (unsigned int i) const
void setData1 (unsigned int data)
 Allow modifiers for member data.
void setData2 (unsigned short data)
void setData3 (unsigned short data)
void setData4 (unsigned char data, unsigned int i)

Static Public Member Functions

static constexpr int stringSize ()
static bool isGuid (std::string_view) noexcept
static const Guidnull () noexcept
 NULL-Guid: static class method.
static GuidGenMethod initGuidGenMethod ()
 Checks for POOL_GUID_TIME or POOL_GUID_RANDOM env variables.
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.

Static Public Attributes

static constexpr size_t StrLen = 36
static const GuidGenMethod m_guidGenMethod = Guid::initGuidGenMethod()

Private Member Functions

constexpr void setToNull () noexcept

Private Attributes

unsigned int m_data1 {}
unsigned short m_data2 {}
unsigned short m_data3 {}
std::array< unsigned char, 8 > m_data4 {}

Friends

bool operator== (std::string_view str, const Guid &rhs)
 Equality operator.
bool operator!= (std::string_view str, const Guid &rhs)
 Non-equality operator.
std::ostream & operator<< (std::ostream &os, const Guid &rhs)
 Extraction operators.

Detailed Description

This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).

Definition at line 25 of file Guid.h.

Member Enumeration Documentation

◆ GuidGenMethod

Enumerator
GuidGenDefault 
GuidGenRandom 
GuidGenByTime 

Definition at line 84 of file Guid.h.

@ GuidGenDefault
Definition Guid.h:84
@ GuidGenByTime
Definition Guid.h:84
@ GuidGenRandom
Definition Guid.h:84

Constructor & Destructor Documentation

◆ Guid() [1/6]

Guid::Guid ( )
inlineconstexpr

Standard constructor.

Definition at line 43 of file Guid.h.

43: m_data1(0U), m_data2(0U), m_data3(0U), m_data4() {}
unsigned short m_data2
Definition Guid.h:115
std::array< unsigned char, 8 > m_data4
Definition Guid.h:117
unsigned int m_data1
Definition Guid.h:114
unsigned short m_data3
Definition Guid.h:116

◆ Guid() [2/6]

Guid::Guid ( bool assign)
inlineexplicit

Standard constructor (With possible initialization)

Definition at line 45 of file Guid.h.

45: Guid() { if (assign) create(*this); }
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.
Definition Guid.cxx:29
constexpr Guid()
Standard constructor.
Definition Guid.h:43

◆ Guid() [3/6]

Guid::Guid ( const std::string & s,
FallBack  )
inline

Definition at line 48 of file Guid.h.

void fromStringFallBack(const std::string &)
Definition Guid.cxx:86

◆ Guid() [4/6]

Guid::Guid ( std::string_view s)
inlineconstexpr

Constructor for Guid from string_view.

Definition at line 50 of file Guid.h.

50{ fromString(s); }
constexpr void fromString(std::string_view s)
Automatic conversion from string representation.
Definition Guid.h:143

◆ Guid() [5/6]

Guid::Guid ( const char * s)
inlineconstexpr

Definition at line 52 of file Guid.h.

52{ fromString(s); }

◆ Guid() [6/6]

Guid::Guid ( const Guid & c)
default

Copy constructor.

Member Function Documentation

◆ create()

void Guid::create ( Guid & guid,
GuidGenMethod method = GuidGenDefault )
static

Create a new Guid default method is currently Random, can be changed by param, API or environment.

Create a new Guid.

Definition at line 29 of file Guid.cxx.

29 {
30 uuid_t me_;
31 if (method == GuidGenDefault) method = m_guidGenMethod;
32 switch(method) {
33 case GuidGenRandom:
34 ::uuid_generate(me_);
35 break;
36 case GuidGenByTime:
37 ::uuid_generate_time(me_);
38 break;
39 default:
40 ::uuid_generate(me_);
41 break;
42 }
43 unsigned int *d1 = (unsigned int*)me_;
44 unsigned short *d2 = (unsigned short*)(me_ + 4);
45 unsigned short *d3 = (unsigned short*)(me_ + 6);
46 guid.m_data1 = *d1;
47 guid.m_data2 = *d2;
48 guid.m_data3 = *d3;
49 for (unsigned int i = 0; i < 8; i++) {
50 guid.m_data4[i] = me_[i + 8];
51 }
52}
static const GuidGenMethod m_guidGenMethod
Definition Guid.h:85

◆ data1()

unsigned int Guid::data1 ( ) const
inline

Allow accessors to member data.

Definition at line 94 of file Guid.h.

94{ return m_data1; }

◆ data2()

unsigned short Guid::data2 ( ) const
inline

Definition at line 95 of file Guid.h.

95{ return m_data2; }

◆ data3()

unsigned short Guid::data3 ( ) const
inline

Definition at line 96 of file Guid.h.

96{ return m_data3; }

◆ data4()

unsigned char Guid::data4 ( unsigned int i) const
inline

Definition at line 97 of file Guid.h.

97{ if (i < 8) return m_data4[i]; return 0; }

◆ fromString()

void Guid::fromString ( std::string_view s)
constexpr

Automatic conversion from string representation.

Definition at line 143 of file Guid.h.

143 {
144 // Trim any whitespace
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");
147 }
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) {
151 sv = {}; // String is empty or all spaces
152 } else {
153 if(std::is_constant_evaluated() && (sv.size() - last_non_space - 1)!=0){
154 throw std::runtime_error("Remove spaces from GUID");
155 }
156 sv.remove_suffix(sv.size() - last_non_space - 1);
157 }
158
159 // Validate format
160 if (sv.size() != 36 ||
161 sv[8] != '-' || sv[13] != '-' || sv[18] != '-' || sv[23] != '-') {
162 setToNull();
163 if(std::is_constant_evaluated()){
164 throw std::runtime_error("failed to compile time parse GUID");
165 }
166 return;
167 }
168 bool success = true;
169 // Custom constexpr hex parser
170 auto parse_hex = [&success](std::string_view part) -> unsigned long long {
171 unsigned long long val = 0;
172 for (char c : part) {
173 val <<= 4; // Multiply by 16
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');
180 } else {
181 success = false;
182 return 0;
183 }
184 }
185 return val;
186 };
187
188 // Parse m_data1 (positions 0-7)
189 m_data1 = static_cast<unsigned int>(parse_hex(sv.substr(0, 8)));
190
191 // Parse m_data2 (positions 9-12)
192 m_data2 = static_cast<unsigned short>(parse_hex(sv.substr(9, 4)));
193
194 // Parse m_data3 (positions 14-17)
195 m_data3 = static_cast<unsigned short>(parse_hex(sv.substr(14, 4)));
196
197
198 // Parse m_data4 bytes
199 int pos = 19;
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);
203 pos+=2;
204 }
205 //Skip the dash at pos 23
206 pos = 24;
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);
210 pos+=2;
211 }
212
213 if (!success) {
214 setToNull();
215 if(std::is_constant_evaluated()){
216 throw std::runtime_error("failed to compile time parse GUID");
217 }
218 }
219 return;
220}
constexpr void setToNull() noexcept
Definition Guid.h:135

◆ fromStringFallBack()

void Guid::fromStringFallBack ( const std::string & s)

Definition at line 86 of file Guid.cxx.

86 {
87 //If it conforms to correct Guid use fast method
88 if(isGuid(s)){
89 fromString(s);
90 return;
91 }
92 //If not try "old" more error tolerant method
93 //sscanf will correct subtle corner cases:
94 //when the input string was missing a single hexadecimal digit,
95 // e.g., 83B9F174-5E27-11E4-98C2-02163E00A82,
96 // sscanf still reported 11 successful conversions.
97 //So, the outcome is an "auto-corrected" CLID as 83B9F174-5E27-11E4-98C2-02163E00A802
98 static const char* const fmt_Guid = "%08X-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
99 if (::sscanf(s.c_str(), fmt_Guid, &m_data1, &m_data2, &m_data3,
100 &m_data4[0], &m_data4[1], &m_data4[2], &m_data4[3], &m_data4[4], &m_data4[5], &m_data4[6], &m_data4[7]) != 11) {
101 setToNull();
102 }
103 return;
104}
static bool isGuid(std::string_view) noexcept
Definition Guid.cxx:54
static const char *const fmt_Guid
Definition v5_Guid.cxx:24

◆ initGuidGenMethod()

Guid::GuidGenMethod Guid::initGuidGenMethod ( )
static

Checks for POOL_GUID_TIME or POOL_GUID_RANDOM env variables.

Definition at line 20 of file Guid.cxx.

20 {
21 char* envv = getenv("POOL_GUID_TIME");
22 if (envv != 0 && *envv) return GuidGenByTime;
23 envv = getenv("POOL_GUID_RANDOM");
24 if (envv != 0 && *envv) return GuidGenRandom;
25 return GuidGenDefault;
26}
std::string getenv(const std::string &variableName)
get an environment variable

◆ isGuid()

bool Guid::isGuid ( std::string_view sv)
staticnoexcept

Definition at line 54 of file Guid.cxx.

54 {
55 // The GUID must be exactly 36 characters long
56 if (sv.size() != 36) {
57 return false;
58 }
59
60 // Check for hyphens at specific positions (0-based indices: 8, 13, 18, 23)
61 if (sv[8] != '-' || sv[13] != '-' || sv[18] != '-' || sv[23] != '-') {
62 return false;
63 }
64
65 // Validate that all other characters are hexadecimal digits (0-9, a-f, A-F)
66 for (size_t i = 0; i < 36; ++i) {
67 // Skip hyphen positions
68 if (i == 8 || i == 13 || i == 18 || i == 23) {
69 continue;
70 }
71
72 char c = sv[i];
73 // Check if it's a valid hex digit
74 if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) {
75 return false;
76 }
77 }
78 return true;
79}

◆ null()

const Guid & Guid::null ( )
staticnoexcept

NULL-Guid: static class method.

Definition at line 14 of file Guid.cxx.

14 {
15 return clid_null;
16}
static const offline_poolCopy_v5::Guid clid_null(std::string("00000000-0000-0000-0000-000000000000"))

◆ operator<=>()

auto Guid::operator<=> ( const Guid & ) const
default

Magic spaceship operator.

◆ operator=()

Guid & Guid::operator= ( const Guid & c)
default

◆ operator==() [1/2]

bool Guid::operator== ( const Guid & ) const
default

◆ operator==() [2/2]

bool Guid::operator== ( std::string_view str) const

Definition at line 81 of file Guid.cxx.

81 {
82 return str.size() == Guid::string::stringSize() && *this == Guid(str);
83}
static constexpr int stringSize()
Definition Guid.h:37

◆ setData1()

void Guid::setData1 ( unsigned int data)
inline

Allow modifiers for member data.

Definition at line 100 of file Guid.h.

100{ m_data1 = data; }
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11

◆ setData2()

void Guid::setData2 ( unsigned short data)
inline

Definition at line 101 of file Guid.h.

101{ m_data2 = data; }

◆ setData3()

void Guid::setData3 ( unsigned short data)
inline

Definition at line 102 of file Guid.h.

102{ m_data3 = data; }

◆ setData4()

void Guid::setData4 ( unsigned char data,
unsigned int i )
inline

Definition at line 103 of file Guid.h.

103{ if (i < 8) m_data4[i] = data; }

◆ setToNull()

void Guid::setToNull ( )
constexprprivatenoexcept

Definition at line 135 of file Guid.h.

135 {
136 m_data1 = 0U;
137 m_data2 = 0U;
138 m_data3 = 0U;
139 m_data4.fill('\0');
140}

◆ stringSize()

constexpr int Guid::stringSize ( )
inlinestaticconstexpr

Definition at line 61 of file Guid.h.

61{ return StrLen; }
static constexpr size_t StrLen
Definition Guid.h:27

◆ to_fixed_string()

Guid::string Guid::to_fixed_string ( bool uppercase = true) const
inlineconstexpr

Definition at line 70 of file Guid.h.

70 {
71 Guid::string buffer;
72 toString(buffer, uppercase);
73 return buffer;
74 }
constexpr void toString(std::span< char, StrLen > buf, bool uppercase=true) const noexcept
Automatic conversion to string representation.

◆ toString() [1/2]

std::string Guid::toString ( bool uppercase = true) const
inlineconstexpr

Definition at line 65 of file Guid.h.

65 {
66 std::string buf(Guid::string::stringSize(), ' ');
67 toString(std::span<char, stringSize()>(buf.data(), stringSize()), uppercase);
68 return buf;
69 }
static constexpr int stringSize()
Definition Guid.h:61

◆ toString() [2/2]

void Guid::toString ( std::span< char, StrLen > buf,
bool uppercase = true ) const
constexprnoexcept

Automatic conversion to string representation.

◆ operator!=

bool operator!= ( std::string_view str,
const Guid & rhs )
friend

Non-equality operator.

Definition at line 108 of file Guid.h.

108{ return !(rhs.operator==(str)); }

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Guid & rhs )
friend

Extraction operators.

Definition at line 106 of file Guid.cxx.

106 {
107 auto buff = rhs.to_fixed_string();
108 os.write(buff.data(), buff.size());
109 return os;
110}
constexpr Guid::string to_fixed_string(bool uppercase=true) const
Definition Guid.h:70

◆ operator==

bool operator== ( std::string_view str,
const Guid & rhs )
friend

Equality operator.

Definition at line 106 of file Guid.h.

106{ return (rhs.operator==(str)); }

Member Data Documentation

◆ m_data1

unsigned int Guid::m_data1 {}
private

Definition at line 114 of file Guid.h.

114{};

◆ m_data2

unsigned short Guid::m_data2 {}
private

Definition at line 115 of file Guid.h.

115{};

◆ m_data3

unsigned short Guid::m_data3 {}
private

Definition at line 116 of file Guid.h.

116{};

◆ m_data4

std::array<unsigned char,8> Guid::m_data4 {}
private

Definition at line 117 of file Guid.h.

117{};

◆ m_guidGenMethod

const Guid::GuidGenMethod Guid::m_guidGenMethod = Guid::initGuidGenMethod()
static

Definition at line 85 of file Guid.h.

◆ StrLen

size_t Guid::StrLen = 36
staticconstexpr

Definition at line 27 of file Guid.h.


The documentation for this class was generated from the following files: