2024-05-31 22:59:00 -04:00
|
|
|
#ifndef RFL_PARSING_VECTORPARSER_HPP_
|
|
|
|
#define RFL_PARSING_VECTORPARSER_HPP_
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <map>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../always_false.hpp"
|
|
|
|
#include "MapParser.hpp"
|
|
|
|
#include "Parent.hpp"
|
|
|
|
#include "Parser_base.hpp"
|
|
|
|
#include "VectorReader.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 {
|
2024-06-08 14:10:59 -04:00
|
|
|
namespace parsing {
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// 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 <class R, class W, class VecType, class ProcessorsType>
|
|
|
|
requires AreReaderAndWriter<R, W, VecType>
|
|
|
|
struct VectorParser {
|
|
|
|
public:
|
|
|
|
using InputArrayType = typename R::InputArrayType;
|
|
|
|
using InputVarType = typename R::InputVarType;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
using OutputArrayType = typename W::OutputArrayType;
|
|
|
|
using OutputVarType = typename W::OutputVarType;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
using ParentType = Parent<W>;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
using T = typename VecType::value_type;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 15:53:06 -04:00
|
|
|
static Result<VecType>
|
|
|
|
read(const R& _r, const InputVarType& _var) noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if constexpr (treat_as_map()) {
|
|
|
|
return MapParser<R, W, VecType, ProcessorsType>::read(_r, _var);
|
|
|
|
} else if constexpr (is_forward_list<VecType>()) {
|
|
|
|
const auto to_forward_list = [](auto&& vec) -> std::forward_list<T> {
|
|
|
|
std::forward_list<T> list;
|
|
|
|
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
|
|
|
|
list.emplace_front(std::move(*it));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
};
|
|
|
|
return Parser<R, W, std::vector<T>, ProcessorsType>::read(_r, _var)
|
|
|
|
.transform(to_forward_list);
|
|
|
|
} else {
|
2024-06-08 15:53:06 -04:00
|
|
|
const auto parse = [&](const InputArrayType& _arr
|
|
|
|
) -> Result<VecType> {
|
2024-06-08 14:10:59 -04:00
|
|
|
VecType vec;
|
2024-06-08 15:53:06 -04:00
|
|
|
auto vector_reader =
|
2024-06-08 14:10:59 -04:00
|
|
|
VectorReader<R, W, VecType, ProcessorsType>(&_r, &vec);
|
|
|
|
const auto err = _r.read_array(vector_reader, _arr);
|
2024-06-09 18:55:00 -04:00
|
|
|
if (err) {
|
|
|
|
return *err;
|
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
return vec;
|
|
|
|
};
|
|
|
|
return _r.to_array(_var).and_then(parse);
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class P>
|
2024-06-08 15:53:06 -04:00
|
|
|
static void
|
|
|
|
write(const W& _w, const VecType& _vec, const P& _parent) noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if constexpr (treat_as_map()) {
|
|
|
|
MapParser<R, W, VecType, ProcessorsType>::write(_w, _vec, _parent);
|
|
|
|
} else {
|
|
|
|
auto arr = ParentType::add_array(
|
2024-06-08 15:53:06 -04:00
|
|
|
_w, std::distance(_vec.begin(), _vec.end()), _parent
|
|
|
|
);
|
2024-06-08 14:10:59 -04:00
|
|
|
const auto new_parent = typename ParentType::Array {&arr};
|
|
|
|
for (const auto& v : _vec) {
|
|
|
|
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
|
2024-06-08 15:53:06 -04:00
|
|
|
_w, v, new_parent
|
|
|
|
);
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
|
|
|
_w.end_array(&arr);
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Generates a schema for the underlying type.
|
|
|
|
static schema::Type to_schema(
|
2024-06-08 15:53:06 -04:00
|
|
|
std::map<std::string, schema::Type>* _definitions
|
|
|
|
) {
|
2024-06-08 14:10:59 -04:00
|
|
|
using Type = schema::Type;
|
|
|
|
return Type {Type::TypedArray {
|
|
|
|
.type_ = Ref<Type>::make(
|
2024-06-08 15:53:06 -04:00
|
|
|
Parser<R, W, T, ProcessorsType>::to_schema(_definitions)
|
|
|
|
)
|
|
|
|
}};
|
2024-06-08 14:10:59 -04:00
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
private:
|
|
|
|
static constexpr bool treat_as_map() {
|
|
|
|
if constexpr (is_map_like_not_multimap<VecType>()) {
|
|
|
|
if constexpr (internal::has_reflection_type_v<
|
|
|
|
typename T::first_type>) {
|
|
|
|
using U =
|
|
|
|
std::remove_cvref_t<typename T::first_type::ReflectionType>;
|
|
|
|
return std::is_same<U, std::string>() || std::is_integral_v<U> ||
|
|
|
|
std::is_floating_point_v<U>;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
// We do not need std::string here, it is already caught by the
|
|
|
|
// template specialization.
|
|
|
|
} else if constexpr (std::is_integral_v<typename T::first_type> ||
|
|
|
|
std::is_floating_point_v<
|
|
|
|
typename T::first_type>) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
}
|
2024-06-08 14:10:59 -04:00
|
|
|
};
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
} // namespace parsing
|
|
|
|
} // namespace rfl
|
2024-05-31 22:59:00 -04:00
|
|
|
|
|
|
|
#endif
|