diff --git a/include/rfl/parsing/AreReaderAndWriter.hpp b/include/rfl/parsing/AreReaderAndWriter.hpp new file mode 100644 index 00000000..962f6390 --- /dev/null +++ b/include/rfl/parsing/AreReaderAndWriter.hpp @@ -0,0 +1,16 @@ +#ifndef RFL_PARSING_AREREADERANDWRITER_HPP_ +#define RFL_PARSING_AREREADERANDWRITER_HPP_ + +#include "rfl/parsing/IsReader.hpp" +#include "rfl/parsing/IsWriter.hpp" + +namespace rfl { +namespace parsing { + +template +concept AreReaderAndWriter = IsReader && IsWriter; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/IsReader.hpp b/include/rfl/parsing/IsReader.hpp new file mode 100644 index 00000000..99c1fcbd --- /dev/null +++ b/include/rfl/parsing/IsReader.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_PARSING_ISREADER_HPP_ +#define RFL_PARSING_ISREADER_HPP_ + +#include +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace parsing { + +template +concept IsReader = requires(R r, typename R::InputArrayType arr, + typename R::InputObjectType obj, + typename R::InputVarType var) { + { r.template to_basic_type(&var) } -> std::same_as>; + + { + r.to_array(&var) + } -> std::same_as>; + + { + r.to_map(&obj) + } -> std::same_as< + std::vector>>; + + { + r.to_object(&var) + } -> std::same_as>; + + { r.to_vec(&arr) } -> std::same_as>; + + { + r.template use_custom_constructor(&var) + } -> std::same_as>; +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/IsWriter.hpp b/include/rfl/parsing/IsWriter.hpp new file mode 100644 index 00000000..11c89a2f --- /dev/null +++ b/include/rfl/parsing/IsWriter.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_PARSING_ISWRITER_HPP_ +#define RFL_PARSING_ISWRITER_HPP_ + +#include + +#include "rfl/Result.hpp" + +namespace rfl { +namespace parsing { + +template +concept IsWriter = requires(W w, std::string name, + typename W::OutputArrayType arr, + typename W::OutputObjectType obj, + typename W::OutputVarType var) { + { w.add(var, &arr) } -> std::same_as; + + { w.empty_var() } -> std::same_as; + + { w.new_array() } -> std::same_as; + + { w.new_object() } -> std::same_as; + + { w.is_empty(&var) } -> std::same_as; + + { w.set_field(name, var, &obj) } -> std::same_as; +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index aeb4fb3a..cf28477a 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -35,6 +35,7 @@ #include "rfl/internal/no_duplicate_field_names.hpp" #include "rfl/internal/to_ptr_named_tuple.hpp" #include "rfl/named_tuple_t.hpp" +#include "rfl/parsing/AreReaderAndWriter.hpp" #include "rfl/parsing/is_forward_list.hpp" #include "rfl/parsing/is_map_like.hpp" #include "rfl/parsing/is_required.hpp" @@ -46,20 +47,22 @@ namespace parsing { // ---------------------------------------------------------------------------- -template +template +requires AreReaderAndWriter struct Parser; // ---------------------------------------------------------------------------- /// Default case - anything that cannot be explicitly matched. -template +template +requires AreReaderAndWriter struct Parser { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result read(const ReaderType& _r, InputVarType* _var) noexcept { - if constexpr (ReaderType::template has_custom_constructor) { + static Result read(const R& _r, InputVarType* _var) noexcept { + if constexpr (R::template has_custom_constructor) { return _r.template use_custom_constructor(_var); } else { if constexpr (internal::has_reflection_type_v) { @@ -71,9 +74,8 @@ struct Parser { return Error(e.what()); } }; - return Parser::read( - _r, _var) - .and_then(wrap_in_t); + return Parser::read(_r, _var).and_then( + wrap_in_t); } else if constexpr (std::is_class_v && !std::is_same()) { using NamedTupleType = named_tuple_t; @@ -84,9 +86,8 @@ struct Parser { return Error(e.what()); } }; - return Parser::read( - _r, _var) - .and_then(to_struct); + return Parser::read(_r, _var).and_then( + to_struct); } else { return _r.template to_basic_type>(_var); } @@ -94,36 +95,35 @@ struct Parser { } /// Converts the variable to a JSON type. - static auto write(const WriterType& _w, const T& _var) noexcept { + static auto write(const W& _w, const T& _var) noexcept { if constexpr (internal::has_reflection_type_v) { using ReflectionType = std::decay_t; if constexpr (internal::has_reflection_method_v) { - return Parser::write( - _w, _var.reflection()); + return Parser::write(_w, + _var.reflection()); } else { const auto& [r] = _var; - return Parser::write(_w, - r); + return Parser::write(_w, r); } } else if constexpr (std::is_class_v && !std::is_same()) { const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var); using PtrNamedTupleType = std::decay_t; - return Parser::write( - _w, ptr_named_tuple); + return Parser::write(_w, ptr_named_tuple); } else { return _w.from_basic_type(_var); } } }; -template -struct Parser { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter +struct Parser { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result read(const ReaderType& _r, InputVarType* _var) noexcept { + static Result read(const R& _r, InputVarType* _var) noexcept { static_assert( always_false_v, "Reading into raw pointers is dangerous and therefore unsupported. " @@ -133,27 +133,27 @@ struct Parser { } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, const T* _ptr) noexcept { + static OutputVarType write(const W& _w, const T* _ptr) noexcept { if (!_ptr) { return _w.empty_var(); } - return Parser>::write(_w, - *_ptr); + return Parser>::write(_w, *_ptr); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { +template +requires AreReaderAndWriter> +struct Parser> { public: - using InputArrayType = typename ReaderType::InputArrayType; - using InputVarType = typename ReaderType::InputVarType; + using InputArrayType = typename R::InputArrayType; + using InputVarType = typename R::InputVarType; - using OutputArrayType = typename WriterType::OutputArrayType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputArrayType = typename W::OutputArrayType; + using OutputVarType = typename W::OutputVarType; - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); }; @@ -177,13 +177,11 @@ struct Parser> { .and_then(extract); } - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::array& _arr) noexcept { auto arr = _w.new_array(); for (auto it = _arr.begin(); it != _arr.end(); ++it) { - _w.add( - Parser>::write(_w, *it), - &arr); + _w.add(Parser>::write(_w, *it), &arr); } return OutputVarType(arr); } @@ -192,7 +190,7 @@ struct Parser> { /// Extracts values from the array, field by field. template static Result> extract_field_by_field( - const ReaderType& _r, std::vector _vec, + const R& _r, std::vector _vec, AlreadyExtracted&&... _already_extracted) noexcept { constexpr size_t i = sizeof...(AlreadyExtracted); if constexpr (i == _size) { @@ -209,47 +207,44 @@ struct Parser> { /// Extracts a single field from a JSON. template - static auto extract_single_field(const ReaderType& _r, + static auto extract_single_field(const R& _r, std::vector* _vec) noexcept { - return Parser::read(_r, &((*_vec)[_i])); + return Parser::read(_r, &((*_vec)[_i])); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, - InputVarType* _var) noexcept { + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_box = [](auto&& _t) { return Box::make(std::move(_t)); }; - return Parser>::read(_r, _var) - .transform(to_box); + return Parser>::read(_r, _var).transform(to_box); } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, - const Box& _box) noexcept { - return Parser>::write(_w, - *_box); + static OutputVarType write(const W& _w, const Box& _box) noexcept { + return Parser>::write(_w, *_box); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_type = [](const auto& _str) { return Literal<_fields...>::from_string(_str); @@ -265,7 +260,7 @@ struct Parser> { } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const Literal<_fields...>& _literal) noexcept { return _w.from_basic_type(_literal.name()); } @@ -275,25 +270,25 @@ struct Parser> { /// Used for maps for which the key type is std::string. These are represented /// as objects. -template +template +requires AreReaderAndWriter struct MapParser { public: - using InputObjectType = typename ReaderType::InputObjectType; - using InputVarType = typename ReaderType::InputVarType; + using InputObjectType = typename R::InputObjectType; + using InputVarType = typename R::InputVarType; - using OutputObjectType = typename WriterType::OutputObjectType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputObjectType = typename W::OutputObjectType; + using OutputVarType = typename W::OutputVarType; using ValueType = std::decay_t; - static Result read(const ReaderType& _r, - InputVarType* _var) noexcept { + static Result read(const R& _r, InputVarType* _var) noexcept { const auto get_pair = [&](auto& _pair) { const auto to_pair = [&](ValueType&& _val) { return std::make_pair(std::move(_pair.first), std::move(_val)); }; - return Parser>::read(_r, &_pair.second) + return Parser>::read(_r, + &_pair.second) .transform(to_pair) .value(); }; @@ -314,42 +309,42 @@ struct MapParser { return _r.to_object(_var).and_then(to_map); } - static OutputVarType write(const WriterType& _w, - const MapType& _m) noexcept { + static OutputVarType write(const W& _w, const MapType& _m) noexcept { auto obj = _w.new_object(); for (const auto& [k, v] : _m) { - _w.set_field( - k, - Parser>::write( - _w, v), - &obj); + _w.set_field(k, Parser>::write(_w, v), + &obj); } return obj; } }; -template -struct Parser> - : public MapParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public MapParser> { +}; -template -struct Parser> - : public MapParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public MapParser> { +}; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputObjectType = typename ReaderType::InputObjectType; - using InputVarType = typename ReaderType::InputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputObjectType = typename R::InputObjectType; + using InputVarType = typename R::InputVarType; - using OutputObjectType = typename WriterType::OutputObjectType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputObjectType = typename W::OutputObjectType; + using OutputVarType = typename W::OutputVarType; public: /// Generates a NamedTuple from a JSON Object. - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_fields_array = [&](auto _obj) { return _r.template to_fields_array( @@ -362,7 +357,7 @@ struct Parser> { } /// Transforms a NamedTuple into a JSON object. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const NamedTuple& _tup) noexcept { auto obj = _w.new_object(); build_object_recursively(_w, _tup, &obj); @@ -373,7 +368,7 @@ struct Parser> { /// Builds the named tuple field by field. template static Result> build_named_tuple_recursively( - const ReaderType& _r, + const R& _r, const std::array, sizeof...(FieldTypes)>& _fields_vec, Args&&... _args) noexcept { @@ -424,7 +419,7 @@ struct Parser> { /// just good UX. template static Error collect_errors( - const ReaderType& _r, + const R& _r, const std::array, sizeof...(FieldTypes)>& _fields_vec, std::vector _errors) noexcept { @@ -472,7 +467,7 @@ struct Parser> { /// Builds the object field by field. template - static void build_object_recursively(const WriterType& _w, + static void build_object_recursively(const W& _w, const NamedTuple& _tup, OutputObjectType* _ptr) noexcept { if constexpr (_i >= sizeof...(FieldTypes)) { @@ -482,8 +477,7 @@ struct Parser> { typename std::tuple_element<_i, std::tuple>::type; using ValueType = std::decay_t; - auto value = Parser::write( - _w, rfl::get<_i>(_tup)); + auto value = Parser::write(_w, rfl::get<_i>(_tup)); const auto name = FieldType::name_.str(); if constexpr (!is_required()) { if (!_w.is_empty(&value)) { @@ -503,21 +497,20 @@ struct Parser> { if (field_indices_.size() == 0) { set_field_indices(); } - return Parser>::field_indices_; + return Parser>::field_indices_; } /// Retrieves the value from the object. This is mainly needed to /// generate a better error message. template - static auto get_value(const ReaderType& _r, InputVarType _var) noexcept { + static auto get_value(const R& _r, InputVarType _var) noexcept { const auto embellish_error = [&](const Error& _e) { const auto key = FieldType::name_.str(); return Error("Failed to parse field '" + key + "': " + _e.what()); }; using ValueType = std::decay_t; - return Parser::read(_r, &_var) - .or_else(embellish_error); + return Parser::read(_r, &_var).or_else( + embellish_error); } /// Builds the object field by field. @@ -530,8 +523,7 @@ struct Parser> { typename std::tuple_element<_i, std::tuple>::type; const auto name = FieldType::name_.string_view(); - Parser>::field_indices_[name] = _i; + Parser>::field_indices_[name] = _i; set_field_indices<_i + 1>(); } } @@ -543,66 +535,64 @@ struct Parser> { // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { if (_r.is_empty(_var)) { return std::optional(); } const auto to_opt = [](auto&& _t) { return std::make_optional(_t); }; - return Parser>::read(_r, _var) - .transform(to_opt); + return Parser>::read(_r, _var).transform(to_opt); } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::optional& _o) noexcept { if (!_o) { return _w.empty_var(); } - return Parser>::write(_w, *_o); + return Parser>::write(_w, *_o); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, - InputVarType* _var) noexcept { + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_ref = [&](auto&& _t) { return Ref::make(std::move(_t)); }; - return Parser>::read(_r, _var) - .transform(to_ref); + return Parser>::read(_r, _var).transform(to_ref); } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, - const Ref& _ref) noexcept { - return Parser>::write(_w, - *_ref); + static OutputVarType write(const W& _w, const Ref& _ref) noexcept { + return Parser>::write(_w, *_ref); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { if (_r.is_empty(_var)) { return std::shared_ptr(); @@ -610,67 +600,66 @@ struct Parser> { const auto to_ptr = [](auto&& _t) { return std::make_shared(std::move(_t)); }; - return Parser>::read(_r, _var) - .transform(to_ptr); + return Parser>::read(_r, _var).transform(to_ptr); } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::shared_ptr& _s) noexcept { if (!_s) { return _w.empty_var(); } - return Parser>::write(_w, *_s); + return Parser>::write(_w, *_s); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. static Result> read( - const ReaderType& _r, InputVarType* _var) noexcept { + const R& _r, InputVarType* _var) noexcept { const auto to_pair = [&](auto&& _t) { return std::make_pair(std::move(std::get<0>(_t)), std::move(std::get<1>(_t))); }; - return Parser>::read(_r, _var) + return Parser>::read(_r, _var) .transform(to_pair); } /// Transform a std::pair into an array static OutputVarType write( - const WriterType& _w, - const std::pair& _p) noexcept { + const W& _w, const std::pair& _p) noexcept { const auto tup = std::make_tuple(&_p.first, &_p.second); return Parser< - ReaderType, WriterType, - std::tuple>::write(_w, tup); + R, W, std::tuple>::write(_w, + tup); } }; // ---------------------------------------------------------------------------- -template -struct Parser> { +template +requires AreReaderAndWriter> +struct Parser> { using ResultType = Result>; public: - using InputObjectType = typename ReaderType::InputObjectType; - using InputVarType = typename ReaderType::InputVarType; + using InputObjectType = typename R::InputObjectType; + using InputVarType = typename R::InputVarType; - using OutputObjectType = typename WriterType::OutputObjectType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputObjectType = typename W::OutputObjectType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept { + static ResultType read(const R& _r, InputVarType* _var) noexcept { const auto get_disc = [&_r](auto _obj) { return get_discriminator(_r, &_obj); }; @@ -684,19 +673,17 @@ struct Parser& - _tagged_union) noexcept { + const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>& + _tagged_union) noexcept { using VariantType = typename TaggedUnion<_discriminator, AlternativeTypes...>::VariantType; - return Parser::write( - _w, _tagged_union.variant_); + return Parser::write(_w, _tagged_union.variant_); } private: template - static ResultType find_matching_alternative(const ReaderType& _r, + static ResultType find_matching_alternative(const R& _r, const std::string& _disc_value, InputVarType* _var) noexcept { if constexpr (_i == sizeof...(AlternativeTypes)) { @@ -720,8 +707,7 @@ struct Parser::read( - _r, _var) + return Parser::read(_r, _var) .transform(to_tagged_union) .or_else(embellish_error); @@ -733,7 +719,7 @@ struct Parser get_discriminator( - const ReaderType& _r, InputObjectType* _obj) noexcept { + const R& _r, InputObjectType* _obj) noexcept { const auto embellish_error = [](const auto& _err) { return Error( "Could not parse tagged union: Could not find field '" + @@ -767,16 +753,17 @@ struct Parser -struct Parser> { +template +requires AreReaderAndWriter> +struct Parser> { public: - using InputArrayType = typename ReaderType::InputArrayType; - using InputVarType = typename ReaderType::InputVarType; + using InputArrayType = typename R::InputArrayType; + using InputVarType = typename R::InputVarType; - using OutputArrayType = typename WriterType::OutputArrayType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputArrayType = typename W::OutputArrayType; + using OutputVarType = typename W::OutputVarType; - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { const auto to_vec = [&](auto _arr) { return _r.to_vec(&_arr); }; @@ -800,7 +787,7 @@ struct Parser> { .and_then(extract); } - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::tuple& _tup) noexcept { auto arr = _w.new_array(); to_array<0>(_w, _tup, &arr); @@ -811,7 +798,7 @@ struct Parser> { /// Extracts values from the array, field by field. template static Result> extract_field_by_field( - const ReaderType& _r, std::vector _vec, + const R& _r, std::vector _vec, AlreadyExtracted&&... _already_extracted) noexcept { constexpr size_t i = sizeof...(AlreadyExtracted); if constexpr (i == sizeof...(Ts)) { @@ -829,25 +816,23 @@ struct Parser> { /// Extracts a single field from a JSON. template - static auto extract_single_field(const ReaderType& _r, + static auto extract_single_field(const R& _r, std::vector* _vec) noexcept { using NewFieldType = std::decay_t< typename std::tuple_element<_i, std::tuple>::type>; - return Parser::read( - _r, &((*_vec)[_i])); + return Parser::read(_r, &((*_vec)[_i])); } /// Transforms a tuple to an array. template - static void to_array(const WriterType& _w, const std::tuple& _tup, + static void to_array(const W& _w, const std::tuple& _tup, OutputArrayType* _ptr) noexcept { if constexpr (_i < sizeof...(Ts)) { using NewFieldType = std::decay_t< typename std::tuple_element<_i, std::tuple>::type>; const auto val = - Parser::write( - _w, std::get<_i>(_tup)); + Parser::write(_w, std::get<_i>(_tup)); _w.add(val, _ptr); to_array<_i + 1>(_w, _tup, _ptr); } @@ -856,13 +841,14 @@ struct Parser> { // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static Result> read(const ReaderType& _r, + static Result> read(const R& _r, InputVarType* _var) noexcept { if (_r.is_empty(_var)) { return std::unique_ptr(); @@ -870,17 +856,16 @@ struct Parser> { const auto to_ptr = [](auto&& _t) { return std::make_unique(std::move(_t)); }; - return Parser>::read(_r, _var) - .transform(to_ptr); + return Parser>::read(_r, _var).transform(to_ptr); } /// Expresses the variable a a JSON. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::unique_ptr& _s) noexcept { if (!_s) { return _w.empty_var(); } - return Parser>::write(_w, *_s); + return Parser>::write(_w, *_s); } }; @@ -888,19 +873,20 @@ struct Parser> { /// To be used when all options of the variants are rfl::Field. Essentially, /// this is an externally tagged union. -template +template +requires AreReaderAndWriter> struct FieldVariantParser { using ResultType = Result>; public: - using InputObjectType = typename ReaderType::InputObjectType; - using InputVarType = typename ReaderType::InputVarType; + using InputObjectType = typename R::InputObjectType; + using InputVarType = typename R::InputVarType; - using OutputObjectType = typename WriterType::OutputObjectType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputObjectType = typename W::OutputObjectType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. - static ResultType read(const ReaderType& _r, InputVarType* _var) noexcept { + static ResultType read(const R& _r, InputVarType* _var) noexcept { static_assert( internal::no_duplicate_field_names>(), "Externally tagged variants cannot have duplicate field " @@ -925,7 +911,7 @@ struct FieldVariantParser { } /// Expresses the variables as a JSON type. - static OutputVarType write(const WriterType& _w, + static OutputVarType write(const W& _w, const std::variant& _v) noexcept { static_assert( internal::no_duplicate_field_names>(), @@ -936,8 +922,7 @@ struct FieldVariantParser { const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field)); using NamedTupleType = std::decay_t; - return Parser::write( - _w, named_tuple); + return Parser::write(_w, named_tuple); }; return std::visit(handle, _v); @@ -945,7 +930,7 @@ struct FieldVariantParser { private: template - static ResultType find_matching_alternative(const ReaderType& _r, + static ResultType find_matching_alternative(const R& _r, const std::string& _disc_value, InputVarType* _var) noexcept { if constexpr (_i == sizeof...(FieldTypes)) { @@ -972,7 +957,7 @@ struct FieldVariantParser { _disc_value + "': " + _e.what()); }; - return Parser::read(_r, _var) + return Parser::read(_r, _var) .transform(to_variant) .or_else(embellish_error); } else { @@ -984,20 +969,20 @@ struct FieldVariantParser { // ---------------------------------------------------------------------------- -template -struct Parser> { - using InputVarType = typename ReaderType::InputVarType; - using OutputVarType = typename WriterType::OutputVarType; +template +requires AreReaderAndWriter> +struct Parser> { + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; /// Expresses the variables as type T. template static Result> read( - const ReaderType& _r, InputVarType* _var, + const R& _r, InputVarType* _var, const std::string _errors = "") noexcept { if constexpr (_i == 0 && internal::all_fields>()) { - return FieldVariantParser::read(_r, _var); + return FieldVariantParser::read(_r, _var); } else if constexpr (_i == sizeof...(FieldTypes)) { return Error("Could not parse variant: " + _errors); } else { @@ -1014,7 +999,7 @@ struct Parser> { using AltType = std::decay_t< std::variant_alternative_t<_i, std::variant>>; - return Parser::read(_r, _var) + return Parser::read(_r, _var) .transform(to_variant) .or_else(try_next); } @@ -1022,15 +1007,13 @@ struct Parser> { /// Expresses the variables as a JSON type. static OutputVarType write( - const WriterType& _w, - const std::variant& _variant) noexcept { + const W& _w, const std::variant& _variant) noexcept { if constexpr (internal::all_fields>()) { - return FieldVariantParser::write(_w, _variant); + return FieldVariantParser::write(_w, _variant); } else { const auto handle = [&](const auto& _v) { using Type = std::decay_t; - return Parser::write(_w, _v); + return Parser::write(_w, _v); }; return std::visit(handle, _variant); } @@ -1043,33 +1026,31 @@ struct Parser> { /// serialized format (std::vector, std::set, std::deque, ...), /// but also includes map-like types, when the key is not of type /// std::string. -template +template +requires AreReaderAndWriter struct VectorParser { public: - using InputArrayType = typename ReaderType::InputArrayType; - using InputVarType = typename ReaderType::InputVarType; + using InputArrayType = typename R::InputArrayType; + using InputVarType = typename R::InputVarType; - using OutputArrayType = typename WriterType::OutputArrayType; - using OutputVarType = typename WriterType::OutputVarType; + using OutputArrayType = typename W::OutputArrayType; + using OutputVarType = typename W::OutputVarType; using T = typename VecType::value_type; - static Result read(const ReaderType& _r, - InputVarType* _var) noexcept { + static Result read(const R& _r, InputVarType* _var) noexcept { using namespace std::ranges::views; const auto get_elem = [&](auto& _v) { - return Parser>::read(_r, - &_v) - .value(); + return Parser>::read(_r, &_v).value(); }; const auto get_pair = [&](auto& _v) { if constexpr (is_map_like()) { using K = std::decay_t; using V = std::decay_t; - return Parser>>::read(_r, &_v) + return Parser>>::read(_r, + &_v) .value(); } }; @@ -1111,67 +1092,83 @@ struct VectorParser { return _r.to_array(_var).and_then(to_result); } - static OutputVarType write(const WriterType& _w, - const VecType& _vec) noexcept { + static OutputVarType write(const W& _w, const VecType& _vec) noexcept { auto arr = _w.new_array(); for (const auto& v : _vec) { - _w.add( - Parser>::write(_w, v), - &arr); + _w.add(Parser>::write(_w, v), &arr); } return OutputVarType(arr); } }; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; -template -struct Parser> - : public VectorParser> {}; +template +requires AreReaderAndWriter> +struct Parser> + : public VectorParser> { +}; } // namespace parsing } // namespace rfl