[ox/std] Add UUID implementation
This commit is contained in:
parent
3b05d4e16b
commit
4f906f6e47
2
deps/ox/src/ox/std/CMakeLists.txt
vendored
2
deps/ox/src/ox/std/CMakeLists.txt
vendored
@ -37,6 +37,7 @@ add_library(
|
||||
strops.cpp
|
||||
trace.cpp
|
||||
typetraits.cpp
|
||||
uuid.cpp
|
||||
)
|
||||
|
||||
if(NOT MSVC)
|
||||
@ -109,6 +110,7 @@ install(
|
||||
types.hpp
|
||||
typetraits.hpp
|
||||
units.hpp
|
||||
uuid.hpp
|
||||
vector.hpp
|
||||
writer.hpp
|
||||
DESTINATION
|
||||
|
2
deps/ox/src/ox/std/random.cpp
vendored
2
deps/ox/src/ox/std/random.cpp
vendored
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 - 2022 gary@drinkingtea.net
|
||||
* Copyright 2015 - 2023 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
|
9
deps/ox/src/ox/std/random.hpp
vendored
9
deps/ox/src/ox/std/random.hpp
vendored
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 - 2022 gary@drinkingtea.net
|
||||
* Copyright 2015 - 2023 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
@ -25,12 +25,19 @@ class OX_PACKED Random {
|
||||
|
||||
explicit constexpr Random(const RandomSeed &seed) noexcept;
|
||||
|
||||
constexpr void seed(const RandomSeed &seed) noexcept;
|
||||
|
||||
constexpr uint64_t gen() noexcept;
|
||||
};
|
||||
|
||||
constexpr Random::Random(const RandomSeed &seed) noexcept: m_seed{seed[0], seed[1]} {
|
||||
}
|
||||
|
||||
constexpr void Random::seed(const RandomSeed &seed) noexcept {
|
||||
m_seed[0] = seed[0];
|
||||
m_seed[1] = seed[1];
|
||||
}
|
||||
|
||||
constexpr uint64_t Random::gen() noexcept {
|
||||
auto s0 = m_seed[0];
|
||||
auto s1 = m_seed[1];
|
||||
|
1
deps/ox/src/ox/std/std.hpp
vendored
1
deps/ox/src/ox/std/std.hpp
vendored
@ -42,4 +42,5 @@
|
||||
#include "types.hpp"
|
||||
#include "typetraits.hpp"
|
||||
#include "units.hpp"
|
||||
#include "uuid.hpp"
|
||||
#include "vector.hpp"
|
||||
|
34
deps/ox/src/ox/std/uuid.cpp
vendored
Normal file
34
deps/ox/src/ox/std/uuid.cpp
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2015 - 2023 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "uuid.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
bool UUID::s_seeded = false;
|
||||
Random UUID::s_rand;
|
||||
|
||||
void UUID::seed(const RandomSeed &seed) noexcept {
|
||||
s_seeded = true;
|
||||
s_rand.seed(seed);
|
||||
}
|
||||
|
||||
// UUID v4
|
||||
Result<UUID> UUID::generate() noexcept {
|
||||
if (!s_seeded) {
|
||||
return OxError(1, "UUID generator not seeded.");
|
||||
}
|
||||
UUID out;
|
||||
for (auto &v : out.m_value) {
|
||||
v = static_cast<uint8_t>(s_rand.gen() % 255);
|
||||
}
|
||||
out.m_value[6] = 4;
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
84
deps/ox/src/ox/std/uuid.hpp
vendored
Normal file
84
deps/ox/src/ox/std/uuid.hpp
vendored
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2015 - 2023 gary@drinkingtea.net
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "array.hpp"
|
||||
#include "bstring.hpp"
|
||||
#include "random.hpp"
|
||||
#include "strops.hpp"
|
||||
#include "trace.hpp"
|
||||
|
||||
namespace ox {
|
||||
|
||||
namespace detail {
|
||||
constexpr ox::BString<2> toHex(uint8_t v) noexcept {
|
||||
constexpr ox::Array<char, 16> valMap {
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'a',
|
||||
'b',
|
||||
'c',
|
||||
'd',
|
||||
'e',
|
||||
'f',
|
||||
};
|
||||
ox::Array<char, 3> out;
|
||||
out[0] = valMap[static_cast<unsigned>((v & 0xf0) / 0xf)];
|
||||
out[1] = valMap[static_cast<unsigned>(v & 0x0f)];
|
||||
out[2] = 0;
|
||||
return out.data();
|
||||
}
|
||||
}
|
||||
|
||||
class UUID {
|
||||
private:
|
||||
static bool s_seeded;
|
||||
static Random s_rand;
|
||||
ox::Array<uint8_t, 16> m_value;
|
||||
|
||||
public:
|
||||
static void seed(const RandomSeed &seed) noexcept;
|
||||
|
||||
static ox::Result<UUID> generate() noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr ox::BString<36> toString() const noexcept {
|
||||
ox::BString<36> out;
|
||||
auto i = 0u;
|
||||
constexpr auto printChars = [](
|
||||
ox::BString<36> *out, const Array<uint8_t, 16> &value, std::size_t cnt, unsigned i) {
|
||||
for (; i < cnt; ++i) {
|
||||
const auto v = value[i];
|
||||
const auto h = detail::toHex(v);
|
||||
oxIgnoreError(out->append(h.c_str(), h.len()));
|
||||
}
|
||||
return i;
|
||||
};
|
||||
i = printChars(&out, m_value, 4, i);
|
||||
out += "-";
|
||||
i = printChars(&out, m_value, 2, i);
|
||||
out += "-";
|
||||
i = printChars(&out, m_value, 2, i);
|
||||
out += "-";
|
||||
i = printChars(&out, m_value, 2, i);
|
||||
out += "-";
|
||||
i = printChars(&out, m_value, 6, i);
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user