azure-core
Loading...
Searching...
No Matches
uuid.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
11#include <array>
12#include <cstdint>
13#include <string>
14
15namespace Azure { namespace Core {
19 class Uuid final {
20 public:
25 using ValueArray = std::array<std::uint8_t, 16>;
26
27 private:
28 ValueArray m_uuid{};
29
30 private:
31 constexpr Uuid(ValueArray const& uuid) : m_uuid(uuid) {}
32
33 public:
38 // Nil UUID, per RFC9562, consists of all zeros:
39 // https://www.rfc-editor.org/rfc/rfc9562.html#name-nil-uuid
40 constexpr explicit Uuid() : m_uuid{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} {}
41
46 std::string ToString() const;
47
53 constexpr ValueArray const& AsArray() const { return m_uuid; }
54
59 static Uuid CreateUuid();
60
65 static constexpr Uuid CreateFromArray(ValueArray const& uuid) { return Uuid{uuid}; }
66
72 static Uuid Parse(std::string const& uuidString);
73
80 constexpr bool operator==(Uuid const& other) const
81 {
82 if (this != &other)
83 {
84 // std::array::operator==() is not a constexpr until C++20
85 for (size_t i = 0; i < m_uuid.size(); ++i)
86 {
87 if (m_uuid[i] != other.m_uuid[i])
88 {
89 return false;
90 }
91 }
92 }
93
94 return true;
95 }
96
103 constexpr bool operator!=(Uuid const& other) const { return !(*this == other); }
104
109 constexpr bool IsNil() const
110 {
111 // Nil UUID, per RFC9562, consists of all zeros:
112 // https://www.rfc-editor.org/rfc/rfc9562.html#name-nil-uuid
113 for (size_t i = 0; i < m_uuid.size(); ++i)
114 {
115 if (m_uuid[i] != 0)
116 {
117 return false;
118 }
119 }
120
121 return true;
122 }
123 };
124}} // namespace Azure::Core
Universally unique identifier.
Definition uuid.hpp:19
std::string ToString() const
Gets Uuid as a string.
Definition uuid.cpp:65
constexpr bool IsNil() const
Checks if the value represents a Nil UUID (00000000-0000-0000-0000-000000000000).
Definition uuid.hpp:109
static constexpr Uuid CreateFromArray(ValueArray const &uuid)
Construct a Uuid from an existing UUID represented as an array of bytes.
Definition uuid.hpp:65
static Uuid CreateUuid()
Creates a new random UUID.
Definition uuid.cpp:133
constexpr bool operator!=(Uuid const &other) const
Compares with another instance of Uuid for inequality.
Definition uuid.hpp:103
static Uuid Parse(std::string const &uuidString)
Construct a Uuid by parsing its representation.
Definition uuid.cpp:87
constexpr ValueArray const & AsArray() const
Returns the binary value of the Uuid for consumption by clients who need non-string representation of...
Definition uuid.hpp:53
std::array< std::uint8_t, 16 > ValueArray
Represents a byte array where the UUID value can be stored.
Definition uuid.hpp:25
constexpr Uuid()
Constructs a Nil UUID (00000000-0000-0000-0000-000000000000).
Definition uuid.hpp:40
constexpr bool operator==(Uuid const &other) const
Compares with another instance of Uuid for equality.
Definition uuid.hpp:80
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57