Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 25 additions & 39 deletions include/rfl/parsing/TupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <cstddef>
#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "../Result.hpp"
#include "../Tuple.hpp"
Expand Down Expand Up @@ -97,8 +99,13 @@ struct TupleParser {
} else {
constexpr auto size = rfl::tuple_size_v<TupleType>;
auto arr = ParentType::add_array(_w, size, _parent);
const auto new_parent = typename ParentType::Array{&arr};
to_array(_w, _tup, new_parent, std::make_integer_sequence<int, size>());
if constexpr (size > 0) {
const auto new_parent = typename ParentType::Array{&arr};
to_array(_w, _tup, new_parent,
std::make_integer_sequence<int, static_cast<int>(size)>());
} else {
static_cast<void>(_tup);
}
_w.end_array(&arr);
}
}
Expand All @@ -111,28 +118,19 @@ struct TupleParser {
*/
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
constexpr auto size = rfl::tuple_size_v<TupleType>;
std::vector<schema::Type> types;
build_schema(
_definitions, &types,
std::make_integer_sequence<int, rfl::tuple_size_v<TupleType>>());
if constexpr (size > 0) {
types.reserve(size);
build_schema(_definitions, &types,
std::make_integer_sequence<int, static_cast<int>(size)>());
} else {
static_cast<void>(_definitions);
}
return schema::Type{schema::Type::Tuple{.types_ = std::move(types)}};
}

private:
/**
* @brief Adds an element of the tuple to the schema.
*
* @tparam _i The index of the element.
* @param _definitions The map of definitions to add to.
* @param _types The vector of types to add the generated schema to.
*/
template <size_t _i>
static void add_to_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types) noexcept {
using U = std::remove_cvref_t<rfl::tuple_element_t<_i, TupleType>>;
_types->push_back(Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
}

/**
* @brief Builds the schema for the tuple.
*
Expand All @@ -145,25 +143,11 @@ struct TupleParser {
static void build_schema(std::map<std::string, schema::Type>* _definitions,
std::vector<schema::Type>* _types,
std::integer_sequence<int, _is...>) noexcept {
(add_to_schema<_is>(_definitions, _types), ...);
}

/**
* @brief Adds an element of the tuple to the array.
*
* @tparam _i The index of the element.
* @tparam P The type of the parent.
* @param _w The writer to use.
* @param _tup The tuple to write.
* @param _parent The parent object.
*/
template <int _i, class P>
static void add_to_array(const W& _w, const TupleType& _tup,
const P& _parent) {
using NewFieldType =
std::remove_cvref_t<rfl::tuple_element_t<_i, TupleType>>;
Parser<R, W, NewFieldType, ProcessorsType>::write(_w, rfl::get<_i>(_tup),
_parent);
(_types->push_back(
Parser<R, W,
std::remove_cvref_t<rfl::tuple_element_t<_is, TupleType>>,
ProcessorsType>::to_schema(_definitions)),
...);
}

/**
Expand All @@ -179,7 +163,9 @@ struct TupleParser {
template <int... _is, class P>
static void to_array(const W& _w, const TupleType& _tup, const P& _parent,
std::integer_sequence<int, _is...>) {
(add_to_array<_is>(_w, _tup, _parent), ...);
(Parser<R, W, std::remove_cvref_t<rfl::tuple_element_t<_is, TupleType>>,
ProcessorsType>::write(_w, rfl::get<_is>(_tup), _parent),
...);
}
};

Expand Down
Loading
Loading