#ifndef RFL_PARSING_VECTORPARSER_HPP_ #define RFL_PARSING_VECTORPARSER_HPP_ #include #include #include #include #include #include "../Result.hpp" #include "../always_false.hpp" #include "MapParser.hpp" #include "Parent.hpp" #include "Parser_base.hpp" #include "is_forward_list.hpp" #include "is_map_like.hpp" #include "is_map_like_not_multimap.hpp" #include "is_set_like.hpp" #include "schema/Type.hpp" namespace rfl { namespace parsing { /// This can be used for data structures that would be expressed as array in /// serialized format (std::vector, std::set, std::deque, ...), /// but also includes map-like types, when the key is not of type /// std::string. template requires AreReaderAndWriter struct VectorParser { public: using InputArrayType = typename R::InputArrayType; using InputVarType = typename R::InputVarType; using OutputArrayType = typename W::OutputArrayType; using OutputVarType = typename W::OutputVarType; using ParentType = Parent; using T = typename VecType::value_type; static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (treat_as_map()) { return MapParser::read(_r, _var); } else { const auto to_res = [&](auto&& _v) { return to_result(_r, std::move(_v)); }; return _r.to_array(_var).and_then(to_res); } } template static void write(const W& _w, const VecType& _vec, const P& _parent) noexcept { if constexpr (treat_as_map()) { MapParser::write(_w, _vec, _parent); } else { auto arr = ParentType::add_array( _w, std::distance(_vec.begin(), _vec.end()), _parent); const auto new_parent = typename ParentType::Array{&arr}; for (const auto& v : _vec) { Parser>::write(_w, v, new_parent); } _w.end_array(&arr); } } /// Generates a schema for the underlying type. static schema::Type to_schema( std::map* _definitions) { using Type = schema::Type; return Type{Type::TypedArray{ .type_ = Ref::make(Parser::to_schema(_definitions))}}; } private: static auto get_elem(const R& _r, auto& _v) { return Parser>::read(_r, _v).value(); }; static auto get_pair(const R& _r, auto& _v) { using K = std::remove_cvref_t; using V = std::remove_cvref_t; return Parser>>::read(_r, _v) .value(); } static Result to_container(const R& _r, InputArrayType&& _arr) { auto input_vars = _r.to_vec(_arr); VecType vec; if constexpr (is_forward_list()) { for (auto it = input_vars.rbegin(); it != input_vars.rend(); ++it) { vec.emplace_front(get_elem(_r, *it)); } } else if constexpr (is_map_like()) { for (auto& v : input_vars) { vec.insert(get_pair(_r, v)); } } else if constexpr (is_set_like()) { for (auto& v : input_vars) { vec.insert(get_elem(_r, v)); } } else { for (auto& v : input_vars) { vec.emplace_back(get_elem(_r, v)); } } return vec; } static Result to_result(const R& _r, InputArrayType&& _arr) { try { return to_container(_r, std::forward(_arr)); } catch (std::exception& e) { return Error(e.what()); } } static constexpr bool treat_as_map() { if constexpr (is_map_like_not_multimap()) { if constexpr (internal::has_reflection_type_v) { using U = std::remove_cvref_t; return std::is_same() || std::is_integral_v || std::is_floating_point_v; // We do not need std::string here, it is already caught by the template // specialization. } else if constexpr (std::is_integral_v || std::is_floating_point_v) { return true; } else { return false; } } else { return false; } } }; } // namespace parsing } // namespace rfl #endif