Apache Ignite C++ Client
Loading...
Searching...
No Matches
uuid.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include <cstdint>
21#include <iomanip>
22#include <istream>
23#include <ostream>
24
25namespace ignite {
26
32class uuid {
33public:
37 constexpr uuid() noexcept = default;
38
45 constexpr uuid(std::int64_t most, std::int64_t least) noexcept
46 : most(most)
47 , least(least) {}
48
54 [[nodiscard]] static uuid random();
55
61 [[nodiscard]] constexpr std::int64_t get_most_significant_bits() const noexcept { return most; }
62
68 [[nodiscard]] constexpr std::int64_t get_least_significant_bits() const noexcept { return least; }
69
82 [[nodiscard]] constexpr std::int32_t version() const noexcept {
83 return static_cast<std::int32_t>((most >> 12) & 0x0f);
84 }
85
98 [[nodiscard]] constexpr std::int32_t variant() const noexcept {
99 auto least0 = static_cast<uint64_t>(least);
100 return static_cast<std::int32_t>((least0 >> (64 - (least0 >> 62))) & (least >> 63));
101 }
102
109 [[nodiscard]] constexpr int compare(const uuid &other) const noexcept {
110 if (most != other.most) {
111 return most < other.most ? -1 : 1;
112 }
113 if (least != other.least) {
114 return least < other.least ? -1 : 1;
115 }
116 return 0;
117 }
118
119private:
121 std::int64_t most = 0;
122
124 std::int64_t least = 0;
125};
126
134constexpr bool operator==(const uuid &lhs, const uuid &rhs) noexcept {
135 return lhs.compare(rhs) == 0;
136}
137
145constexpr bool operator!=(const uuid &lhs, const uuid &rhs) noexcept {
146 return lhs.compare(rhs) != 0;
147}
148
156constexpr bool operator<(const uuid &lhs, const uuid &rhs) noexcept {
157 return lhs.compare(rhs) < 0;
158}
159
167constexpr bool operator<=(const uuid &lhs, const uuid &rhs) noexcept {
168 return lhs.compare(rhs) <= 0;
169}
170
178constexpr bool operator>(const uuid &lhs, const uuid &rhs) noexcept {
179 return lhs.compare(rhs) > 0;
180}
181
189constexpr bool operator>=(const uuid &lhs, const uuid &rhs) noexcept {
190 return lhs.compare(rhs) >= 0;
191}
192
200template<typename C, typename T>
201::std::basic_ostream<C, T> &operator<<(std::basic_ostream<C, T> &os, const uuid &uuid) {
202 auto msb = uuid.get_most_significant_bits();
203 auto lsb = uuid.get_least_significant_bits();
204
205 auto part1 = static_cast<std::uint32_t>(msb >> 32);
206 auto part2 = static_cast<std::uint16_t>(msb >> 16);
207 auto part3 = static_cast<std::uint16_t>(msb);
208 auto part4 = static_cast<std::uint16_t>(lsb >> 48);
209 uint64_t part5 = lsb & 0x0000FFFFFFFFFFFFU;
210
211 std::ios_base::fmtflags saved_flags = os.flags();
212
213 // clang-format off
214 os << std::hex
215 << std::setfill<C>('0') << std::setw(8) << part1 << '-'
216 << std::setfill<C>('0') << std::setw(4) << part2 << '-'
217 << std::setfill<C>('0') << std::setw(4) << part3 << '-'
218 << std::setfill<C>('0') << std::setw(4) << part4 << '-'
219 << std::setfill<C>('0') << std::setw(12) << part5;
220 // clang-format on
221
222 os.flags(saved_flags);
223
224 return os;
225}
226
234template<typename C, typename T>
235::std::basic_istream<C, T> &operator>>(std::basic_istream<C, T> &is, uuid &result) {
236 std::uint64_t parts[5];
237
238 std::ios_base::fmtflags saved_flags = is.flags();
239
240 is >> std::hex;
241
242 for (int i = 0; i < 4; ++i) {
243 C delim;
244
245 is >> parts[i] >> delim;
246
247 if (delim != static_cast<C>('-')) {
248 return is;
249 }
250 }
251
252 is >> parts[4];
253
254 is.flags(saved_flags);
255
256 result =
257 uuid(std::int64_t((parts[0] << 32) | (parts[1] << 16) | parts[2]), std::int64_t((parts[3] << 48) | parts[4]));
258
259 return is;
260}
261
262} // namespace ignite
Universally unique identifier (UUID).
Definition uuid.h:32
constexpr std::int64_t get_most_significant_bits() const noexcept
Definition uuid.h:61
constexpr int compare(const uuid &other) const noexcept
Definition uuid.h:109
static uuid random()
Definition uuid.cpp:25
constexpr uuid() noexcept=default
constexpr std::int64_t get_least_significant_bits() const noexcept
Definition uuid.h:68
constexpr std::int32_t variant() const noexcept
Definition uuid.h:98
constexpr std::int32_t version() const noexcept
Definition uuid.h:82