Audacity 3.2.0
Public Types | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
audacity::Uuid Class Referencefinal

Utility class that generates and parses UUIDs. More...

#include <Uuid.h>

Collaboration diagram for audacity::Uuid:
[legend]

Public Types

using Bytes = std::array< uint8_t, 16 >
 

Public Member Functions

 Uuid ()
 Creates a nil UUID. More...
 
 Uuid (const Bytes &data) noexcept
 Creates UUID from the 16 bytes long array. More...
 
bool IsNil () const noexcept
 Checks, if the UUID is nil. More...
 
 operator bool () const noexcept
 Synonymous to !isNil() More...
 
bool operator== (const Uuid &other) const noexcept
 
bool operator!= (const Uuid &other) const noexcept
 
bool operator> (const Uuid &other) const noexcept
 
bool operator>= (const Uuid &other) const noexcept
 
bool operator< (const Uuid &other) const noexcept
 
bool operator<= (const Uuid &other) const noexcept
 
const BytesToBytes () const noexcept
 Get the 16 bytes long array with the raw UUID data. More...
 
std::string ToString () const
 Get a string in the xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format. More...
 
std::string ToHexString () const
 Get a string in the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx format. More...
 
std::size_t GetHash () const noexcept
 

Static Public Member Functions

static Uuid Generate ()
 Generate a new UUID. More...
 
static Uuid FromString (const std::string &str)
 

Private Attributes

Bytes mData
 

Detailed Description

Utility class that generates and parses UUIDs.

UUIDs are generated using: UuidCreate on Windows, CFUUIDCreate on macOS, libuuid on other systems.

Definition at line 27 of file Uuid.h.

Member Typedef Documentation

◆ Bytes

using audacity::Uuid::Bytes = std::array<uint8_t, 16>

Definition at line 30 of file Uuid.h.

Constructor & Destructor Documentation

◆ Uuid() [1/2]

Uuid::Uuid ( )

Creates a nil UUID.

Definition at line 72 of file Uuid.cpp.

73 : Uuid(Bytes {})
74{
75}
Uuid()
Creates a nil UUID.
Definition: Uuid.cpp:72
std::array< uint8_t, 16 > Bytes
Definition: Uuid.h:30

◆ Uuid() [2/2]

Uuid::Uuid ( const Bytes data)
explicitnoexcept

Creates UUID from the 16 bytes long array.

Definition at line 77 of file Uuid.cpp.

78 : mData(data)
79{
80}
Bytes mData
Definition: Uuid.h:78

Member Function Documentation

◆ FromString()

Uuid Uuid::FromString ( const std::string &  str)
static

Parses an UUID from the string. This method expects the string to be in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx or {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} formats. This will return nil UUID if parsing fails

Definition at line 119 of file Uuid.cpp.

120{
121 const size_t length = str.length();
122
123 if (length == 0)
124 return {};
125
126 const bool hasBraces = str[0] == '{';
127
128 if (hasBraces && (length != BRACED_UUID_LENGTH || str.back() != '}'))
129 return {};
130 else if (!hasBraces && length != UUID_LENGTH)
131 return {};
132
133 const unsigned int iteratorOffset = hasBraces ? 1 : 0;
134
135 std::string::const_iterator currentSymbol = str.begin() + iteratorOffset;
136 std::string::const_iterator inputEnd = str.end() - iteratorOffset;
137
138 Uuid uuid;
139 Bytes::iterator currentByte = uuid.mData.begin();
140
141 for (int i = 0; i < 16; ++i)
142 {
143 if (!readByte(currentByte, currentSymbol, inputEnd))
144 return {};
145
146 if (currentSymbol != inputEnd && *currentSymbol == '-')
147 ++currentSymbol;
148 }
149
150 return uuid;
151}
#define str(a)
Platform independent class for generating and parsing UUIDs.
bool readByte(Uuid::Bytes::iterator &outputIt, std::string::const_iterator &inputIt, const std::string::const_iterator &inputEnd)
Definition: Uuid.cpp:44
constexpr int BRACED_UUID_LENGTH
Definition: Uuid.cpp:38
constexpr int UUID_LENGTH
Definition: Uuid.cpp:39

References audacity::BRACED_UUID_LENGTH, mData, audacity::anonymous_namespace{Uuid.cpp}::readByte(), str, and audacity::UUID_LENGTH.

Here is the call graph for this function:

◆ Generate()

Uuid Uuid::Generate ( )
static

Generate a new UUID.

Definition at line 82 of file Uuid.cpp.

83{
84#if defined(USE_UUID_CREATE)
85 UUID winUid;
86
87 if (RPC_S_OK != ::UuidCreate(&winUid))
88 return {};
89
90 Uuid uuid;
91
92 std::memcpy(uuid.mData.data(), &winUid, sizeof(winUid));
93
94 return uuid;
95#elif defined(USE_CFUUID)
96 CFUUIDBytes bytes = CFUUIDGetUUIDBytes(
97 CF_ptr<CFUUIDRef>{ CFUUIDCreate(NULL) }.get());
98
99 Uuid uuid;
100
101 std::memcpy(uuid.mData.data(), &bytes, sizeof(bytes));
102
103 return uuid;
104#elif defined(USE_LIBUUID)
105 uuid_t newId;
106
107 uuid_generate(newId);
108
109 Uuid uuid;
110
111 std::memcpy(uuid.mData.data(), newId, sizeof(newId));
112
113 return uuid;
114#else
115# error "UUID generator is not defined"
116#endif
117}

