VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
guid.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3#pragma once
4
5#include <array>
6#include <cstdint>
7#include <random>
8#include <sstream>
9#include <string>
10
11namespace visutwin::canvas
12{
13 class GUID
14 {
15 public:
16 static std::string create()
17 {
18 static thread_local std::mt19937 generator(std::random_device{}());
19 std::uniform_int_distribution<int> dist(0, 255);
20
21 std::array<uint8_t, 16> bytes{};
22 for (auto& b : bytes) {
23 b = static_cast<uint8_t>(dist(generator));
24 }
25
26 // RFC4122 version 4
27 bytes[6] = static_cast<uint8_t>((bytes[6] & 0x0F) | 0x40);
28 bytes[8] = static_cast<uint8_t>((bytes[8] & 0x3F) | 0x80);
29
30 std::ostringstream stream;
31 stream << std::hex;
32 for (size_t i = 0; i < bytes.size(); ++i) {
33 stream.width(2);
34 stream.fill('0');
35 stream << static_cast<int>(bytes[i]);
36 if (i == 3 || i == 5 || i == 7 || i == 9) {
37 stream << '-';
38 }
39 }
40
41 return stream.str();
42 }
43 };
44}
static std::string create()
Definition guid.h:16