10#include "concepts.hpp"
14namespace hi::inline v1 {
39template<numeric_
integral T>
41 [[nodiscard]]
datum encode(T
const &rhs)
const noexcept
46 [[nodiscard]] T decode(
datum rhs)
const
48 if (
auto *i = get_if<long long>(rhs)) {
50 throw parse_error(std::format(
"Encoded value is to out of range, got {}", rhs));
52 return static_cast<T
>(*i);
54 throw parse_error(std::format(
"Expecting numeric integrals to be encoded as a long long, got {}", rhs));
59template<std::
floating_po
int T>
61 [[nodiscard]]
datum encode(T
const &rhs)
const noexcept
66 [[nodiscard]] T decode(datum rhs)
const
68 if (
auto *f = get_if<double>(rhs)) {
69 return static_cast<T
>(*f);
71 }
else if (
auto *i = get_if<long long>(rhs)) {
72 return static_cast<T
>(*i);
75 throw parse_error(std::format(
"Expecting floating point to be encoded as a double or long long, got {}", rhs));
82 [[nodiscard]]
datum encode(
bool const &rhs)
const noexcept
87 [[nodiscard]]
bool decode(
datum rhs)
const
89 if (
auto *b = get_if<bool>(rhs)) {
93 throw parse_error(std::format(
"Expecting bool to be encoded as a bool, got {}", rhs));
107 if (
auto *b = get_if<std::string>(rhs)) {
111 throw parse_error(std::format(
"Expecting std::string to be encoded as a string, got {}", rhs));
This file includes required definitions.
A dynamic data type.
Definition datum.hpp:209
Exception thrown during parsing on an error.
Definition exception.hpp:25
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:22
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.