Sequoia
Loading...
Searching...
No Matches
CoreInfrastructure.hpp
Go to the documentation of this file.
1
2// Copyright Oliver J. Rosten 2020. //
3// Distributed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. //
4// (See accompanying file LICENSE.md or copy at //
5// https://www.gnu.org/licenses/gpl-3.0.en.html) //
7
8#pragma once
9
16
17#include <format>
18#include <filesystem>
19#include <sstream>
20
21namespace sequoia::testing
22{
27 template<class T>
28 struct serializer;
29
30 template<std::formattable<char> T>
31 struct serializer<T>
32 {
33 [[nodiscard]]
34 static std::string make(const T& val)
35 {
36 return std::format("{}", val);
37 }
38 };
39
40 template<serializable_to<std::stringstream> T>
41 requires (!std::formattable<T, char>)
42 struct serializer<T>
43 {
44 [[nodiscard]]
45 static std::string make(const T& val)
46 {
47 std::ostringstream os{};
48 os << std::boolalpha << val;
49 return os.str();
50 }
51 };
52
53 template<class T>
54 concept serializable = requires(serializer<T>& s, T& t) {
55 s.make(t);
56 };
57
58 template<serializable T>
59 [[nodiscard]]
60 std::string to_string(const T& value)
61 {
62 return serializer<T>::make(value);
63 }
64
66 template<class T>
68 {
69 using type = T;
70 };
71
72 template<class T>
73 requires (std::is_unsigned_v<T> && (sizeof(T) == sizeof(uint64_t)))
75 {
76 using type = uint64_t;
77 };
78
79 template<class T>
80 requires (std::is_unsigned_v<T> && (sizeof(T) == sizeof(uint32_t)))
81 struct type_normalizer<T>
82 {
83 using type = uint32_t;
84 };
85
86 template<class T>
87 using type_normalizer_t = typename type_normalizer<T>::type;
88
89 template<std::integral T>
90 [[nodiscard]]
91 auto fixed_width_unsigned_cast(T x) noexcept
92 {
93 using U = std::make_unsigned_t<T>;
94
95 return static_cast<type_normalizer_t<U>>(x);
96 }
97
99 {
100 int num{};
101 std::string top_level_message{};
102 };
103}
Concepts which are sufficiently general to appear in the sequoia namespace.
Definition: CoreInfrastructure.hpp:54
Specialize this struct template to provide custom serialization of a given class. .
Definition: CoreInfrastructure.hpp:28
Primary class template for converting unsigned types of implementation-defined size into fixed-width ...
Definition: CoreInfrastructure.hpp:68
Definition: CoreInfrastructure.hpp:99