7#include "../utility/utility.hpp"
10#include "../macros.hpp"
14hi_export_module(hikogui.codec.pickle);
16hi_export
namespace hi {
inline namespace v1 {
23hi_export
template<
typename T>
32 hi_static_not_implemented();
35 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
36 requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
38 auto *rhs_ =
reinterpret_cast<std::byte
const *
>(&rhs);
39 return datum{base64::encode({rhs_,
sizeof(rhs_)})};
50 hi_static_not_implemented();
54 requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
56 if (
auto *b = get_if<std::string>(rhs)) {
57 auto tmp = base64::decode(*b);
58 if (tmp.size() !=
sizeof(T)) {
60 std::format(
"Length of base64 encoded object is {}, expected length {}", tmp.size(),
sizeof(T)));
68 throw parse_error(std::format(
"Expecting std::string to be encoded as a base64-string, got {}", rhs));
73hi_export
template<numeric_
integral T>
75 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
82 if (
auto *i = get_if<long long>(rhs)) {
84 throw parse_error(std::format(
"Encoded value is to out of range, got {}", rhs));
86 return static_cast<T
>(*i);
88 throw parse_error(std::format(
"Expecting numeric integrals to be encoded as a long long, got {}", rhs));
93hi_export
template<std::
floating_po
int T>
95 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
100 [[nodiscard]] T
decode(datum rhs)
const
102 if (
auto *f = get_if<double>(rhs)) {
103 return static_cast<T
>(*f);
105 }
else if (
auto *i = get_if<long long>(rhs)) {
106 return static_cast<T
>(*i);
109 throw parse_error(std::format(
"Expecting floating point to be encoded as a double or long long, got {}", rhs));
116 [[nodiscard]]
datum encode(
bool const& rhs)
const noexcept
123 if (
auto *b = get_if<bool>(rhs)) {
127 throw parse_error(std::format(
"Expecting bool to be encoded as a bool, got {}", rhs));
141 if (
auto *b = get_if<std::string>(rhs)) {
145 throw parse_error(std::format(
"Expecting std::string to be encoded as a string, got {}", rhs));
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A dynamic data type.
Definition datum.hpp:198
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:24
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
Definition pickle.hpp:48
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
Definition pickle.hpp:30
Exception thrown during parsing on an error.
Definition exception_intf.hpp:48