AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::UUID Class Reference

UUID generator and parser. More...

#include <uuid.hpp>

Collaboration diagram for anch::UUID:

Public Types

enum class  Version {
  NOT_SET = 0 , MAC_ADDRESS = 1 , DCE_SECURITY , MD5_HASH ,
  RANDOM , SHA1_HASH , MAC_ADDRESS_R , RANDOM_TIME ,
  EXP
}

Public Member Functions

 UUID ()=default
 UUID (const UUID &uuid)=default
 UUID (UUID &&uuid)=default
 UUID (const std::string &uuid)
 UUID (uint32_t lowTime, uint16_t midTime, uint16_t highTime, uint16_t clockSeqLow, uint16_t clockSeqHighRes, uint64_t node, UUID::Version version)
 ~UUID ()=default
std::array< uint8_t, 16 > toBytes () const
void parse (const std::string &uuid)
std::string toString () const
char * toChars (char *first) const
 operator std::string () const
std::strong_ordering operator<=> (const UUID &uuid) const =default
bool operator== (const UUID &uuid) const =default
UUID & operator= (const UUID &uuid)=default
UUID & operator= (UUID &&uuid)=default
uint32_t getLowTime () const
uint16_t getMidTime () const
uint16_t getHighTime () const
uint16_t getClockSeqLow () const
uint16_t getClockSeqHighRes () const
uint64_t getNode () const
anch::UUID::Version getVersion () const

Static Public Member Functions

static void registerProvider (UUID::Version version, std::function< UUID(const std::string &)> provider)
static UUID generateUUID (anch::UUID::Version version, const std::string &data="")
static UUID generateUUID (anch::UUID::Version version, const UUID &nameSpace, const std::string &name)
static UUID fromBytes (const std::array< uint8_t, 16 > &bytes)
static const UUID & namespaceDNS ()
static const UUID & namespaceURL ()
static const UUID & namespaceOID ()
static const UUID & namespaceX500 ()
static UUID random ()
static UUID randomTime ()
static uint64_t getUtcTimestamp ()
static UUID parseUUID (const std::string &uuid)

Static Public Attributes

static const uint64_t TIME_LOW_MASK = 0x00000000FFFFFFFF
static const uint64_t TIME_MID_MASK = 0x000000000000FFFF
static const uint64_t TIME_HIGH_MASK = 0x0000000000000FFF
static const uint64_t VERSION_MASK = 0x000000000000F000
static const uint32_t SEQ_LOW_MASK = 0x00FF
static const uint32_t SEQ_HIGH_MASK = 0x3F00
static constexpr std::size_t STRING_SIZE = 36

Detailed Description

UUID generator and parser.

