diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 3f37828b8..1e1d2ac9f 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -10,6 +10,7 @@ #include "../Tuple.hpp" #include "../internal/always_inline.hpp" +#include "../internal/cold.hpp" #include "../internal/is_array.hpp" #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" @@ -19,6 +20,95 @@ namespace rfl::parsing { +/// Placement-moves a parsed field value into uninitialized struct storage. +/// Free function parameterized only by (Target, Source)—never ViewType—so +/// instantiations do not carry the enclosing NamedTuple in mangled symbol +/// names or DWARF debug info. +template +void placement_move_field(Target* _t, Source* _s) { + if constexpr (std::is_const_v) { + placement_move_field(const_cast*>(_t), _s); + } else if constexpr (!internal::is_array_v && + !std::is_array_v) { + ::new (_t) Target(std::move(*_s)); + } else if constexpr (internal::is_array_v) { + static_assert(std::is_array_v, "Expected target to be a c-array."); + for (size_t i = 0; i < _s->arr_.size(); ++i) { + placement_move_field(&((*_t)[i]), &(_s->arr_[i])); + } + } else { + for (size_t i = 0; i < _s->size(); ++i) { + placement_move_field(&((*_t)[i]), &((*_s)[i])); + } + } +} + +template +RFL_COLD void record_uninitialized_field_error(const std::string_view _name, + Error _err, + FieldErrorsType* _errors, + const size_t _reserve_errors) { + // Reserve lazily to avoid error-vector reallocations while keeping + // successful reads allocation-free. + _errors->reserve(_reserve_errors); + _errors->emplace_back(wrap_field_error(_name, std::move(_err))); +} + +/// Reads one field into an uninitialized view slot. Free function parameterized +/// only by (R, W, OriginalType, ProcessorsType)—never the enclosing ViewType—so +/// instantiations do not repeat the full NamedTuple in mangled symbol names or +/// DWARF debug info. +template +RFL_ALWAYS_INLINE void assign_uninitialized_view_field_inline( + const R& _r, const typename R::InputVarType& _var, + const std::string_view _name, OriginalType& _slot, bool& _is_set, + FieldErrors* _errors, const size_t _reserve_errors) { + using T = std::remove_cvref_t>; + auto res = Parser::read(_r, _var); + if (!res) [[unlikely]] { + record_uninitialized_field_error(_name, std::move(res).error(), _errors, + _reserve_errors); + return; + } + if constexpr (std::is_pointer_v) { + placement_move_field(_slot, &(*res)); + } else { + _slot = std::move(*res); + } + _is_set = true; +} + +template +inline void assign_uninitialized_view_field( + const R& _r, const typename R::InputVarType& _var, + const std::string_view _name, OriginalType& _slot, bool& _is_set, + FieldErrors* _errors, const size_t _reserve_errors) { + assign_uninitialized_view_field_inline( + _r, _var, _name, _slot, _is_set, _errors, _reserve_errors); +} + +/// Reads one extra field into an uninitialized ExtraFields slot. +template +void assign_extra_uninitialized_field( + const R& _r, const std::string_view _current_name, + const typename R::InputVarType& _var, ExtraFieldsType* _extra_fields, + bool& _is_found, bool& _is_set, FieldErrors* _errors) { + using T = std::remove_cvref_t< + std::remove_pointer_t>; + if (!_is_set) { + ::new (_extra_fields) ExtraFieldsType(); + _is_set = true; + _is_found = true; + } + auto res = Parser::read(_r, _var); + if (!res) [[unlikely]] { + _errors->emplace_back( + wrap_field_error(_current_name, std::move(res).error())); + return; + } + _extra_fields->emplace(std::string(_current_name), std::move(*res)); +} + template class ViewReader { public: @@ -38,7 +128,8 @@ class ViewReader { * @param _errors The field error collector. */ ViewReader(const R* _r, ViewType* _view, std::array* _found, - std::array* _set, FieldErrors* _errors) + std::array* _set, + FieldErrors* _errors) : r_(_r), view_(_view), found_(_found), set_(_set), errors_(_errors) {} ~ViewReader() = default; @@ -49,10 +140,24 @@ class ViewReader { * @param _name The name of the field. * @param _var The input variable to read from. */ - void read(const std::string_view& _name, const InputVarType& _var) const { + RFL_ALWAYS_INLINE void read(const std::string_view& _name, + const InputVarType& _var) const { if (should_stop_on_field_error(*errors_)) return; - assign_to_matching_field(*r_, _name, _var, view_, errors_, found_, set_, - std::make_integer_sequence()); + if constexpr (size_ == 0) { + static_cast(_var); + if constexpr (internal::no_extra_fields_v) { + handle_no_extra_fields_error(_name, errors_); + } else { + static_cast(_name); + } + } else if constexpr (size_ <= 4) { + assign_to_matching_name_small(*r_, _name, _var, view_, errors_, found_, + set_, + std::make_integer_sequence()); + } else { + assign_to_matching_name(*r_, _name, _var, view_, errors_, found_, set_, + std::make_integer_sequence()); + } } /** @@ -63,7 +168,7 @@ class ViewReader { */ void read(const int _index, const InputVarType& _var) const { if (should_stop_on_field_error(*errors_)) return; - assign_to_matching_field(*r_, _index, _var, view_, errors_, found_, set_, + assign_to_matching_index(*r_, _index, _var, view_, errors_, found_, set_, std::make_integer_sequence()); } @@ -71,8 +176,35 @@ class ViewReader { template void read(std::integral_constant, const InputVarType& _var) const { if (should_stop_on_field_error(*errors_)) return; - assign_to_matching_field(*r_, i, _var, view_, errors_, found_, set_, - std::integer_sequence()); + bool already_assigned = false; + using FieldType = tuple_element_t; + if (!std::get(*found_)) { + std::get(*found_) = true; + already_assigned = true; + assign_uninitialized_view_field_inline( + *r_, _var, FieldType::name(), rfl::get(*view_), std::get(*set_), + errors_, size_); + } + + if constexpr (ViewType::pos_extra_fields() != -1) { + static_assert(!schemaful::IsSchemafulReader, + "rfl::ExtraFields are not supported for schemaful formats, " + "because schemaful formats cannot have extra fields."); + static_assert(ViewType::pos_extra_fields() == -1, + "rfl::ExtraFields are not supported for positional reads."); + } else if constexpr (internal::no_extra_fields_v) { + static_assert( + !schemaful::IsSchemafulReader, + "Passing rfl::NoExtraFields to a schemaful format does not make " + "sense, because schemaful formats cannot have extra fields."); + if (!already_assigned) { + std::stringstream stream; + stream << "Value named '" << i << "' not used."; + errors_->emplace_back( + make_nested_error(static_cast(i), stream.str())); + } + } } /** @@ -83,107 +215,173 @@ class ViewReader { static constexpr size_t size() { return size_; } private: - template - static bool is_matching(const int _current_index) { - return _current_index == i; - } - - template - static bool is_matching(const std::string_view& _current_name) { - return _current_name == FieldType::name(); + template + static RFL_ALWAYS_INLINE void assign_by_index_small( + const R& _r, const int _idx, const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _set, + std::integer_sequence) { + using Fields = typename ViewType::Fields; + static_cast( + ((_idx == is + ? (assign_uninitialized_view_field_inline< + R, W, typename tuple_element_t::Type, + ProcessorsType>(_r, _var, + tuple_element_t::name(), + rfl::get(*_view), std::get(*_set), + _errors, size_), + true) + : false) || + ...)); } - static ErrorDetail::PathSegment to_path_segment(const int _current_index) { - return static_cast(_current_index); + template + static void assign_by_index(const R& _r, const int _idx, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, + std::array* _set, + std::integer_sequence) { + using Fields = typename ViewType::Fields; + if constexpr (size_ <= 4) { + assign_by_index_small(_r, _idx, _var, _view, _errors, _set, + std::integer_sequence()); + } else { + static_cast( + ((_idx == is ? (assign_uninitialized_view_field< + R, W, typename tuple_element_t::Type, + ProcessorsType>( + _r, _var, tuple_element_t::name(), + rfl::get(*_view), std::get(*_set), + _errors, size_), + true) + : false) || + ...)); + } } - static ErrorDetail::PathSegment to_path_segment( - const std::string_view& _current_name) { - return std::string(_current_name); + template + static RFL_ALWAYS_INLINE void assign_to_matching_name_small( + const R& _r, const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _found, + std::array* _set, std::integer_sequence) { + using Fields = typename ViewType::Fields; + const bool matched = + (((is != ViewType::pos_extra_fields() && + _current_name == tuple_element_t::name()) + ? (!std::get(*_found) + ? (std::get(*_found) = true, + assign_uninitialized_view_field_inline< + R, W, typename tuple_element_t::Type, + ProcessorsType>( + _r, _var, tuple_element_t::name(), + rfl::get(*_view), std::get(*_set), _errors, + size_), + true) + : false) + : false) || + ...); + if constexpr (ViewType::pos_extra_fields() != -1) { + if (!matched) { + handle_extra_field_name(_r, _current_name, _var, _view, _errors, _found, + _set); + } + } else if constexpr (internal::no_extra_fields_v) { + if (!matched) { + handle_no_extra_fields_error(_current_name, _errors); + } + } else { + static_cast(matched); + } } - template - requires(i == ViewType::pos_extra_fields()) - static RFL_ALWAYS_INLINE void assign_if_field_matches( - const R&, const auto, const auto&, auto*, auto*, auto*, auto*, bool*) {} - - template - requires(i != ViewType::pos_extra_fields()) - static RFL_ALWAYS_INLINE void assign_if_field_matches( - const R& _r, const auto _current_name_or_index, const auto& _var, - auto* _view, auto* _errors, auto* _found, auto* _set, - bool* _already_assigned) { - using FieldType = tuple_element_t; - using OriginalType = typename FieldType::Type; - using T = - std::remove_cvref_t>; - if (!(*_already_assigned) && !std::get(*_found) && - is_matching(_current_name_or_index)) { - std::get(*_found) = true; - *_already_assigned = true; - auto res = Parser::read(_r, _var); - if (!res) [[unlikely]] { - constexpr auto name = FieldType::name(); - // Reserve lazily to avoid error-vector reallocations while keeping - // successful reads allocation-free. - _errors->reserve(size_); - _errors->emplace_back(wrap_field_error(name, std::move(res).error())); - return; + template + static void assign_to_matching_name( + const R& _r, const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _found, + std::array* _set, std::integer_sequence) { + using Fields = typename ViewType::Fields; + const bool matched = + (((is != ViewType::pos_extra_fields() && + _current_name == tuple_element_t::name()) + ? (!std::get(*_found) + ? (std::get(*_found) = true, + assign_uninitialized_view_field< + R, W, typename tuple_element_t::Type, + ProcessorsType>( + _r, _var, tuple_element_t::name(), + rfl::get(*_view), std::get(*_set), _errors, + size_), + true) + : false) + : false) || + ...); + if constexpr (ViewType::pos_extra_fields() != -1) { + if (!matched) { + handle_extra_field_name(_r, _current_name, _var, _view, _errors, _found, + _set); } - if constexpr (std::is_pointer_v) { - move_to(rfl::get(*_view), &(*res)); - } else { - rfl::get(*_view) = std::move(*res); + } else if constexpr (internal::no_extra_fields_v) { + if (!matched) { + handle_no_extra_fields_error(_current_name, _errors); } - std::get(*_set) = true; + } else { + static_cast(matched); } } - template - static void assign_to_extra_fields(const R& _r, - const std::string_view& _current_name, - const auto& _var, auto* _view, - auto* _errors, auto* _found, auto* _set) { - auto* extra_fields = _view->template get<_pos>(); + static void handle_extra_field_name(const R& _r, + const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, + std::array* _found, + std::array* _set) { + static_assert(!schemaful::IsSchemafulReader, + "rfl::ExtraFields are not supported for schemaful formats, " + "because schemaful formats cannot have extra fields."); + constexpr int pos = ViewType::pos_extra_fields(); + auto* extra_fields = _view->template get(); using ExtraFieldsType = std::remove_cvref_t>; - using T = std::remove_cvref_t< - std::remove_pointer_t>; - if (!std::get<_pos>(*_set)) { - ::new (extra_fields) ExtraFieldsType(); - std::get<_pos>(*_set) = true; - std::get<_pos>(*_found) = true; - } - auto res = Parser::read(_r, _var); - if (!res) [[unlikely]] { - _errors->emplace_back( - wrap_field_error(_current_name, std::move(res).error())); - return; - } - extra_fields->emplace(std::string(_current_name), std::move(*res)); + assign_extra_uninitialized_field( + _r, _current_name, _var, extra_fields, std::get(*_found), + std::get(*_set), _errors); + } + + static RFL_COLD void handle_no_extra_fields_error( + const std::string_view _current_name, + FieldErrors* _errors) { + static_assert( + !schemaful::IsSchemafulReader, + "Passing rfl::NoExtraFields to a schemaful format does not make " + "sense, because schemaful formats cannot have extra fields."); + std::stringstream stream; + stream << "Value named '" << _current_name << "' not used."; + _errors->emplace_back( + make_nested_error(std::string(_current_name), stream.str())); } template - static void assign_to_matching_field(const R& _r, - const auto _current_name_or_index, - const auto& _var, auto* _view, - auto* _errors, auto* _found, auto* _set, - std::integer_sequence) { + static void assign_to_matching_index( + const R& _r, const int _index, const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _found, + std::array* _set, std::integer_sequence _seq) { bool already_assigned = false; - - (assign_if_field_matches(_r, _current_name_or_index, _var, _view, - _errors, _found, _set, &already_assigned), - ...); + if constexpr (size_ > 0) { + if (_index >= 0 && static_cast(_index) < size_ && + !(*_found)[static_cast(_index)]) { + (*_found)[static_cast(_index)] = true; + already_assigned = true; + assign_by_index(_r, _index, _var, _view, _errors, _set, _seq); + } + } if constexpr (ViewType::pos_extra_fields() != -1) { static_assert(!schemaful::IsSchemafulReader, "rfl::ExtraFields are not supported for schemaful formats, " "because schemaful formats cannot have extra fields."); - constexpr int pos = ViewType::pos_extra_fields(); - if (!already_assigned) { - assign_to_extra_fields(_r, _current_name_or_index, _var, _view, - _errors, _found, _set); - } + static_assert(ViewType::pos_extra_fields() == -1, + "rfl::ExtraFields are not supported for positional reads."); } else if constexpr (internal::no_extra_fields_v) { static_assert( !schemaful::IsSchemafulReader, @@ -191,29 +389,9 @@ class ViewReader { "sense, because schemaful formats cannot have extra fields."); if (!already_assigned) { std::stringstream stream; - stream << "Value named '" << _current_name_or_index << "' not used."; - _errors->emplace_back(make_nested_error( - to_path_segment(_current_name_or_index), stream.str())); - } - } - } - - template - static void move_to(Target* _t, Source* _s) { - if constexpr (std::is_const_v) { - return move_to(const_cast*>(_t), _s); - } else if constexpr (!rfl::internal::is_array_v && - !std::is_array_v) { - ::new (_t) Target(std::move(*_s)); - } else if constexpr (rfl::internal::is_array_v) { - static_assert(std::is_array_v, - "Expected target to be a c-array."); - for (size_t i = 0; i < _s->arr_.size(); ++i) { - move_to(&((*_t)[i]), &(_s->arr_[i])); - } - } else { - for (size_t i = 0; i < _s->size(); ++i) { - move_to(&((*_t)[i]), &((*_s)[i])); + stream << "Value named '" << _index << "' not used."; + _errors->emplace_back( + make_nested_error(static_cast(_index), stream.str())); } } } diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index 593a72bde..1df6b30ec 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -4,12 +4,15 @@ #include #include #include +#include #include #include #include #include "../Ref.hpp" #include "../Tuple.hpp" +#include "../internal/always_inline.hpp" +#include "../internal/cold.hpp" #include "../internal/is_array.hpp" #include "../internal/no_extra_fields_v.hpp" #include "FieldErrors.hpp" @@ -18,6 +21,91 @@ namespace rfl::parsing { +/// Move-assigns a parsed field value into an already-constructed struct field. +/// Free function parameterized only by (Target, Source)—never ViewType—so +/// instantiations do not carry the enclosing NamedTuple in mangled symbol +/// names or DWARF debug info. +template +void move_assign_field(Target* _t, Source* _s) { + if constexpr (!internal::is_array_v && !std::is_array_v) { + if constexpr (std::is_const_v) { + // The struct was default-constructed, so *_t is a live const object + // and must not be assigned to. Re-create it in place instead. If the + // constructor throws, we are inside a noexcept function, so it cannot + // be destroyed twice. + using NonConstTarget = std::remove_const_t; + auto* const ptr = const_cast(_t); + std::destroy_at(ptr); + ::new (ptr) NonConstTarget(std::move(*_s)); + } else { + *_t = Target(std::move(*_s)); + } + } else if constexpr (internal::is_array_v) { + static_assert(std::is_array_v, "Expected target to be a c-array."); + for (size_t i = 0; i < _s->arr_.size(); ++i) { + move_assign_field(&((*_t)[i]), &(_s->arr_[i])); + } + } else { + for (size_t i = 0; i < _s->size(); ++i) { + move_assign_field(&((*_t)[i]), &((*_s)[i])); + } + } +} + +template +RFL_COLD void record_default_field_error(const std::string_view _name, + Error _err, FieldErrorsType* _errors) { + _errors->emplace_back(wrap_field_error(_name, std::move(_err))); +} + +/// Reads one field into an already-constructed view slot. Free function +/// parameterized only by (R, W, OriginalType, ProcessorsType)—never the +/// enclosing ViewType—so instantiations do not repeat the full NamedTuple in +/// mangled symbol names or DWARF debug info. +template +RFL_ALWAYS_INLINE void assign_default_view_field_inline( + const R& _r, const typename R::InputVarType& _var, + const std::string_view _name, OriginalType& _slot, + FieldErrors* _errors) { + using T = std::remove_cvref_t>; + auto res = Parser::read(_r, _var); + if (!res) [[unlikely]] { + record_default_field_error(_name, std::move(res).error(), _errors); + return; + } + if constexpr (std::is_pointer_v) { + move_assign_field(_slot, &(*res)); + } else { + _slot = std::move(*res); + } +} + +template +inline void assign_default_view_field( + const R& _r, const typename R::InputVarType& _var, + const std::string_view _name, OriginalType& _slot, + FieldErrors* _errors) { + assign_default_view_field_inline( + _r, _var, _name, _slot, _errors); +} + +/// Reads one extra field into an already-constructed ExtraFields slot. +template +void assign_extra_default_field( + const R& _r, const std::string_view _current_name, + const typename R::InputVarType& _var, ExtraFieldsType* _extra_fields, + FieldErrors* _errors) { + using T = std::remove_cvref_t< + std::remove_pointer_t>; + auto res = Parser::read(_r, _var); + if (!res) [[unlikely]] { + _errors->emplace_back( + wrap_field_error(_current_name, std::move(res).error())); + return; + } + _extra_fields->emplace(std::string(_current_name), std::move(*res)); +} + template class ViewReaderWithDefault { public: @@ -54,10 +142,24 @@ class ViewReaderWithDefault { * @param _name The name of the field. * @param _var The input variable to read from. */ - void read(const std::string_view& _name, const InputVarType& _var) const { + RFL_ALWAYS_INLINE void read(const std::string_view& _name, + const InputVarType& _var) const { if (should_stop_on_field_error(*errors_)) return; - assign_to_matching_field(*r_, _name, _var, view_, errors_, found_.get(), - std::make_integer_sequence()); + if constexpr (size_ == 0) { + static_cast(_var); + if constexpr (internal::no_extra_fields_v) { + handle_no_extra_fields_error(_name, errors_); + } else { + static_cast(_name); + } + } else if constexpr (size_ <= 4) { + assign_to_matching_field_small(*r_, _name, _var, view_, errors_, + found_.get(), + std::make_integer_sequence()); + } else { + assign_to_matching_field(*r_, _name, _var, view_, errors_, found_.get(), + std::make_integer_sequence()); + } } /** @@ -68,116 +170,95 @@ class ViewReaderWithDefault { static constexpr size_t size() { return size_; } private: - template - requires(i == ViewType::pos_extra_fields()) - static void assign_if_field_matches(const R&, const std::string_view&, - const auto&, auto*, auto*, auto*, - bool*) {} - - template - requires(i != ViewType::pos_extra_fields()) - static void assign_if_field_matches(const R& _r, - const std::string_view& _current_name, - const auto& _var, auto* _view, - auto* _errors, auto* _found, - bool* _already_assigned) { - using FieldType = tuple_element_t; - using OriginalType = typename FieldType::Type; - using T = - std::remove_cvref_t>; - constexpr auto name = FieldType::name(); - if (!(*_already_assigned) && !std::get(*_found) && - _current_name == name) { - std::get(*_found) = true; - *_already_assigned = true; - auto res = Parser::read(_r, _var); - if (!res) { - _errors->emplace_back(wrap_field_error(name, std::move(res).error())); - return; + template + static RFL_ALWAYS_INLINE void assign_to_matching_field_small( + const R& _r, const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _found, + std::integer_sequence) { + using Fields = typename ViewType::Fields; + const bool matched = + (((is != ViewType::pos_extra_fields() && + _current_name == tuple_element_t::name()) + ? (!std::get(*_found) + ? (std::get(*_found) = true, + assign_default_view_field_inline< + R, W, typename tuple_element_t::Type, + ProcessorsType>( + _r, _var, tuple_element_t::name(), + rfl::get(*_view), _errors), + true) + : false) + : false) || + ...); + if constexpr (ViewType::pos_extra_fields() != -1) { + if (!matched) { + handle_extra_field(_r, _current_name, _var, _view, _errors); } - if constexpr (std::is_pointer_v) { - move_to(rfl::get(*_view), &(*res)); - } else { - rfl::get(*_view) = std::move(*res); + } else if constexpr (internal::no_extra_fields_v) { + if (!matched) { + handle_no_extra_fields_error(_current_name, _errors); } + } else { + static_cast(matched); } } - template - static void assign_to_extra_fields(const R& _r, - const std::string_view& _current_name, - const auto& _var, auto* _view, - auto* _errors) { - auto* extra_fields = _view->template get<_pos>(); - using ExtraFieldsType = - std::remove_cvref_t>; - using T = std::remove_cvref_t< - std::remove_pointer_t>; - auto res = Parser::read(_r, _var); - if (!res) { - _errors->emplace_back( - wrap_field_error(_current_name, std::move(res).error())); - return; - } - extra_fields->emplace(std::string(_current_name), std::move(*res)); - } - template - static void assign_to_matching_field(const R& _r, - const std::string_view& _current_name, - const auto& _var, auto* _view, - auto* _errors, auto* _found, - std::integer_sequence) { - bool already_assigned = false; - - (assign_if_field_matches(_r, _current_name, _var, _view, _errors, - _found, &already_assigned), - ...); - + static void assign_to_matching_field( + const R& _r, const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, std::array* _found, + std::integer_sequence) { + using Fields = typename ViewType::Fields; + const bool matched = + (((is != ViewType::pos_extra_fields() && + _current_name == tuple_element_t::name()) + ? (!std::get(*_found) + ? (std::get(*_found) = true, + assign_default_view_field< + R, W, typename tuple_element_t::Type, + ProcessorsType>( + _r, _var, tuple_element_t::name(), + rfl::get(*_view), _errors), + true) + : false) + : false) || + ...); if constexpr (ViewType::pos_extra_fields() != -1) { - constexpr int pos = ViewType::pos_extra_fields(); - if (!already_assigned) { - assign_to_extra_fields(_r, _current_name, _var, _view, _errors); + if (!matched) { + handle_extra_field(_r, _current_name, _var, _view, _errors); } } else if constexpr (internal::no_extra_fields_v) { - if (!already_assigned) { - std::stringstream stream; - stream << "Value named '" << std::string(_current_name) - << "' not used. Remove the rfl::NoExtraFields processor or add " - "rfl::ExtraFields to avoid this error message."; - _errors->emplace_back( - make_nested_error(std::string(_current_name), stream.str())); + if (!matched) { + handle_no_extra_fields_error(_current_name, _errors); } + } else { + static_cast(matched); } } - template - static void move_to(Target* _t, Source* _s) { - if constexpr (!rfl::internal::is_array_v && - !std::is_array_v) { - if constexpr (std::is_const_v) { - // The struct was default-constructed, so *_t is a live const object - // and must not be assigned to. Re-create it in place instead. If the - // constructor throws, we are inside a noexcept function, so it cannot - // be destroyed twice. - using NonConstTarget = std::remove_const_t; - auto* const ptr = const_cast(_t); - std::destroy_at(ptr); - ::new (ptr) NonConstTarget(std::move(*_s)); - } else { - *_t = Target(std::move(*_s)); - } - } else if constexpr (rfl::internal::is_array_v) { - static_assert(std::is_array_v, - "Expected target to be a c-array."); - for (size_t i = 0; i < _s->arr_.size(); ++i) { - move_to(&((*_t)[i]), &(_s->arr_[i])); - } - } else { - for (size_t i = 0; i < _s->size(); ++i) { - move_to(&((*_t)[i]), &((*_s)[i])); - } - } + static void handle_extra_field(const R& _r, + const std::string_view _current_name, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors) { + constexpr int pos = ViewType::pos_extra_fields(); + auto* extra_fields = _view->template get(); + using ExtraFieldsType = + std::remove_cvref_t>; + assign_extra_default_field( + _r, _current_name, _var, extra_fields, _errors); + } + + static RFL_COLD void handle_no_extra_fields_error( + const std::string_view _current_name, + FieldErrors* _errors) { + std::stringstream stream; + stream << "Value named '" << std::string(_current_name) + << "' not used. Remove the rfl::NoExtraFields processor or add " + "rfl::ExtraFields to avoid this error message."; + _errors->emplace_back( + make_nested_error(std::string(_current_name), stream.str())); } private: