1 #ifndef GARLIC_ENCODING_H
2 #define GARLIC_ENCODING_H
13 template<
typename Type>
19 template<GARLIC_VIEW Layer>
21 decode(Layer&& layer) {
return layer.get_int(); }
23 template<GARLIC_REF Layer>
25 encode(Layer&& layer,
int value) { layer.set_int(value); }
27 template<GARLIC_VIEW Layer,
typename Callable>
29 safe_decode(Layer&& layer, Callable&& cb) {
38 template<GARLIC_VIEW Layer>
40 decode(Layer&& layer) {
return layer.get_bool(); }
42 template<GARLIC_REF Layer>
44 encode(Layer&& layer,
bool value) { layer.set_bool(value); }
46 template<GARLIC_VIEW Layer,
typename Callable>
48 safe_decode(Layer&& layer, Callable&& cb) {
55 struct coder<std::string> {
57 template<GARLIC_VIEW Layer>
58 static inline std::string
59 decode(Layer&& layer) {
return layer.get_string(); }
61 template<GARLIC_REF Layer>
63 encode(Layer&& layer,
const std::string& value) {
64 layer.set_string(value.c_str());
67 template<GARLIC_VIEW Layer,
typename Callable>
69 safe_decode(Layer&& layer, Callable&& cb) {
70 if (layer.is_string())
71 cb(layer.get_string());
76 struct coder<std::string_view> {
78 template<GARLIC_VIEW Layer>
79 static inline std::string_view
80 decode(Layer&& layer) {
return layer.get_string_view(); }
82 template<GARLIC_REF Layer>
84 encode(Layer&& layer, std::string_view value) {
85 layer.set_string(value.data());
88 template<GARLIC_VIEW Layer,
typename Callable>
90 safe_decode(Layer&& layer, Callable&& cb) {
91 if (layer.is_string())
92 cb(layer.get_string_view());
99 template<GARLIC_VIEW Layer>
101 decode(Layer&& layer) {
102 auto view = layer.get_string_view();
103 return text(view.data(), view.size());
106 template<GARLIC_REF Layer>
108 encode(Layer&& layer, text value) {
109 layer.set_string(value.data());
112 template<GARLIC_VIEW Layer,
typename Callable>
114 safe_decode(Layer&& layer, Callable&& cb) {
115 if (layer.is_string())
116 cb(coder::decode(layer));
121 struct coder<const char*> {
123 template<GARLIC_VIEW Layer>
124 static inline const char*
125 decode(Layer&& layer) {
return layer.get_cstr(); }
127 template<GARLIC_REF Layer>
129 encode(Layer&& layer,
const char* value) { layer.set_string(value); }
131 template<GARLIC_VIEW Layer,
typename Callable>
133 safe_decode(Layer&& layer, Callable&& cb) {
134 if (layer.is_string())
135 cb(layer.get_cstr());
140 struct coder<double> {
142 template<GARLIC_VIEW Layer>
144 decode(Layer&& layer) {
return layer.get_double(); }
146 template<GARLIC_REF Layer>
148 encode(Layer&& layer,
double value) {
149 layer.set_double(value);
152 template<GARLIC_VIEW Layer,
typename Callable>
154 safe_decode(Layer&& layer, Callable&& cb) {
155 if (layer.is_double())
156 cb(layer.get_double());
162 template<
typename,
typename,
class =
void>
163 struct has_explicit_decoder : std::false_type {};
165 template<
typename Type,
typename Layer>
166 struct has_explicit_decoder<
168 std::void_t<decltype(coder<Type>::decode(std::declval<Layer>()))>> : std::true_type {};
170 template<
typename T,
typename L>
171 using explicit_safe_decode_t = decltype(
172 coder<T>::safe_decode(std::declval<L>(), std::declval<
void(*)(T&&)>()));
174 template<
typename,
typename,
class =
void>
175 struct has_explicit_safe_decoder : std::false_type {};
177 template<
typename,
typename,
class =
void>
178 struct has_explicit_encoder : std::false_type {};
180 template<
typename Type,
typename Layer>
181 struct has_explicit_encoder<
184 decltype(coder<Type>::encode(std::declval<Layer>(), std::declval<const Type&>()))
185 >> : std::true_type {};
187 template<
typename Type,
typename Layer>
188 struct has_explicit_safe_decoder<
190 std::void_t<internal::explicit_safe_decode_t<Type, Layer>>> : std::true_type {};
192 template<
typename,
typename,
class =
void>
193 struct is_layer_constructible : std::false_type{};
195 template<
typename Type,
typename Layer>
196 struct is_layer_constructible<
198 std::void_t<decltype(Type(std::declval<Layer>()))>> : std::true_type {};
200 template<
typename,
typename,
class =
void>
201 struct has_decode_layer_method : std::false_type {};
203 template<
typename Type,
typename Layer>
204 struct has_decode_layer_method<
206 std::void_t<decltype(Type::decode(std::declval<Layer>()))>> : std::true_type {};
208 template<
typename,
typename,
class =
void>
209 struct has_encode_layer_method : std::false_type {};
211 template<
typename Type,
typename Layer>
212 struct has_encode_layer_method<
215 decltype(Type::encode(std::declval<Layer>(), std::declval<const Type&>()))
216 >> : std::true_type {};
218 template<
typename,
typename,
class =
void>
219 struct has_safe_decode_layer_method : std::false_type {};
221 template<
typename Type,
typename Layer>
222 struct has_safe_decode_layer_method<
225 decltype(Type::safe_decode(std::declval<Layer>(), std::declval<void(*)(Type&&)>()))>
226 > : std::true_type {};
228 template<
typename Type,
typename Layer>
229 constexpr
bool use_explicit_decoder = has_explicit_decoder<Type, Layer>::value;
231 template<
typename Type,
typename Layer>
232 constexpr
bool use_explicit_safe_decoder = has_explicit_safe_decoder<Type, Layer>::value;
234 template<
typename Type,
typename Layer>
235 constexpr
bool use_decode_layer_method =
236 has_decode_layer_method<Type, Layer>::value
237 && !has_explicit_decoder<Type, Layer>::value;
239 template<
typename Type,
typename Layer>
240 constexpr
bool use_safe_decode_layer_method =
241 has_safe_decode_layer_method<Type, Layer>::value
242 && !has_explicit_safe_decoder<Type, Layer>::value;
244 template<
typename Type,
typename Layer>
245 constexpr
bool use_decode_constructor =
246 is_layer_constructible<Type, Layer>::value
247 && !has_decode_layer_method<Type, Layer>::value
248 && !has_explicit_decoder<Type, Layer>::value;
250 template<
typename Type,
typename Layer>
251 constexpr
bool use_explicit_encoder = has_explicit_encoder<Type, Layer>::value;
253 template<
typename Type,
typename Layer>
254 constexpr
bool use_encode_layer_method =
255 has_encode_layer_method<Type, Layer>::value
256 && !has_explicit_encoder<Type, Layer>::value;
259 template<
typename Type, GARLIC_VIEW Layer>
260 static inline std::enable_if_t<internal::use_decode_layer_method<Type, Layer>, Type>
261 decode(Layer&& layer) {
return Type::decode(layer); }
263 template<
typename Type, GARLIC_VIEW Layer>
264 static inline std::enable_if_t<internal::use_decode_constructor<Type, Layer>, Type>
265 decode(Layer&& layer) {
return Type(layer); }
273 template<
typename Type, GARLIC_VIEW Layer>
274 static inline std::enable_if_t<internal::use_explicit_decoder<Type, Layer>, Type>
275 decode(Layer&& layer) {
return coder<Type>::decode(layer); }
277 template<
typename Type, GARLIC_VIEW Layer,
typename Callable>
278 static inline std::enable_if_t<internal::use_explicit_safe_decoder<Type, Layer>>
279 safe_decode(Layer&& layer, Callable&& cb) {
280 coder<Type>::safe_decode(layer, cb);
290 template<
typename Type, GARLIC_VIEW Layer,
typename Callable>
291 static inline std::enable_if_t<internal::use_safe_decode_layer_method<Type, Layer>>
292 safe_decode(Layer&& layer, Callable&& cb) {
293 Type::safe_decode(layer, cb);
296 template<GARLIC_REF Layer,
typename Type>
297 static inline std::enable_if_t<internal::use_explicit_encoder<Type, Layer>>
298 encode(Layer&& layer,
const Type& value) {
299 coder<Type>::encode(layer, value);
310 template<GARLIC_REF Layer,
typename Type>
311 static inline std::enable_if_t<internal::use_encode_layer_method<Type, Layer>>
312 encode(Layer&& layer,
const Type& value) {
313 Type::encode(layer, value);