AnCH Framework 0.1
Another C++ Hack Framework
Loading...
Searching...
No Matches
uuid.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 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 <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <iostream>
23#include <iomanip>
24#include <sstream>
25#include <random>
26#include <functional>
27#include <atomic>
28#include <mutex>
29#include <map>
30#include <cinttypes>
31
32
33namespace anch {
34
46 class UUID {
47 public:
55 enum class Version {
58
61
64
67
70
73 };
74
75 // Attributes +
76 public:
78 static const uint64_t TIME_LOW_MASK = 0x00000000FFFFFFFF;
79
81 static const uint64_t TIME_MID_MASK = 0x000000000000FFFF;
82
84 static const uint64_t TIME_HIGH_MASK = 0x0000000000000FFF;
85
87 static const uint64_t VERSION_MASK = 0x000000000000F000;
88
90 static const uint32_t SEQ_LOW_MASK = 0x00FF;
91
93 static const uint32_t SEQ_HIGH_MASK = 0x3F00;
94
95 private:
97 static std::mutex _mutex;
98
100 static std::atomic_bool _seeded;
101
103 static std::map<UUID::Version, std::function<UUID(const std::string&)>> _providers;
104
106 uint32_t _lowTime;
107
109 uint16_t _midTime;
110
112 uint16_t _highTime;
113
115 uint16_t _clockSeqLow;
116
118 uint16_t _clockSeqHighRes;
119
121 uint64_t _node;
122
124 anch::UUID::Version _version;
125 // Attributes -
126
127
128 // Constructors +
129 public:
134
140 UUID(const UUID& uuid);
141
147 UUID(UUID&& uuid);
148
157 UUID(const std::string& uuid);
158
170 UUID(uint32_t lowTime,
171 uint16_t midTime,
172 uint16_t highTime,
173 uint16_t clockSeqLow,
174 uint16_t clockSeqHighRes,
175 uint64_t node,
176 UUID::Version version);
177 // Constructors -
178
179 // Destructor +
180 public:
184 virtual ~UUID();
185 // Destructor -
186
187 // Methods +
188 public:
195 static void registerProvider(UUID::Version version, std::function<UUID(const std::string&)> provider);
196
207 static UUID generateUUID(anch::UUID::Version version, const std::string& data = "");
208
214 static UUID random();
215
221 static uint64_t getUtcTimestamp();
222
228 static std::random_device& getRandomEngine();
229
235 static std::uniform_int_distribution<uint16_t>& getDistSeq();
236
244 static UUID parseUUID(const std::string& uuid);
245
251 void parse(const std::string& uuid);
252
258 std::string toString() const;
259
260 private:
264 static void registerRandomUUID();
265 // Methods -
266
267
268 // Operators +
269 public:
273 explicit operator std::string() const;
274
282 bool operator ==(const UUID& uuid) const;
283
291 bool operator !=(const UUID& uuid) const;
292
300 UUID& operator =(const UUID& uuid);
301 // Operators -
302
303
304 // Accessors +
305 public:
311 uint32_t getLowTime() const;
312
318 uint16_t getMidTime() const;
319
325 uint16_t getHighTime() const;
326
332 uint16_t getClockSeqLow() const;
333
339 uint16_t getClockSeqHighRes() const;
340
346 uint64_t getNode() const;
347
354 // Accessors -
355
356 };
357
358}
359
369template<class CharT, class Traits>
370std::basic_ostream<CharT, Traits>&
371operator<<(std::basic_ostream<CharT, Traits>& out, const anch::UUID& uuid);
372
373#include "impl/uuid.hpp"
UUID generator and parser.
Definition uuid.hpp:46
uint32_t getLowTime() const
static std::random_device & getRandomEngine()
bool operator==(const UUID &uuid) const
UUID(UUID &&uuid)
static std::uniform_int_distribution< uint16_t > & getDistSeq()
static const uint32_t SEQ_HIGH_MASK
Definition uuid.hpp:93
bool operator!=(const UUID &uuid) const
std::string toString() const
uint16_t getClockSeqLow() const
UUID(const std::string &uuid)
static const uint64_t TIME_LOW_MASK
Definition uuid.hpp:78
static uint64_t getUtcTimestamp()
static const uint32_t SEQ_LOW_MASK
Definition uuid.hpp:90
Version
Definition uuid.hpp:55
@ SHA1_HASH
Definition uuid.hpp:72
@ RANDOM
Definition uuid.hpp:69
@ NOT_SET
Definition uuid.hpp:57
@ MAC_ADDRESS
Definition uuid.hpp:60
@ MD5_HASH
Definition uuid.hpp:66
@ DCE_SECURITY
Definition uuid.hpp:63
UUID(uint32_t lowTime, uint16_t midTime, uint16_t highTime, uint16_t clockSeqLow, uint16_t clockSeqHighRes, uint64_t node, UUID::Version version)
static const uint64_t TIME_HIGH_MASK
Definition uuid.hpp:84
uint16_t getHighTime() const
anch::UUID::Version getVersion() const
static const uint64_t TIME_MID_MASK
Definition uuid.hpp:81
static UUID random()
static UUID generateUUID(anch::UUID::Version version, const std::string &data="")
UUID & operator=(const UUID &uuid)
uint64_t getNode() const
uint16_t getMidTime() const
virtual ~UUID()
void parse(const std::string &uuid)
UUID(const UUID &uuid)
static const uint64_t VERSION_MASK
Definition uuid.hpp:87
static void registerProvider(UUID::Version version, std::function< UUID(const std::string &)> provider)
static UUID parseUUID(const std::string &uuid)
uint16_t getClockSeqHighRes() const
AnCH framework base namespace.
Definition app.hpp:28