diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 03a89c85..6163ec2e 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -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 diff --git a/deps/ox/src/ox/std/random.cpp b/deps/ox/src/ox/std/random.cpp index f8351697..22136ab4 100644 --- a/deps/ox/src/ox/std/random.cpp +++ b/deps/ox/src/ox/std/random.cpp @@ -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 diff --git a/deps/ox/src/ox/std/random.hpp b/deps/ox/src/ox/std/random.hpp index fbc83368..f07821bd 100644 --- a/deps/ox/src/ox/std/random.hpp +++ b/deps/ox/src/ox/std/random.hpp @@ -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]; diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 5ba52f6a..96bdcb67 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -42,4 +42,5 @@ #include "types.hpp" #include "typetraits.hpp" #include "units.hpp" +#include "uuid.hpp" #include "vector.hpp" diff --git a/deps/ox/src/ox/std/uuid.cpp b/deps/ox/src/ox/std/uuid.cpp new file mode 100644 index 00000000..a82286d7 --- /dev/null +++ b/deps/ox/src/ox/std/uuid.cpp @@ -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::generate() noexcept { + if (!s_seeded) { + return OxError(1, "UUID generator not seeded."); + } + UUID out; + for (auto &v : out.m_value) { + v = static_cast(s_rand.gen() % 255); + } + out.m_value[6] = 4; + return out; +} + +} diff --git a/deps/ox/src/ox/std/uuid.hpp b/deps/ox/src/ox/std/uuid.hpp new file mode 100644 index 00000000..079dccb5 --- /dev/null +++ b/deps/ox/src/ox/std/uuid.hpp @@ -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 valMap { + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + }; + ox::Array out; + out[0] = valMap[static_cast((v & 0xf0) / 0xf)]; + out[1] = valMap[static_cast(v & 0x0f)]; + out[2] = 0; + return out.data(); +} +} + +class UUID { + private: + static bool s_seeded; + static Random s_rand; + ox::Array m_value; + + public: + static void seed(const RandomSeed &seed) noexcept; + + static ox::Result 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 &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; + } +}; + +}