10#include "concepts.hpp"
11#include "codec/base_n.hpp"
15namespace hi::inline
v1 {
34 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
35 requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
37 auto *rhs_ =
reinterpret_cast<std::byte
const *
>(&rhs);
38 return datum{base64::encode({rhs_,
sizeof(rhs_)})};
52 [[nodiscard]] T decode(
datum rhs)
const requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
54 if (
auto *b = get_if<std::string>(rhs)) {
55 auto tmp = base64::decode(*b);
56 if (tmp.size() !=
sizeof(T)) {
57 throw parse_error(std::format(
"Length of base64 encoded object is {}, expected length {}", tmp.size(),
sizeof(T)));
65 throw parse_error(std::format(
"Expecting std::string to be encoded as a base64-string, got {}", rhs));
70template<numeric_
integral T>
72 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
77 [[nodiscard]] T decode(
datum rhs)
const
79 if (
auto *i = get_if<long long>(rhs)) {
81 throw parse_error(std::format(
"Encoded value is to out of range, got {}", rhs));
83 return static_cast<T
>(*i);
85 throw parse_error(std::format(
"Expecting numeric integrals to be encoded as a long long, got {}", rhs));
90template<std::
floating_po
int T>
92 [[nodiscard]]
datum encode(T
const& rhs)
const noexcept
97 [[nodiscard]] T decode(datum rhs)
const
99 if (
auto *f = get_if<double>(rhs)) {
100 return static_cast<T
>(*f);
102 }
else if (
auto *i = get_if<long long>(rhs)) {
103 return static_cast<T
>(*i);
106 throw parse_error(std::format(
"Expecting floating point to be encoded as a double or long long, got {}", rhs));
113 [[nodiscard]]
datum encode(
bool const& rhs)
const noexcept
118 [[nodiscard]]
bool decode(
datum rhs)
const
120 if (
auto *b = get_if<bool>(rhs)) {
124 throw parse_error(std::format(
"Expecting bool to be encoded as a bool, got {}", rhs));
138 if (
auto *b = get_if<std::string>(rhs)) {
142 throw parse_error(std::format(
"Expecting std::string to be encoded as a string, got {}", rhs));
Utilities to assert and bound check.
#define hi_static_not_implemented()
This part of the code has not been implemented yet.
Definition assert.hpp:187
Utilities used by the HikoGUI library itself.
DOXYGEN BUG.
Definition algorithm.hpp:15
A dynamic data type.
Definition datum.hpp:224
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:23
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
Definition pickle.hpp:29
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
Definition pickle.hpp:47