See RFC 4122 for more details (http://www.ietf.org/rfc/rfc4122.txt)

This class needs anch-crypto and anch-device libraries.

Since
0.1
Author
Vincent Lachenal

Member Enumeration Documentation

◆ Version

enum class anch::UUID::Version
strong

The UUID versions

Since
0.1
Author
Vincent Lachenal
Enumerator
NOT_SET 

Undefined

MAC_ADDRESS 

MAC address based algorithm

DCE_SECURITY 

DCE security (Windows) base algorithm

MD5_HASH 

MD5 hash based algorithm

RANDOM 

Random based algorithm

SHA1_HASH 

SHA1 hash based algorithm

MAC_ADDRESS_R 

MAC address based algorithm ; all time bits are in reserve order compared to MAC_ADDRESS

RANDOM_TIME 

Random and time based algorithm

EXP 

Random and time based algorithm

Constructor & Destructor Documentation

◆ UUID() [1/5]

anch::UUID::UUID ( )
default

UUID default constructor: nil UUID ( 00000000-0000-0000-0000-000000000000 )

◆ UUID() [2/5]

anch::UUID::UUID ( const UUID & uuid)
default

UUID copy constructor

Parameters
uuidThe UUID to copy
Here is the call graph for this function:

◆ UUID() [3/5]

anch::UUID::UUID ( UUID && uuid)
default

UUID move constructor

Parameters
uuidThe UUID to 'move'
Here is the call graph for this function:

◆ UUID() [4/5]

anch::UUID::UUID ( const std::string & uuid)

UUID from std::string constructor

Parameters
uuidthe std::string to parse
Exceptions
std::invalid_argumentinvalid UUID: length is not 36, '-' is not at positions 8, 13, 18 and 23, or other characters are not hexadecimal digits

◆ UUID() [5/5]

anch::UUID::UUID ( uint32_t lowTime,
uint16_t midTime,
uint16_t highTime,
uint16_t clockSeqLow,
uint16_t clockSeqHighRes,
uint64_t node,
UUID::Version version )

UUID constructor.
Each part is truncated to its width (32 bits for lowTime , 16 for midTime , 12 for highTime , 8 for clockSeqLow and clockSeqHighRes , 48 for node and 4 for version ).

Parameters
lowTimethe low time part
midTimethe mid time part
highTimethe high time part
clockSeqLowthe clock sequence low part
clockSeqHighResthe clock sequence high resolution part
nodethe node part
versionthe UUID version

◆ ~UUID()

anch::UUID::~UUID ( )
default

UUID destructor (not virtual: UUID is a value type)

Member Function Documentation

◆ fromBytes()

UUID anch::UUID::fromBytes ( const std::array< uint8_t, 16 > & bytes)
static

Build UUID from its 16 bytes binary representation (RFC 9562, big-endian).
Version is read from the 4 high bits of byte 6 ; bytes are not checked (variant is not forced).

Parameters
bytesthe 16 bytes
Returns
the UUID
Here is the call graph for this function:

◆ generateUUID() [1/2]

UUID anch::UUID::generateUUID ( anch::UUID::Version version,
const std::string & data = "" )
static

Generate a new UUID

Parameters
versionThe UUID algorithm version to use (default to MAC based algorithm)
dataThe data to process (used to SHA1 and MD5 algorithms)
Returns
the new UUID
Exceptions
std::runtime_errorno provider for specified UUID version
Here is the call graph for this function:

◆ generateUUID() [2/2]

UUID anch::UUID::generateUUID ( anch::UUID::Version version,
const UUID & nameSpace,
const std::string & name )
static

Generate a new name based UUID (version 3 or 5) according to RFC 9562: the provider hashes the namespace UUID (16 bytes, see toBytes) followed by the name.
For other versions, namespace and name are given to the provider in the same way (random providers ignore them).

Parameters
versionThe UUID algorithm version to use (MD5_HASH or SHA1_HASH)
nameSpacethe namespace (for example namespaceDNS )
namethe name in the namespace
Returns
the new UUID
Exceptions
std::runtime_errorno provider for specified UUID version
Here is the call graph for this function:

◆ getClockSeqHighRes()

uint16_t anch::UUID::getClockSeqHighRes ( ) const

The clock sequence high value and reserved value getter

Returns
The clock sequence high value and reserved value

◆ getClockSeqLow()

uint16_t anch::UUID::getClockSeqLow ( ) const

The clock sequence low value getter

Returns
The clock sequence low value

◆ getHighTime()

uint16_t anch::UUID::getHighTime ( ) const

The timestamp high value getter

Returns
The timestamp high value

◆ getLowTime()

uint32_t anch::UUID::getLowTime ( ) const

The timestamp low value getter

Returns
The timestamp low value

◆ getMidTime()

uint16_t anch::UUID::getMidTime ( ) const

The timestamp mid value getter

Returns
The timestamp mid value

◆ getNode()

uint64_t anch::UUID::getNode ( ) const

The node getter

Returns
The node

◆ getUtcTimestamp()

uint64_t anch::UUID::getUtcTimestamp ( )
static

Get UUID version 1 / 6 timestamp: number of 100 ns intervals since 1582-10-15 00:00:00 UTC (Gregorian calendar reform, RFC 9562), from system clock

Returns
The timestamp

◆ getVersion()

anch::UUID::Version anch::UUID::getVersion ( ) const

Version getter

Returns
The version

◆ namespaceDNS()

const UUID & anch::UUID::namespaceDNS ( )
static

DNS namespace (6ba7b810-9dad-11d1-80b4-00c04fd430c8 ) for name based UUID (RFC 9562)

Returns
the namespace
Here is the call graph for this function:

◆ namespaceOID()

const UUID & anch::UUID::namespaceOID ( )
static

ISO OID namespace (6ba7b812-9dad-11d1-80b4-00c04fd430c8 ) for name based UUID (RFC 9562)

Returns
the namespace
Here is the call graph for this function:

◆ namespaceURL()

const UUID & anch::UUID::namespaceURL ( )
static

URL namespace (6ba7b811-9dad-11d1-80b4-00c04fd430c8 ) for name based UUID (RFC 9562)

Returns
the namespace
Here is the call graph for this function:

◆ namespaceX500()

const UUID & anch::UUID::namespaceX500 ( )
static

X.500 DN namespace (6ba7b814-9dad-11d1-80b4-00c04fd430c8 ) for name based UUID (RFC 9562)

Returns
the namespace
Here is the call graph for this function:

◆ operator std::string()

anch::UUID::operator std::string ( ) const
explicit

Cast UUID to std::string

◆ operator<=>()

std::strong_ordering anch::UUID::operator<=> ( const UUID & uuid) const
default

Three-way comparison: binary order (RFC 9562), which is also string representation order.
== , != , < , <= , > and >= are generated from it.

Parameters
uuidthe UUID to compare with
Returns
the comparison result
Here is the call graph for this function:

◆ operator=() [1/2]

UUID & anch::UUID::operator= ( const UUID & uuid)
default

= operator definition

Parameters
uuidthe UUID to copy
Returns
this
Here is the call graph for this function:

◆ operator=() [2/2]

UUID & anch::UUID::operator= ( UUID && uuid)
default

Move = operator definition

Parameters
uuidthe UUID to move
Returns
this
Here is the call graph for this function:

◆ operator==()

bool anch::UUID::operator== ( const UUID & uuid) const
default

== operator definition

Parameters
uuidthe UUID to compare with
Returns
true when the two UUID are equals, false otherwise
Here is the call graph for this function:

◆ parse()

void anch::UUID::parse ( const std::string & uuid)

Parse UUID for this value (usefull with default constructor).
Uppercase and lowercase hexadecimal digits are accepted. The value is not modified when uuid is invalid.

Parameters
uuidthe UUID to parse
Exceptions
std::invalid_argumentinvalid UUID: length is not 36, '-' is not at positions 8, 13, 18 and 23, or other characters are not hexadecimal digits

◆ parseUUID()

UUID anch::UUID::parseUUID ( const std::string & uuid)
static

Parse UUID

Parameters
uuidthe UUID to parse
Returns
the parsed UUID
Exceptions
std::invalid_argumentinvalid UUID: length is not 36, '-' is not at positions 8, 13, 18 and 23, or other characters are not hexadecimal digits
Here is the call graph for this function:

◆ random()

UUID anch::UUID::random ( )
static

Generate a new UUID with version 4 (random) algorithm

Returns
the new UUID
Here is the call graph for this function:

◆ randomTime()

UUID anch::UUID::randomTime ( )
static

Generate a new UUID with version 7 (random + time) algorithm.
A 16 bits counter (RFC 9562 section 6.2, method 1) is written in the 12 rand_a bits and in the 4 first rand_b bits: UUID generated by this process are strictly increasing (string and binary order), including between threads. It is initialized randomly on each new millisecond (at least 32768 UUID per millisecond before timestamp is incremented). The 58 last bits are random.

Returns
the new UUID
Here is the call graph for this function:

◆ registerProvider()

void anch::UUID::registerProvider ( UUID::Version version,
std::function< UUID(const std::string &)> provider )
static

Register UUID provider

Parameters
versionthe UUID version which can be handled by provider
providerthe provider
Here is the call graph for this function:

◆ toBytes()

std::array< uint8_t, 16 > anch::UUID::toBytes ( ) const

Get UUID 16 bytes binary representation (RFC 9562, big-endian)

Returns
the 16 bytes

◆ toChars()

char * anch::UUID::toChars ( char * first) const

Write UUID string representation without allocation.
Exactly STRING_SIZE lowercase hexadecimal characters and '-' are written (no '\0' ). Each part is written with its fixed width: higher bits of a part which does not fit (which can only be set with the parts constructor) are ignored.

Parameters
firstthe first character to write (at least STRING_SIZE characters available)
Returns
the pointer after the last written character

◆ toString()

std::string anch::UUID::toString ( ) const

Convert UUID to std::string

Returns
The string representation of UUID

Member Data Documentation

◆ SEQ_HIGH_MASK

const uint32_t anch::UUID::SEQ_HIGH_MASK = 0x3F00
static

Sequence high part time mask

◆ SEQ_LOW_MASK

const uint32_t anch::UUID::SEQ_LOW_MASK = 0x00FF
static

Sequence low part time mask

◆ STRING_SIZE

std::size_t anch::UUID::STRING_SIZE = 36
staticconstexpr

UUID string representation size (without '\0' )

◆ TIME_HIGH_MASK

const uint64_t anch::UUID::TIME_HIGH_MASK = 0x0000000000000FFF
static

Time high part time mask

◆ TIME_LOW_MASK

const uint64_t anch::UUID::TIME_LOW_MASK = 0x00000000FFFFFFFF
static

Time low part time mask

◆ TIME_MID_MASK

const uint64_t anch::UUID::TIME_MID_MASK = 0x000000000000FFFF
static

Time middle part time mask

◆ VERSION_MASK

const uint64_t anch::UUID::VERSION_MASK = 0x000000000000F000
static

Time high part time mask


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