MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
generate_uuid.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_HELPER_GENERATE_UUID_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_HELPER_GENERATE_UUID_H_
28
29#include <array>
30#include <cinttypes>
31#include <random>
32
33namespace helper {
34
35using UUID = std::array<uint8_t, 16>;
36
39 std::random_device rd;
40 std::mt19937 gen(rd());
41 std::uniform_int_distribution<uint32_t> dis(0, 0xFFFFFFFF);
42
43 uint32_t *parts32 = reinterpret_cast<uint32_t *>(result.data());
44 uint16_t *parts16 = reinterpret_cast<uint16_t *>(result.data());
45
46 parts32[0] = dis(gen);
47
48 parts16[0 + 4] = dis(gen);
49 parts16[1 + 4] = (dis(gen) & 0x0FFF) | 0x4000;
50
51 parts16[2 + 4] = (dis(gen) & 0x3FFF) | 0x8000;
52 parts16[3 + 4] = dis(gen);
53
54 parts32[3] = dis(gen);
55
56 return result;
57}
58
59inline std::string to_uuid_string(const UUID &uuid) {
60 const uint32_t *parts32 = reinterpret_cast<const uint32_t *>(uuid.data());
61 const uint16_t *parts16 = reinterpret_cast<const uint16_t *>(uuid.data());
63 oss << std::hex << std::setfill('0') << std::setw(8) << parts32[0];
64 oss << "-";
65 oss << std::setw(4) << parts16[4] << "-" << std::setw(4) << parts16[5] << "-"
66 << std::setw(4) << parts16[6] << "-";
67 oss << std::setw(4) << parts16[7] << std::setw(8) << parts32[3];
68
69 return oss.str();
70}
71
72} // namespace helper
73
74#endif /* ROUTER_SRC_MYSQL_REST_SERVICE_INCLUDE_HELPER_GENERATE_UUID_H_ */
std::string hex(const Container &c)
Definition: hex.h:61
Definition: cache.h:33
std::array< uint8_t, 16 > UUID
Definition: generate_uuid.h:35
std::string to_uuid_string(const UUID &uuid)
Definition: generate_uuid.h:59
UUID generate_uuid_v4()
Definition: generate_uuid.h:37
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872
struct result result
Definition: result.h:34
Definition: result.h:30