References mData.

Referenced by audacity::sentry::anonymous_namespace{SentryReport.cpp}::CreateSentryDocument().

Here is the caller graph for this function:

◆ GetHash()

std::size_t Uuid::GetHash ( ) const
noexcept

Definition at line 252 of file Uuid.cpp.

253{
254 const std::hash<uint8_t> hasher;
255
256 constexpr std::size_t goldenRatio = GoldenRatio<sizeof(std::size_t)>::Value;
257
258 std::size_t seed = ~0;
259
260 for (uint8_t byte : mData)
261 {
262 seed ^= hasher(seed) + goldenRatio + (seed << 6) + (seed >> 2);
263 }
264
265 return seed;
266}

References mData.

◆ IsNil()

bool Uuid::IsNil ( ) const
noexcept

Checks, if the UUID is nil.

Definition at line 153 of file Uuid.cpp.

154{
155 return std::all_of(mData.begin(), mData.end(),
156 [](uint8_t c) { return c == 0; });
157}

References mData.

◆ operator bool()

Uuid::operator bool ( ) const
explicitnoexcept

Synonymous to !isNil()

Definition at line 159 of file Uuid.cpp.

160{
161 return !IsNil();
162}
bool IsNil() const noexcept
Checks, if the UUID is nil.
Definition: Uuid.cpp:153

◆ operator!=()

bool Uuid::operator!= ( const Uuid other) const
noexcept

Definition at line 169 of file Uuid.cpp.

170{
171 return mData != rhs.mData;
172}

◆ operator<()

bool Uuid::operator< ( const Uuid other) const
noexcept

Definition at line 184 of file Uuid.cpp.

185{
186 return mData < rhs.mData;
187}

◆ operator<=()

bool Uuid::operator<= ( const Uuid other) const
noexcept

Definition at line 189 of file Uuid.cpp.

190{
191 return mData <= rhs.mData;
192}

◆ operator==()

bool Uuid::operator== ( const Uuid other) const
noexcept

Definition at line 164 of file Uuid.cpp.

165{
166 return mData == rhs.mData;
167}

◆ operator>()

bool Uuid::operator> ( const Uuid other) const
noexcept

Definition at line 174 of file Uuid.cpp.

175{
176 return mData > rhs.mData;
177}

◆ operator>=()

bool Uuid::operator>= ( const Uuid other) const
noexcept

Definition at line 179 of file Uuid.cpp.

180{
181 return mData >= rhs.mData;
182}

◆ ToBytes()

const Uuid::Bytes & Uuid::ToBytes ( ) const
noexcept

Get the 16 bytes long array with the raw UUID data.

Definition at line 194 of file Uuid.cpp.

195{
196 return mData;
197}

References mData.

◆ ToHexString()

std::string Uuid::ToHexString ( ) const

Get a string in the xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx format.

Definition at line 215 of file Uuid.cpp.

216{
217 char buffer[HEX_UUID_LENGTH + 1] = {};
218
219 const int bytesWritten = snprintf(
220 buffer, sizeof(buffer),
221 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
222 mData[0], mData[1], mData[2], mData[3], mData[4], mData[5], mData[6],
223 mData[7], mData[8], mData[9], mData[10], mData[11], mData[12], mData[13],
224 mData[14], mData[15]);
225
226 assert(bytesWritten == HEX_UUID_LENGTH);
227
228 return buffer;
229}
constexpr int HEX_UUID_LENGTH
Definition: Uuid.cpp:40

References audacity::HEX_UUID_LENGTH, and mData.

Referenced by audacity::sentry::anonymous_namespace{SentryReport.cpp}::CreateSentryDocument().

Here is the caller graph for this function:

◆ ToString()

std::string Uuid::ToString ( ) const

Get a string in the xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.

Definition at line 199 of file Uuid.cpp.

200{
201 char buffer[UUID_LENGTH + 1] = {};
202
203 const int bytesWritten = snprintf(
204 buffer, sizeof(buffer),
205 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
206 mData[0], mData[1], mData[2], mData[3], mData[4], mData[5], mData[6],
207 mData[7], mData[8], mData[9], mData[10], mData[11], mData[12], mData[13],
208 mData[14], mData[15]);
209
210 assert(bytesWritten == UUID_LENGTH);
211
212 return buffer;
213}

References mData, and audacity::UUID_LENGTH.

Member Data Documentation

◆ mData

Bytes audacity::Uuid::mData
private

Definition at line 78 of file Uuid.h.

Referenced by FromString(), Generate(), GetHash(), IsNil(), ToBytes(), ToHexString(), and ToString().


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