7#include "utility/module.hpp"
9#include "codec/base_n.hpp"
13namespace hi::inline
v1 {
32 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
33 requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
35 auto *rhs_ =
reinterpret_cast<std::byte
const *
>(&rhs);
36 return datum{base64::encode({rhs_,
sizeof(rhs_)})};
50 [[nodiscard]] T decode(
datum rhs)
const requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
52 if (
auto *b = get_if<std::string>(rhs)) {
53 auto tmp = base64::decode(*b);
54 if (tmp.size() !=
sizeof(T)) {
55 throw parse_error(std::format(
"Length of base64 encoded object is {}, expected length {}", tmp.size(),
sizeof(T)));
63 throw parse_error(std::format(
"Expecting std::string to be encoded as a base64-string, got {}", rhs));
68template<numeric_
integral T>
70 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
75 [[nodiscard]] T decode(
datum rhs)
const
77 if (
auto *i = get_if<long long>(rhs)) {
79 throw parse_error(std::format(
"Encoded value is to out of range, got {}", rhs));
81 return static_cast<T
>(*i);
83 throw parse_error(std::format(
"Expecting numeric integrals to be encoded as a long long, got {}", rhs));
88template<std::
floating_po
int T>
90 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
95 [[nodiscard]] T decode(datum rhs)
const
97 if (
auto *f = get_if<double>(rhs)) {
98 return static_cast<T
>(*f);
100 }
else if (
auto *i = get_if<long long>(rhs)) {
101 return static_cast<T
>(*i);
104 throw parse_error(std::format(
"Expecting floating point to be encoded as a double or long long, got {}", rhs));
111 [[nodiscard]]
datum encode(
bool const& rhs)
const noexcept
116 [[nodiscard]]
bool decode(
datum rhs)
const
118 if (
auto *b = get_if<bool>(rhs)) {
122 throw parse_error(std::format(
"Expecting bool to be encoded as a bool, got {}", rhs));
136 if (
auto *b = get_if<std::string>(rhs)) {
140 throw parse_error(std::format(
"Expecting std::string to be encoded as a string, got {}", rhs));
#define hi_static_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:327
DOXYGEN BUG.
Definition algorithm.hpp:13
A dynamic data type.
Definition datum.hpp:219
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:21
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
Definition pickle.hpp:27
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
Definition pickle.hpp:45