include/UUID.h
include/UUID.h File Reference
Namespaces
| Name |
|---|
| Acl |
Classes
| Name | |
|---|---|
| class | Acl::UUID A class holding a UUID, stored as a sequence of bytes. This class only supports version 4 variant 1 of the UUID standard. |
| struct | fmt::formatter< Acl::UUID > |
Functions
| Name | |
|---|---|
| UUID() Default constructor, returns a nil UUID. | |
| constexpr | UUID(const std::array< uint8_t, kUUIDSize > & bytes) Construct a UUID from a sequence of bytes. |
| UUID | generateRandom() Create a new, randomly generated UUID. |
| std::optional< UUID > | fromString(const std::string & uuid) Parse a UUID from a string. |
| std::optional< UUID > | fromVector(const std::vector< uint8_t >::const_iterator & start, const std::vector< uint8_t >::const_iterator & end) Read a UUID from a vector of uint8s. |
| std::optional< UUID > | fromPointer(const uint8_t * start, const uint8_t * end) Read a UUID from a pointer. |
| std::string | toString() const |
| std::string | toBitString() const |
| bool | operator==(const UUID & other) const Compare this UUID to another UUID. |
| bool | operator!=(const UUID & other) const Compare this UUID to another UUID for inequality. |
| bool | operator<(const UUID & other) const Less than operator implementation. |
| std::array< uint8_t, kUUIDSize >::const_iterator | begin() const |
| std::array< uint8_t, kUUIDSize >::const_iterator | end() const |
| constexpr size_t | size() const |
Attributes
| Name | |
|---|---|
| constexpr size_t | kUUIDSize |
| const UUID | kNilUUID |
| const UUID | kOmniUUID |
Functions Documentation
function UUID
UUID()
Default constructor, returns a nil UUID.
function UUID
explicit constexpr UUID(
const std::array< uint8_t, kUUIDSize > & bytes
)
Construct a UUID from a sequence of bytes.
Note: This will accept UUIDs that are not valid version 4 UUIDs.
function generateRandom
static UUID generateRandom()
Create a new, randomly generated UUID.
Return: A new, randomly generated UUID
function fromString
static std::optional< UUID > fromString(
const std::string & uuid
)
Parse a UUID from a string.
Parameters:
- uuid The string representation of the UUID
Return: An optional containing the UUID on success, or nullopt in case the parsing failed
function fromVector
static std::optional< UUID > fromVector(
const std::vector< uint8_t >::const_iterator & start,
const std::vector< uint8_t >::const_iterator & end
)
Read a UUID from a vector of uint8s.
Parameters:
- start Start iterator to read the UUID from
- end End iterator to read the UUID from, must be 16 bytes after
start
Return: An optional containing the UUID on success, or nullopt in case the parsing failed
function fromPointer
static std::optional< UUID > fromPointer(
const uint8_t * start,
const uint8_t * end
)
Read a UUID from a pointer.
Parameters:
- start Start pointer to read the UUID from
- end End pointer to read the UUID from, must be 16 bytes after
start
Return: An optional containing the UUID on success, or nullopt in case the parsing failed
function toString
std::string toString() const
Return: A string representation of the UUID formatted as hex values
function toBitString
std::string toBitString() const
Return: A string representation of the UUID formatted as bits
function operator==
bool operator==(
const UUID & other
) const
Compare this UUID to another UUID.
Parameters:
- other The other UUID to compare this UUID to
Return: True if the UUIDs are identical, false otherwise
function operator!=
bool operator!=(
const UUID & other
) const
Compare this UUID to another UUID for inequality.
Parameters:
- other The other UUID to compare this UUID to
Return: True if the UUIDs are not equal, false in case they are identical
function operator<
bool operator<(
const UUID & other
) const
Less than operator implementation.
Parameters:
- other The other UUID to compare this UUID to
Return: True if this UUID should be sorted before the other UUID
function begin
std::array< uint8_t, kUUIDSize >::const_iterator begin() const
Return: Begin iterator for the UUID byte sequence
function end
std::array< uint8_t, kUUIDSize >::const_iterator end() const
Return: End iterator for the UUID byte sequence
function size
constexpr size_t size() const
Return: The size in bytes of the UUID. Will always return 16
Attributes Documentation
variable kUUIDSize
static constexpr size_t kUUIDSize = 16;
variable kNilUUID
static const UUID kNilUUID;
variable kOmniUUID
static const UUID kOmniUUID;
Source code
// Copyright (c) 2024, Ateliere. All rights reserved.
#pragma once
#include <array>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include <fmt/format.h>
namespace Acl {
class UUID {
public:
static constexpr size_t kUUIDSize = 16;
// Predefined UUID values
static const UUID kNilUUID;
static const UUID kOmniUUID;
UUID();
constexpr explicit UUID(const std::array<uint8_t, kUUIDSize>& bytes)
: mUUID{bytes} {
}
static UUID generateRandom();
static std::optional<UUID> fromString(const std::string& uuid);
static std::optional<UUID> fromVector(const std::vector<uint8_t>::const_iterator& start,
const std::vector<uint8_t>::const_iterator& end);
static std::optional<UUID> fromPointer(const uint8_t* start, const uint8_t* end);
[[nodiscard]] std::string toString() const;
[[nodiscard]] std::string toBitString() const;
bool operator==(const UUID& other) const;
bool operator!=(const UUID& other) const;
bool operator<(const UUID& other) const;
[[nodiscard]] std::array<uint8_t, kUUIDSize>::const_iterator begin() const;
[[nodiscard]] std::array<uint8_t, kUUIDSize>::const_iterator end() const;
[[nodiscard]] constexpr size_t size() const {
return mUUID.size();
}
private:
std::array<uint8_t, kUUIDSize> mUUID{};
} __attribute__((packed));
static_assert(sizeof(UUID) == 16, "UUID has unexpected size");
std::ostream& operator<<(std::ostream& stream, const UUID& uuid);
} // namespace Acl
template <> struct fmt::formatter<Acl::UUID> : formatter<std::string> {
template <typename FormatContext> auto format(const Acl::UUID& uuid, FormatContext& ctx) {
return formatter<std::string>::format(uuid.toString(), ctx);
}
};