HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
pickle.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility/module.hpp"
8#include "datum.hpp"
9#include "codec/base_n.hpp"
10#include <string>
11#include <limits>
12
13namespace hi::inline v1 {
14
20template<typename T>
21struct pickle {
27 [[nodiscard]] datum encode(T const& rhs) const noexcept
28 {
30 }
31
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>)
34 {
35 auto *rhs_ = reinterpret_cast<std::byte const *>(&rhs);
36 return datum{base64::encode({rhs_, sizeof(rhs_)})};
37 }
38
45 [[nodiscard]] T decode(datum rhs) const
46 {
48 }
49
50 [[nodiscard]] T decode(datum rhs) const requires(std::has_unique_object_representations_v<T> and not std::is_pointer_v<T>)
51 {
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)));
56 }
57
58 auto r = T{};
59 std::memcpy(&r, tmp.data(), sizeof(T));
60 return r;
61
62 } else {
63 throw parse_error(std::format("Expecting std::string to be encoded as a base64-string, got {}", rhs));
64 }
65 }
66};
67
68template<numeric_integral T>
69struct pickle<T> {
70 [[nodiscard]] datum encode(T const& rhs) const noexcept
71 {
72 return datum{rhs};
73 }
74
75 [[nodiscard]] T decode(datum rhs) const
76 {
77 if (auto *i = get_if<long long>(&rhs)) {
79 throw parse_error(std::format("Encoded value is to out of range, got {}", rhs));
80 }
81 return static_cast<T>(*i);
82 } else {
83 throw parse_error(std::format("Expecting numeric integrals to be encoded as a long long, got {}", rhs));
84 }
85 }
86};
87
88template<std::floating_point T>
89struct pickle<T> {
90 [[nodiscard]] datum encode(T const& rhs) const noexcept
91 {
92 return datum{rhs};
93 }
94
95 [[nodiscard]] T decode(datum rhs) const
96 {
97 if (auto *f = get_if<double>(&rhs)) {
98 return static_cast<T>(*f);
99
100 } else if (auto *i = get_if<long long>(&rhs)) {
101 return static_cast<T>(*i);
102
103 } else {
104 throw parse_error(std::format("Expecting floating point to be encoded as a double or long long, got {}", rhs));
105 }
106 }
107};
108
109template<>
110struct pickle<bool> {
111 [[nodiscard]] datum encode(bool const& rhs) const noexcept
112 {
113 return datum{rhs};
114 }
115
116 [[nodiscard]] bool decode(datum rhs) const
117 {
118 if (auto *b = get_if<bool>(&rhs)) {
119 return *b;
120
121 } else {
122 throw parse_error(std::format("Expecting bool to be encoded as a bool, got {}", rhs));
123 }
124 }
125};
126
127template<>
128struct pickle<std::string> {
129 [[nodiscard]] datum encode(std::string const& rhs) const noexcept
130 {
131 return datum{rhs};
132 }
133
134 [[nodiscard]] std::string decode(datum rhs) const
135 {
136 if (auto *b = get_if<std::string>(&rhs)) {
137 return *b;
138
139 } else {
140 throw parse_error(std::format("Expecting std::string to be encoded as a string, got {}", rhs));
141 }
142 }
143};
144
145
146} // namespace hi::inline v1
#define hi_static_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:342
STL namespace.
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
T memcpy(T... args)