Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
ResultTypes.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include <type_traits>
5
6#include "Stroika/Foundation/Cryptography/Format.h"
7
8namespace Stroika::Foundation::Cryptography::Digest {
9
10 namespace Private_ {
11 // Try to use simple constuction of result from argument if possible
12 template <typename OUT_RESULT, typename IN_RESULT>
13 constexpr OUT_RESULT mkReturnType_ (IN_RESULT hashVal)
14 {
15 if constexpr (constructible_from<OUT_RESULT, IN_RESULT>) {
16 return OUT_RESULT (hashVal); // intentionally allow narrowing conversions (so () not {})
17 }
18 else if constexpr (is_trivially_copyable_v<IN_RESULT> and is_trivially_copyable_v<OUT_RESULT>) {
19 // Else if both (IN AND OUT) values trivially copyable, use memcpy (and zero fill result as needed)
20 size_t mBytes2Copy = std::min (sizeof (OUT_RESULT), sizeof (IN_RESULT));
21 OUT_RESULT result{}; // zero initialize non-copied bits (@todo could just zero-fill end bits)
22 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wclass-memaccess\"") // memcpy only requires trivially_copyable, not is_trivial
23 ::memcpy (&result, &hashVal, mBytes2Copy);
24 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wclass-memaccess\"")
25 return result;
26 }
27 else if constexpr (same_as<OUT_RESULT, string> or same_as<OUT_RESULT, Characters::String> or same_as<OUT_RESULT, Common::GUID>) {
28 return Format<OUT_RESULT> (hashVal);
29 }
30 // else error
31 }
32 }
33
34 template <typename OUT_RESULT, typename IN_RESULT>
35 constexpr OUT_RESULT ConvertResult (IN_RESULT inResult)
36 {
37 return Private_::mkReturnType_<OUT_RESULT> (inResult);
38 }
39
40}