AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
uuid.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-uuid-export.hpp"
23
24#include <iostream>
25#include <iomanip>
26#include <sstream>
27#include <functional>
28#include <atomic>
29#include <mutex>
30#include <shared_mutex>
31#include <map>
32#include <cinttypes>
33#include <array>
34#include <compare>
35#include <cstddef>
36
37
38namespace anch {
39
51 class ANCH_UUID_EXPORT UUID {
52 public:
60 enum class Version {
62 NOT_SET = 0,
63
65 MAC_ADDRESS = 1,
66
68 DCE_SECURITY,
69
71 MD5_HASH,
72
74 RANDOM,
75
77 SHA1_HASH,
78
80 MAC_ADDRESS_R,
81
83 RANDOM_TIME,
84
86 EXP
87 };
88
89 // Attributes +
90 public:
92 static const uint64_t TIME_LOW_MASK = 0x00000000FFFFFFFF;
93
95 static const uint64_t TIME_MID_MASK = 0x000000000000FFFF;
96
98 static const uint64_t TIME_HIGH_MASK = 0x0000000000000FFF;
99
101 static const uint64_t VERSION_MASK = 0x000000000000F000;
102
104 static const uint32_t SEQ_LOW_MASK = 0x00FF;
105
107 static const uint32_t SEQ_HIGH_MASK = 0x3F00;
108
110 static constexpr std::size_t STRING_SIZE = 36;
111
112 private:
114 static std::shared_mutex _mutex;
115
117 static std::atomic_bool _seeded;
118
120 static std::map<UUID::Version, std::function<UUID(const std::string&)>> _providers;
121
123 std::array<uint8_t,16> _bytes{};
124 // Attributes -
125
126
127 // Constructors +
128 public:
132 UUID() = default;
133
139 UUID(const UUID& uuid) = default;
140
146 UUID(UUID&& uuid) = default;
147
156 UUID(const std::string& uuid);
157
171 UUID(uint32_t lowTime,
172 uint16_t midTime,
173 uint16_t highTime,
174 uint16_t clockSeqLow,
175 uint16_t clockSeqHighRes,
176 uint64_t node,
177 UUID::Version version);
178 // Constructors -
179
180 // Destructor +
181 public:
185 ~UUID() = default;
186 // Destructor -
187
188 // Methods +
189 public:
196 static void registerProvider(UUID::Version version, std::function<UUID(const std::string&)> provider);
197
208 static UUID generateUUID(anch::UUID::Version version, const std::string& data = "");
209
223 static UUID generateUUID(anch::UUID::Version version, const UUID& nameSpace, const std::string& name);
224
233 static UUID fromBytes(const std::array<uint8_t,16>& bytes);
234
240 std::array<uint8_t,16> toBytes() const;
241
247 static const UUID& namespaceDNS();
248
254 static const UUID& namespaceURL();
255
261 static const UUID& namespaceOID();
262
268 static const UUID& namespaceX500();
269
275 static UUID random();
276
286 static UUID randomTime();
287
294 static uint64_t getUtcTimestamp();
295
306 static UUID parseUUID(const std::string& uuid);
307
317 void parse(const std::string& uuid);
318
324 std::string toString() const;
325
336 char* toChars(char* first) const;
337
338 private:
342 static void registerRandomUUID();
343 // Methods -
344
345
346 // Operators +
347 public:
351 explicit operator std::string() const;
352
361 std::strong_ordering operator <=>(const UUID& uuid) const = default;
362
370 bool operator ==(const UUID& uuid) const = default;
371
379 UUID& operator =(const UUID& uuid) = default;
380
388 UUID& operator =(UUID&& uuid) = default;
389 // Operators -
390
391
392 // Accessors +
393 public:
399 uint32_t getLowTime() const;
400
406 uint16_t getMidTime() const;
407
413 uint16_t getHighTime() const;
414
420 uint16_t getClockSeqLow() const;
421
427 uint16_t getClockSeqHighRes() const;
428
434 uint64_t getNode() const;
435
442 // Accessors -
443
444 };
445
446}
447
455template<>
456struct std::hash<anch::UUID> {
464 std::size_t operator()(const anch::UUID& uuid) const noexcept;
465};
466
476template<class CharT, class Traits>
477std::basic_ostream<CharT, Traits>&
478operator<<(std::basic_ostream<CharT, Traits>& out, const anch::UUID& uuid);
479
480#include "impl/uuid.hpp"
UUID generator and parser.
Definition uuid.hpp:51
uint32_t getLowTime() const
static UUID generateUUID(anch::UUID::Version version, const UUID &nameSpace, const std::string &name)
static const UUID & namespaceURL()
static const uint32_t SEQ_HIGH_MASK
Definition uuid.hpp:107
std::string toString() const
uint16_t getClockSeqLow() const
std::array< uint8_t, 16 > toBytes() const
UUID(UUID &&uuid)=default
UUID(const std::string &uuid)
static const uint64_t TIME_LOW_MASK
Definition uuid.hpp:92
static uint64_t getUtcTimestamp()
static constexpr std::size_t STRING_SIZE
Definition uuid.hpp:110
static const uint32_t SEQ_LOW_MASK
Definition uuid.hpp:104
static UUID fromBytes(const std::array< uint8_t, 16 > &bytes)
Version
Definition uuid.hpp:60
UUID(uint32_t lowTime, uint16_t midTime, uint16_t highTime, uint16_t clockSeqLow, uint16_t clockSeqHighRes, uint64_t node, UUID::Version version)
static const UUID & namespaceOID()
static const uint64_t TIME_HIGH_MASK
Definition uuid.hpp:98
uint16_t getHighTime() const
anch::UUID::Version getVersion() const
static const uint64_t TIME_MID_MASK
Definition uuid.hpp:95
static UUID randomTime()
static UUID random()
static UUID generateUUID(anch::UUID::Version version, const std::string &data="")
static const UUID & namespaceX500()
~UUID()=default
uint64_t getNode() const
uint16_t getMidTime() const
void parse(const std::string &uuid)
UUID(const UUID &uuid)=default
static const uint64_t VERSION_MASK
Definition uuid.hpp:101
static void registerProvider(UUID::Version version, std::function< UUID(const std::string &)> provider)
static const UUID & namespaceDNS()
static UUID parseUUID(const std::string &uuid)
char * toChars(char *first) const
uint16_t getClockSeqHighRes() const
UUID()=default
AnCH framework base namespace.
Definition app.hpp:30
std::size_t operator()(const anch::UUID &uuid) const noexcept