From 14dc42b991290c991e9aea20cc99ae27a2e0b697 Mon Sep 17 00:00:00 2001 From: sspacelynx <37104290+sspacelynx@users.noreply.github.com> Date: Fri, 3 Sep 2021 14:58:02 +0200 Subject: [PATCH] Add `IntegerFor` template class + Update `GenerateUuidV4` --- app/src/main/cpp/skyline/common.h | 19 +++++++++++++++++++ app/src/main/cpp/skyline/common/uuid.cpp | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/skyline/common.h b/app/src/main/cpp/skyline/common.h index 359de46c..7b3dab13 100644 --- a/app/src/main/cpp/skyline/common.h +++ b/app/src/main/cpp/skyline/common.h @@ -340,6 +340,20 @@ namespace skyline { return frz::elsa{}(frz::string(view.data(), view.size()), 0); } + /** + * @brief Selects the largest possible integer type for representing an object alongside providing the size of the object in terms of the underlying type + */ + template + struct IntegerFor { + using type = std::conditional_t + > + >; + + static constexpr size_t count{sizeof(T) / sizeof(type)}; + }; + namespace detail { static thread_local std::mt19937_64 generator{GetTimeTicks()}; } @@ -354,6 +368,11 @@ namespace skyline { std::generate(in.begin(), in.end(), gen); } + + template requires (!std::is_integral_v && std::is_trivially_copyable_v) + void FillRandomBytes(T &object) { + FillRandomBytes(std::span(reinterpret_cast::type *>(&object), IntegerFor::count)); + } } /** diff --git a/app/src/main/cpp/skyline/common/uuid.cpp b/app/src/main/cpp/skyline/common/uuid.cpp index a5ce4e81..9ea9baf9 100644 --- a/app/src/main/cpp/skyline/common/uuid.cpp +++ b/app/src/main/cpp/skyline/common/uuid.cpp @@ -58,7 +58,7 @@ namespace skyline { // Create an initial random UUID UuidLayout uuid; - util::FillRandomBytes(span(&uuid, 1).cast()); + util::FillRandomBytes(uuid); // Set format bits uuid.reserved = reserved;