Fray Engine
Loading...
Searching...
No Matches
uid.hpp
Go to the documentation of this file.
1/* uid.hpp */
2
3#ifndef _FRAY_UID_HPP_
4#define _FRAY_UID_HPP_
5
6#include <algorithm>
7#include <array>
8#include <cstdint>
9#include <string>
10
11#include <boost/uuid/uuid_generators.hpp>
12
13namespace fray
14{
15 namespace identity
16 {
18 using Uid = std::array<std::uint8_t, 16>;
19
24 inline Uid generate()
25 {
26 thread_local boost::uuids::random_generator generator;
27 const auto uuid = generator();
28 Uid result;
29 std::copy(uuid.begin(), uuid.end(), result.begin());
30 return result;
31 }
32
38 inline std::string hex(
39 const Uid& uid)
40 {
41 if (std::none_of(uid.begin(), uid.end(), [](std::uint8_t byte) { return byte != 0; }))
42 return {};
43 static constexpr char digits[] = "0123456789abcdef";
44 std::string result;
45 result.reserve(32);
46 for (const auto byte : uid)
47 {
48 result.push_back(digits[byte >> 4]);
49 result.push_back(digits[byte & 15]);
50 }
51 return result;
52 }
53
59 inline bool valid(
60 const std::string& uid)
61 {
62 return uid.size() == 32 &&
63 std::all_of(uid.begin(), uid.end(), [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); }) &&
64 std::any_of(uid.begin(), uid.end(), [](char c) { return c != '0'; });
65 }
66 }
67}
68
69#endif
std::array< std::uint8_t, 16 > Uid
A 128-bit identifier shared by routing and stored entries.
Definition uid.hpp:18
std::string hex(const Uid &uid)
Formats a routing UID as lowercase hexadecimal.
Definition uid.hpp:38
Uid generate()
Generates a random, globally unique identifier.
Definition uid.hpp:24
bool valid(const std::string &uid)
Validates the canonical text form of an assigned UID.
Definition uid.hpp:59