diff --git a/include/rfl/internal/VisitTree.hpp b/include/rfl/internal/VisitTree.hpp index 954877a6..8e0ad19b 100644 --- a/include/rfl/internal/VisitTree.hpp +++ b/include/rfl/internal/VisitTree.hpp @@ -1,29 +1,26 @@ #ifndef RFL_INTERNAL_VISITTREE_HPP_ #define RFL_INTERNAL_VISITTREE_HPP_ -namespace rfl { -namespace internal { +namespace rfl::internal { struct VisitTree { - /// Evaluates a visitor pattern using a tree-like structure. - template - static inline auto visit(const auto& _v, const int _i, - const Args&... _args) { - static_assert(_end > _begin, "_end needs to be greater than _begin."); - if constexpr (_end - _begin == 1) { - return _v.template visit<_begin>(_args...); - } else { - constexpr int middle = (_begin + _end) / 2; - if (_i < middle) { - return visit<_begin, middle, Visitor>(_v, _i, _args...); - } else { - return visit(_v, _i, _args...); - } - } + /// Evaluates a visitor pattern using a tree-like structure. + template + static inline auto visit(const auto& _v, const int _i, const Args&... _args) { + static_assert(_end > _begin, "_end needs to be greater than _begin."); + if constexpr (_end - _begin == 1) { + return _v.template visit<_begin>(_args...); + } else { + constexpr int middle = (_begin + _end) / 2; + if (_i < middle) { + return visit<_begin, middle, Visitor>(_v, _i, _args...); + } else { + return visit(_v, _i, _args...); + } } + } }; -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/element_index.hpp b/include/rfl/internal/element_index.hpp index c02b5a29..09085a7b 100644 --- a/include/rfl/internal/element_index.hpp +++ b/include/rfl/internal/element_index.hpp @@ -2,26 +2,37 @@ #define RFL_INTERNAL_ELEMENT_INDEX_HPP_ #include +#include namespace rfl::internal { -template -consteval int find_element_index() { - return -1; -} +template +struct ElementIndexWrapper { + static constexpr int index = _i; -template -consteval int find_element_index() { - if constexpr (std::is_same_v) { - return _i; - } else { - return find_element_index<_i + 1, T, Tail...>(); + consteval auto operator||(auto other) const { + if constexpr (std::is_same_v) { + return *this; + } else { + return other; + } } -} +}; +/** + * Finds the index of the first type T in the list of AlternativeTypes. + * If T is not found, returns -1. + */ template consteval int element_index() { - return find_element_index<0, T, AlternativeTypes...>(); + return [](std::integer_sequence) { + return ((ElementIndexWrapper, + std::remove_cvref_t, + _is>{} || + ...) || + ElementIndexWrapper{}) + .index; + }(std::make_integer_sequence()); } } // namespace rfl::internal diff --git a/include/rfl/internal/get_meta_fields.hpp b/include/rfl/internal/get_meta_fields.hpp index 6468210f..34b898c3 100644 --- a/include/rfl/internal/get_meta_fields.hpp +++ b/include/rfl/internal/get_meta_fields.hpp @@ -2,31 +2,32 @@ #define RFL_INTERNAL_GETMETAFIELDS_HPP_ #include +#include #include #include "../MetaField.hpp" #include "../Tuple.hpp" #include "../type_name_t.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -auto get_meta_fields(AlreadyExtracted&&... _already_extracted) { - constexpr size_t i = sizeof...(_already_extracted); - if constexpr (NamedTupleType::size() == i) { - return std::array{std::move(_already_extracted)...}; - } else { - using FieldType = tuple_element_t; +template +auto get_meta_fields() { + const auto get_one = [&](std::integral_constant) { + using FieldType = tuple_element_t<_i, typename NamedTupleType::Fields>; auto name = typename FieldType::Name().str(); auto type = type_name_t().str(); - return get_meta_fields( - std::move(_already_extracted)..., - MetaField(std::move(name), std::move(type))); - } + return MetaField(std::move(name), std::move(type)); + }; + + constexpr size_t size = NamedTupleType::size(); + + return [&](std::index_sequence<_i...>) { + return std::array{ + get_one(std::integral_constant{})...}; + }(std::make_index_sequence{}); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/has_arrays.hpp b/include/rfl/internal/has_arrays.hpp new file mode 100644 index 00000000..89669205 --- /dev/null +++ b/include/rfl/internal/has_arrays.hpp @@ -0,0 +1,28 @@ +#ifndef RFL_INTERNAL_HASARRAYS_HPP_ +#define RFL_INTERNAL_HASARRAYS_HPP_ + +#include +#include + +#include "../Tuple.hpp" +#include "is_array.hpp" + +namespace rfl::internal { + +template +constexpr bool has_arrays() { + const auto is_true_for_one = + [](std::integral_constant) -> bool { + using T = std::remove_cvref_t>; + return is_array_v; + }; + + return [&](std::integer_sequence) { + return (false || ... || + is_true_for_one(std::integral_constant{})); + }(std::make_integer_sequence>()); +} + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/internal/has_flatten_fields.hpp b/include/rfl/internal/has_flatten_fields.hpp index 42209e7a..869b4153 100644 --- a/include/rfl/internal/has_flatten_fields.hpp +++ b/include/rfl/internal/has_flatten_fields.hpp @@ -7,8 +7,7 @@ #include "../Tuple.hpp" #include "is_flatten_field.hpp" -namespace rfl { -namespace internal { +namespace rfl ::internal { template constexpr bool has_flatten_fields() { @@ -21,11 +20,9 @@ constexpr bool has_flatten_fields() { return [&](std::integer_sequence) { return (false || ... || is_true_for_one(std::integral_constant{})); - } - (std::make_integer_sequence>()); + }(std::make_integer_sequence>()); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index 0905a567..c5edf904 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -1,135 +1,202 @@ #ifndef RFL_MOVE_FROM_TUPLE_HPP_ #define RFL_MOVE_FROM_TUPLE_HPP_ +#include #include #include "../Tuple.hpp" #include "../tuple_cat.hpp" #include "Array.hpp" +#include "has_arrays.hpp" +#include "has_flatten_fields.hpp" #include "is_flatten_field.hpp" -#include "is_named_tuple.hpp" #include "ptr_tuple_t.hpp" #include "tup_to_ptr_tuple.hpp" #include "tuple_t.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template -constexpr int calc_flattened_size() { - if constexpr (_i == rfl::tuple_size_v) { - return 0; +/** + * Calculates the flattened size of a tuple. + * @tparam T The type to calculate the size for. + * @return The flattened size. + */ +template +consteval size_t calc_flattened_size() { + if constexpr (is_flatten_field_v) { + using Tuple = ptr_tuple_t; + return [&](std::integer_sequence) { + return (calc_flattened_size>() + ...); + }(std::make_integer_sequence>()); } else { - using T = std::remove_pointer_t>; - if constexpr (is_flatten_field_v) { - return calc_flattened_size>() + - calc_flattened_size(); - } else { - return 1 + calc_flattened_size(); - } + return 1; } } -// TODO: Non-recursive implementation -template -auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) { - constexpr auto i = sizeof...(Args); +/** + * Helper struct for calculating the indptr array. + * @tparam _head The head of the indptr array. + * @tparam _tail The tail of the indptr array. + */ +template +struct IndptrHelper { + using ArrayType = std::array; + static constexpr auto indptr_ = ArrayType{_head, _tail...}; + + template + consteval auto operator+(const IndptrHelper<_is...>& _other) const { + constexpr auto last = std::get(indptr_); + return IndptrHelper<_head, _tail..., (last + _is)...>{}; + } +}; + +/** + * Calculates the indptr array for a tuple. + * The indptr array is used to map the flattened tuple back to the original + * tuple structure. + * @tparam Tuple The tuple type. + * @tparam _begin The starting index for the indptr. + * @return The indptr array. + */ +template +consteval auto calc_indptr() { + return [&](std::integer_sequence) { + return (IndptrHelper<_begin>{} + ... + + IndptrHelper>()>{}) + .indptr_; + }(std::make_integer_sequence>()); +} +/** + * Unflattens a tuple of pointers into a target tuple. T + * This is meant for when the uses uses rfl::Flatten<...>. + * @param _t Tuple of pointers to elements. + * @return A tuple with unflattened elements. + */ +template +auto unflatten_ptr_tuple(PtrTupleType& _t) { constexpr auto size = rfl::tuple_size_v>; - if constexpr (i == size) { - return rfl::make_tuple(_args...); - } else { - using T = std::remove_cvref_t< - std::remove_pointer_t>>; + constexpr auto indptr = calc_indptr(); - if constexpr (is_flatten_field_v) { - using SubTargetTupleType = - ptr_tuple_t>; + const auto unflatten_one = + [&](std::integral_constant) { + using T = std::remove_cvref_t< + std::remove_pointer_t>>; - constexpr int flattened_size = calc_flattened_size(); + constexpr size_t j = std::get<_i>(indptr); - return unflatten_ptr_tuple( - _t, _args..., - unflatten_ptr_tuple(_t)); + if constexpr (is_flatten_field_v) { + using SubTargetTupleType = + ptr_tuple_t>; - } else { - return unflatten_ptr_tuple( - _t, _args..., std::get<_j>(_t)); - } - } + return unflatten_ptr_tuple(_t); + + } else { + return std::get(_t); + } + }; + + return [&](std::integer_sequence) { + return rfl::make_tuple( + unflatten_one(std::integral_constant{})...); + }(std::make_integer_sequence()); } -template -auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { - constexpr auto i = sizeof...(Args); - if constexpr (i == rfl::tuple_size_v>) { - return std::remove_cvref_t{std::move(_args)...}; - } else { - using FieldType = tuple_element_t>; +/** + * Moves elements from a tuple of pointers to a target type (usually a struct). + * @param _ptrs Tuple of pointers to elements. + * @return A target struct with moved elements. + */ +template +auto move_from_pointers(Pointers&& _ptrs) { + const auto move_one = [&](std::integral_constant) { + using FieldType = tuple_element_t<_i, std::remove_cvref_t>; if constexpr (std::is_pointer_v) { - return move_from_pointers(_ptrs, std::move(_args)..., - std::move(*std::get(_ptrs))); + return std::move(*std::get<_i>(_ptrs)); } else { using PtrTupleType = ptr_tuple_t>; using U = std::remove_cvref_t>::Type>; + tuple_element_t<_i, PtrTupleType>>::Type>; - return move_from_pointers(_ptrs, std::move(_args)..., - move_from_pointers(std::get(_ptrs))); + return move_from_pointers(std::get<_i>(_ptrs)); } - } + }; + return [&](std::integer_sequence) { + return std::remove_cvref_t{ + move_one(std::integral_constant{})...}; + }(std::make_integer_sequence>()); } +/** + * Flattens a single element into a tuple. + * @param _v Source element. + * @return A tuple containing the element. + * + */ template auto flatten_array(T* _v) { return rfl::make_tuple(_v); } +/** + * Flattens a multi-dimensional array into a tuple. + * @param _arr Source array. + * @return A flattened tuple. + */ template auto flatten_array(std::array* _arr) { - const auto fct = [](auto&... _v) { - return rfl::tuple_cat(flatten_array(&_v)...); - }; - return std::apply(fct, *_arr); + return std::apply( + [](auto&... _v) { return rfl::tuple_cat(flatten_array(&_v)...); }, *_arr); } +/** + * Creates a tuple from a single element. + * @param _v Source element. + * @return A tuple containing the element. + */ template auto make_tuple_from_element(T _v) { return rfl::make_tuple(_v); } +/** + * Flattens an array into a tuple. + * @param _arr Source array. + * @return A flattened tuple. + */ template auto make_tuple_from_element(Array* _arr) { return flatten_array(&(_arr->arr_)); } -auto flatten_c_arrays(const auto& _tup) { - const auto fct = [](auto... _v) { - return rfl::tuple_cat(make_tuple_from_element(_v)...); - }; - return rfl::apply(fct, _tup); +/** + * Flattens C-style arrays in a tuple. + * @param _tup Source tuple. + * @return A flattened tuple. + */ +auto flatten_c_arrays(auto&& _tup) { + return rfl::apply( + [](auto... _v) { return rfl::tuple_cat(make_tuple_from_element(_v)...); }, + _tup); } -/// Creates a struct of type T from a tuple by moving the underlying -/// fields. +/** + * Moves elements from a tuple to a target object. + * @param _t Source tuple. + * @return A target object with the elements moved from the tuple. + */ template auto move_from_tuple(TupleType&& _t) { - auto ptr_tuple = tup_to_ptr_tuple(_t); - using TargetTupleType = tuple_t>; - - auto pointers = - flatten_c_arrays(unflatten_ptr_tuple(ptr_tuple)); - - return move_from_pointers(pointers); + auto ptr_tuple = tup_to_ptr_tuple(_t); + return move_from_pointers( + flatten_c_arrays(unflatten_ptr_tuple(ptr_tuple))); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/tup_to_ptr_tuple.hpp b/include/rfl/internal/tup_to_ptr_tuple.hpp index 3e8bfdb1..38765cf7 100644 --- a/include/rfl/internal/tup_to_ptr_tuple.hpp +++ b/include/rfl/internal/tup_to_ptr_tuple.hpp @@ -6,20 +6,16 @@ #include "../apply.hpp" #include "../make_named_tuple.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { /// Generates a named tuple that contains pointers to the original values in /// the struct from a tuple. template auto tup_to_ptr_tuple(TupleType& _t) { - const auto to_ptr = [](auto&... _fields) { - return rfl::make_tuple(&_fields...); - }; - return rfl::apply(to_ptr, _t); + return rfl::apply( + [](auto&... _fields) { return rfl::make_tuple(&_fields...); }, _t); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 9d0fcdde..df127aa7 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -8,6 +8,7 @@ #include "../Result.hpp" #include "../Tuple.hpp" +#include "../internal/always_inline.hpp" #include "../internal/is_array.hpp" #include "AreReaderAndWriter.hpp" #include "Parser_base.hpp" @@ -41,7 +42,9 @@ class TupleReader { std::optional handle_missing_fields() const { std::optional err; if (num_set_ < size_) { - handle_missing_fields_impl(&err); + [&](std::integer_sequence) { + (handle_missing_fields_impl<_is>(&err), ...); + }(std::make_integer_sequence()); } return err; } @@ -60,80 +63,87 @@ class TupleReader { * @return An optional error. */ std::optional read(const InputVarType& _var) const { + if (num_set_ == size_) { + std::stringstream stream; + stream << "Expected " << size_ << " fields, but got at least one more."; + return Error(stream.str()); + } std::optional err; - read_impl(_var, &err); + [&](std::integer_sequence) { + (read_impl<_is>(*this, _var, &err), ...); + if (!err) { + ++num_set_; + } + }(std::make_integer_sequence()); return err; } + /** + * @brief Returns the number of fields in the tuple. + * + * @return The number of fields. + */ + static constexpr size_t size() { return size_; } + private: - template + template void handle_missing_fields_impl(std::optional* _err) const noexcept { - if constexpr (_i < size_) { - if (num_set_ == _i) { - using CurrentType = - std::remove_cvref_t>; - - if constexpr (_all_required || - is_required()) { + if (num_set_ == _i) { + using CurrentType = + std::remove_cvref_t>; + + if constexpr (_all_required || + is_required()) { + *_err = make_nested_error( + static_cast(_i), + "Field " + std::to_string(_i) + " was required, but missing."); + return; + } else if constexpr (std::is_nothrow_default_constructible_v< + CurrentType>) { + ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); + ++num_set_; + } else { + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); + ++num_set_; + } catch (std::exception& e) { *_err = make_nested_error( static_cast(_i), - "Field " + std::to_string(_i) + " was required, but missing."); + "Field " + std::to_string(_i) + + " was missing, and its default value could not be " + "constructed: " + + e.what()); return; - } else if constexpr (std::is_nothrow_default_constructible_v< - CurrentType>) { - ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); - ++num_set_; - } else { - // The default value can throw, for instance a Validator whose - // default value is invalid. - try { - ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); - ++num_set_; - } catch (std::exception& e) { - *_err = make_nested_error( - static_cast(_i), - "Field " + std::to_string(_i) + - " was missing, and its default value could not be " - "constructed: " + - e.what()); - return; - } } } - handle_missing_fields_impl<_i + 1>(_err); } } - template - void read_impl(const InputVarType& _var, std::optional* _err) const { - if constexpr (_i < size_) { - if (num_set_ == _i) { - using CurrentType = - std::remove_cvref_t>; + template + static void read_impl(const TupleReader& _tuple_reader, + const InputVarType& _var, std::optional* _err) { + if (_tuple_reader.num_set_ != _i) { + return; + } + using CurrentType = + std::remove_cvref_t>; - auto res = Parser::read(*r_, _var); + auto res = Parser::read( + *_tuple_reader.r_, _var); - if (res) { - move_to(&(rfl::get<_i>(*tuple_)), &(*res)); - ++num_set_; - } else { - *_err = - wrap_error("Failed to parse field " + std::to_string(_i) + ": ", - _i, std::move(res).error()); - } - return; - } - read_impl<_i + 1>(_var, _err); + if (res) { + move_to(&(rfl::get<_i>(*_tuple_reader.tuple_)), &(*res)); } else { - std::stringstream stream; - stream << "Expected " << size_ << " fields, but got at least one more."; - *_err = Error(stream.str()); - return; + *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", + _i, std::move(res).error()); } + return; } template - void move_to(Target* _t, Source* _s) const { + static void move_to(Target* _t, Source* _s) { if constexpr (std::is_const_v) { return move_to(const_cast*>(_t), _s); } else if constexpr (!internal::is_array_v &&