diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 0e3655dff..1d649de9f 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -3,8 +3,10 @@ #include #include +#include #include #include +#include #include "../Result.hpp" #include "../Tuple.hpp" @@ -97,8 +99,13 @@ struct TupleParser { } else { constexpr auto size = rfl::tuple_size_v; 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()); + if constexpr (size > 0) { + const auto new_parent = typename ParentType::Array{&arr}; + to_array(_w, _tup, new_parent, + std::make_integer_sequence(size)>()); + } else { + static_cast(_tup); + } _w.end_array(&arr); } } @@ -111,28 +118,19 @@ struct TupleParser { */ static schema::Type to_schema( std::map* _definitions) { + constexpr auto size = rfl::tuple_size_v; std::vector types; - build_schema( - _definitions, &types, - std::make_integer_sequence>()); + if constexpr (size > 0) { + types.reserve(size); + build_schema(_definitions, &types, + std::make_integer_sequence(size)>()); + } else { + static_cast(_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 - static void add_to_schema(std::map* _definitions, - std::vector* _types) noexcept { - using U = std::remove_cvref_t>; - _types->push_back(Parser::to_schema(_definitions)); - } - /** * @brief Builds the schema for the tuple. * @@ -145,25 +143,11 @@ struct TupleParser { static void build_schema(std::map* _definitions, std::vector* _types, std::integer_sequence) 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 - static void add_to_array(const W& _w, const TupleType& _tup, - const P& _parent) { - using NewFieldType = - std::remove_cvref_t>; - Parser::write(_w, rfl::get<_i>(_tup), - _parent); + (_types->push_back( + Parser>, + ProcessorsType>::to_schema(_definitions)), + ...); } /** @@ -179,7 +163,9 @@ struct TupleParser { template static void to_array(const W& _w, const TupleType& _tup, const P& _parent, std::integer_sequence) { - (add_to_array<_is>(_w, _tup, _parent), ...); + (Parser>, + ProcessorsType>::write(_w, rfl::get<_is>(_tup), _parent), + ...); } }; diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index df127aa70..095c6b82d 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -1,14 +1,18 @@ #ifndef RFL_PARSING_TUPLEREADER_HPP_ #define RFL_PARSING_TUPLEREADER_HPP_ +#include #include -#include +#include +#include #include #include +#include #include "../Result.hpp" #include "../Tuple.hpp" #include "../internal/always_inline.hpp" +#include "../internal/cold.hpp" #include "../internal/is_array.hpp" #include "AreReaderAndWriter.hpp" #include "Parser_base.hpp" @@ -17,6 +21,113 @@ namespace rfl::parsing { +/// Placement-moves a parsed tuple element into uninitialized tuple storage. +/// Free function parameterized only by (Target, Source)—never TupleType—so +/// instantiations do not carry the enclosing tuple in mangled symbol names or +/// DWARF debug info. +template +void placement_move_tuple_element(Target* _t, Source* _s) { + if constexpr (std::is_const_v) { + placement_move_tuple_element(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) { + for (size_t i = 0; i < _s->arr_.size(); ++i) { + placement_move_tuple_element(&((*_t)[i]), &(_s->arr_[i])); + } + } else { + for (size_t i = 0; i < _s->size(); ++i) { + placement_move_tuple_element(&((*_t)[i]), &((*_s)[i])); + } + } +} + +RFL_COLD inline Error make_tuple_element_parse_error(const size_t _index, + Error&& _err) { + return wrap_error("Failed to parse field " + std::to_string(_index) + ": ", + _index, std::move(_err)); +} + +RFL_COLD inline Error make_tuple_too_many_fields_error(const size_t _size) { + return Error("Expected " + std::to_string(_size) + + " fields, but got at least one more."); +} + +/// Reads one element into an uninitialized tuple slot. Free function +/// parameterized only by (R, W, Target, ProcessorsType)—never the enclosing +/// TupleType—so instantiations do not repeat the full tuple in mangled symbol +/// names or DWARF debug info. +template +RFL_ALWAYS_INLINE void read_tuple_element_inline( + const R& _r, const typename R::InputVarType& _var, const size_t _index, + Target* _slot, std::optional* _err) { + using CurrentType = std::remove_cvref_t; + auto res = Parser::read(_r, _var); + if (res) [[likely]] { + placement_move_tuple_element(_slot, &(*res)); + } else { + *_err = make_tuple_element_parse_error(_index, std::move(res).error()); + } +} + +template +inline void read_tuple_element(const R& _r, + const typename R::InputVarType& _var, + const size_t _index, Target* _slot, + std::optional* _err) { + read_tuple_element_inline(_r, _var, _index, + _slot, _err); +} + +/// Handles one missing element of a tuple, default-constructing it when +/// optional or recording a missing-field error when required. Returns true if +/// an error was recorded and iteration should stop. +template +inline bool handle_missing_tuple_element(const size_t _index, Target* _slot, + size_t& _num_set, + std::optional* _err) noexcept { + if (_num_set != _index) { + return false; + } + using CurrentType = std::remove_cvref_t; + if constexpr (_all_required || + is_required()) { + *_err = make_nested_error( + _index, + "Field " + std::to_string(_index) + " was required, but missing."); + return true; + } else if constexpr (std::is_nothrow_default_constructible_v) { + ::new (const_cast(_slot)) CurrentType(); + ++_num_set; + return false; + } else { + // The default value can throw, for instance a Validator whose + // default value is invalid. + try { + ::new (const_cast(_slot)) CurrentType(); + ++_num_set; + return false; + } catch (const std::exception& e) { + *_err = make_nested_error( + _index, + "Field " + std::to_string(_index) + + " was missing, and its default value could not be " + "constructed: " + + e.what()); + return true; + } catch (...) { + *_err = make_nested_error( + _index, + "Field " + std::to_string(_index) + + " was missing, and its default value could not be " + "constructed: unknown exception."); + return true; + } + } +} + template class TupleReader { @@ -41,10 +152,12 @@ class TupleReader { */ std::optional handle_missing_fields() const { std::optional err; - if (num_set_ < size_) { - [&](std::integer_sequence) { - (handle_missing_fields_impl<_is>(&err), ...); - }(std::make_integer_sequence()); + if constexpr (size_ > 0) { + if (num_set_ < size_) [[unlikely]] { + handle_missing_fields_impl( + tuple_, num_set_, &err, + std::make_integer_sequence(size_)>()); + } } return err; } @@ -63,19 +176,22 @@ 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()); + if (num_set_ == size_) [[unlikely]] { + return make_tuple_too_many_fields_error(size_); } - std::optional err; - [&](std::integer_sequence) { - (read_impl<_is>(*this, _var, &err), ...); - if (!err) { + if constexpr (size_ == 0) { + static_cast(_var); + return std::nullopt; + } else { + std::optional err; + assign_by_index( + *r_, static_cast(num_set_), _var, tuple_, &err, + std::make_integer_sequence(size_)>()); + if (!err) [[likely]] { ++num_set_; } - }(std::make_integer_sequence()); - return err; + return err; + } } /** @@ -86,78 +202,47 @@ class TupleReader { static constexpr size_t size() { return size_; } private: - template - void handle_missing_fields_impl(std::optional* _err) const noexcept { - 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 missing, and its default value could not be " - "constructed: " + - e.what()); - return; - } - } - } - } - - 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( - *_tuple_reader.r_, _var); - - if (res) { - move_to(&(rfl::get<_i>(*_tuple_reader.tuple_)), &(*res)); + template + static void assign_by_index(const R& _r, const int _idx, + const InputVarType& _var, TupleType* _tuple, + std::optional* _err, + std::integer_sequence) { + if constexpr (size_ <= 4) { + static_cast( + ((_idx == _is + ? (read_tuple_element_inline< + R, W, + std::remove_reference_t< + rfl::tuple_element_t<_is, TupleType>>, + ProcessorsType>(_r, _var, static_cast(_is), + &(rfl::get<_is>(*_tuple)), _err), + true) + : false) || + ...)); } else { - *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", - _i, std::move(res).error()); + static_cast( + ((_idx == _is + ? (read_tuple_element< + R, W, + std::remove_reference_t< + rfl::tuple_element_t<_is, TupleType>>, + ProcessorsType>(_r, _var, static_cast(_is), + &(rfl::get<_is>(*_tuple)), _err), + true) + : false) || + ...)); } - return; } - 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 (!internal::is_array_v && - !std::is_array_v) { - ::new (_t) Target(std::move(*_s)); - } else if constexpr (internal::is_array_v) { - 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])); - } - } + template + static RFL_COLD void handle_missing_fields_impl( + TupleType* _tuple, size_t& _num_set, std::optional* _err, + std::integer_sequence) noexcept { + static_cast( + (handle_missing_tuple_element<_ignore_empty_containers, _all_required>( + static_cast(_is), &(rfl::get<_is>(*_tuple)), _num_set, + _err) || + ...)); } private: diff --git a/include/rfl/parsing/VectorParser.hpp b/include/rfl/parsing/VectorParser.hpp index 2be9ee2e7..7c27e4e62 100644 --- a/include/rfl/parsing/VectorParser.hpp +++ b/include/rfl/parsing/VectorParser.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_VECTORPARSER_HPP_ #define RFL_PARSING_VECTORPARSER_HPP_ +#include #include #include #include @@ -91,8 +92,13 @@ struct VectorParser { if constexpr (treat_as_map()) { MapParser::write(_w, _vec, _parent); } else { - auto arr = ParentType::add_array( - _w, std::distance(_vec.begin(), _vec.end()), _parent); + size_t size = 0; + if constexpr (requires { static_cast(_vec.size()); }) { + size = static_cast(_vec.size()); + } else { + size = static_cast(std::distance(_vec.begin(), _vec.end())); + } + auto arr = ParentType::add_array(_w, size, _parent); const auto new_parent = typename ParentType::Array{&arr}; for (const auto& v : _vec) { Parser, ProcessorsType>::write(_w, v, diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index 450125c6a..9bfb01d83 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -9,7 +9,6 @@ #include #include -#include "../Ref.hpp" #include "../Tuple.hpp" #include "../internal/always_inline.hpp" #include "../internal/cold.hpp" @@ -124,9 +123,10 @@ class ViewReaderWithDefault { */ ViewReaderWithDefault(const R* _r, ViewType* _view, FieldErrors* _errors) - : r_(_r), view_(_view), errors_(_errors) { - found_->fill(false); - } + : r_(_r), view_(_view), errors_(_errors) {} + + ViewReaderWithDefault(const ViewReaderWithDefault&) = delete; + ViewReaderWithDefault& operator=(const ViewReaderWithDefault&) = delete; ~ViewReaderWithDefault() = default; @@ -135,7 +135,7 @@ class ViewReaderWithDefault { * * @return The boolean array. */ - const std::array& found() const { return *found_; } + const std::array& found() const { return found_; } /** * @brief Reads a single field into the view. @@ -154,11 +154,10 @@ class ViewReaderWithDefault { static_cast(_name); } } else if constexpr (size_ <= 4) { - assign_to_matching_field_small(*r_, _name, _var, view_, errors_, - found_.get(), + assign_to_matching_field_small(*r_, _name, _var, view_, errors_, &found_, std::make_integer_sequence()); } else { - assign_to_matching_field(*r_, _name, _var, view_, errors_, found_.get(), + assign_to_matching_field(*r_, _name, _var, view_, errors_, &found_, next_idx_, std::make_integer_sequence()); } @@ -278,7 +277,7 @@ class ViewReaderWithDefault { ViewType* view_; /// Indicates that a certain field has been found. - rfl::Ref> found_; + mutable std::array found_{}; /// Stores field errors according to the selected processor policy. FieldErrors* errors_; diff --git a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp index 72512ff66..8a6ea3f5d 100644 --- a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp @@ -1,22 +1,29 @@ #ifndef RFL_PARSING_VIEWREADERWITHDEFAULTANDSTRIPPEDFIELDNAMES_HPP_ #define RFL_PARSING_VIEWREADERWITHDEFAULTANDSTRIPPEDFIELDNAMES_HPP_ +#include #include -#include +#include #include -#include +#include #include #include #include "../Result.hpp" #include "../Tuple.hpp" -#include "../internal/is_array.hpp" +#include "../internal/cold.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" -#include "wrap_error.hpp" +#include "ViewReaderWithDefault.hpp" namespace rfl::parsing { +RFL_COLD inline Error make_default_stripped_view_too_many_fields_error( + const size_t _size) { + return Error("Expected a maximum of " + std::to_string(_size) + + " fields, but got at least one more."); +} + template class ViewReaderWithDefaultAndStrippedFieldNames { public: @@ -32,8 +39,8 @@ class ViewReaderWithDefaultAndStrippedFieldNames { * @param _view The view to read into. * @param _errors The field error collector. */ - ViewReaderWithDefaultAndStrippedFieldNames(const R* _r, ViewType* _view, - FieldErrors* _errors) + ViewReaderWithDefaultAndStrippedFieldNames( + const R* _r, ViewType* _view, FieldErrors* _errors) : i_(0), r_(_r), view_(_view), errors_(_errors) {} ~ViewReaderWithDefaultAndStrippedFieldNames() = default; @@ -44,9 +51,10 @@ class ViewReaderWithDefaultAndStrippedFieldNames { * @return The boolean array. */ std::array found() const { - std::array f; - std::fill(f.begin(), f.begin() + i_, true); - std::fill(f.begin() + i_, f.end(), false); + std::array f{}; + if constexpr (size_ > 0) { + std::fill(f.begin(), f.begin() + i_, true); + } return f; } @@ -57,15 +65,20 @@ class ViewReaderWithDefaultAndStrippedFieldNames { * @return An optional error. */ std::optional read(const InputVarType& _var) const { - if (should_stop_on_field_error(*errors_)) return std::nullopt; - if (i_ == size_) { - std::stringstream stream; - stream << "Expected a maximum of " << std::to_string(size_) - << " fields, but got at least one more."; - return Error(stream.str()); + if (should_stop_on_field_error(*errors_)) { + return std::nullopt; + } + if (static_cast(i_) == size_) [[unlikely]] { + return make_default_stripped_view_too_many_fields_error(size_); + } + if constexpr (size_ > 0) { + assign_by_index( + *r_, i_, _var, view_, errors_, + std::make_integer_sequence(size_)>()); + ++i_; + } else { + static_cast(_var); } - assign_to_field_i(_var, std::make_integer_sequence()); - ++i_; return std::nullopt; } @@ -77,69 +90,34 @@ class ViewReaderWithDefaultAndStrippedFieldNames { static constexpr size_t size() { return size_; } private: - template - static void assign_if_field_is_field_i(const R& _r, const auto& _var, - auto* _view, auto* _errors, int _i) { - using FieldType = tuple_element_t; - using OriginalType = typename FieldType::Type; - using T = - std::remove_cvref_t>; - constexpr auto name = FieldType::name(); - if (_i == i) { - auto res = Parser::read(_r, _var); - if (!res) { - _errors->emplace_back(wrap_field_error(name, std::move(res).error())); - return; - } - if constexpr (std::is_pointer_v) { - move_to(rfl::get(*_view), &(*res)); - } else { - rfl::get(*_view) = std::move(*res); - } - } - } - template - void assign_to_field_i(const InputVarType& _var, - std::integer_sequence) const { - using ReadField = void (*)( - const ViewReaderWithDefaultAndStrippedFieldNames*, const InputVarType&); - static constexpr std::array readers{ - +[](const ViewReaderWithDefaultAndStrippedFieldNames* self, - const InputVarType& var) { - assign_if_field_is_field_i(*self->r_, var, self->view_, - self->errors_, is); - }...}; - readers[i_](this, _var); - } - - // TODO: Unnecessary code duplication. - 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])); - } + static void assign_by_index(const R& _r, const int _idx, + const InputVarType& _var, ViewType* _view, + FieldErrors* _errors, + std::integer_sequence) { + using Fields = typename ViewType::Fields; + if constexpr (size_ <= 4) { + static_cast( + ((_idx == is + ? (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) || + ...)); } else { - for (size_t i = 0; i < _s->size(); ++i) { - move_to(&((*_t)[i]), &((*_s)[i])); - } + static_cast( + ((_idx == is + ? (assign_default_view_field< + R, W, typename tuple_element_t::Type, + ProcessorsType>(_r, _var, + tuple_element_t::name(), + rfl::get(*_view), _errors), + true) + : false) || + ...)); } } diff --git a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp index 2b356f807..9ab72df69 100644 --- a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp @@ -2,20 +2,27 @@ #define RFL_PARSING_VIEWREADERWITHSTRIPPEDFIELDNAMES_HPP_ #include +#include #include -#include +#include #include #include #include "../Result.hpp" #include "../Tuple.hpp" -#include "../internal/is_array.hpp" +#include "../internal/cold.hpp" #include "FieldErrors.hpp" #include "Parser_base.hpp" -#include "wrap_error.hpp" +#include "ViewReader.hpp" namespace rfl::parsing { +RFL_COLD inline Error make_stripped_view_too_many_fields_error( + const size_t _size) { + return Error("Expected a maximum of " + std::to_string(_size) + + " fields, but got at least one more."); +} + template class ViewReaderWithStrippedFieldNames { public: @@ -54,15 +61,21 @@ class ViewReaderWithStrippedFieldNames { * @return An optional error. */ std::optional read(const InputVarType& _var) const { - if (should_stop_on_field_error(*errors_)) return std::nullopt; - if (i_ == size_) { - std::stringstream stream; - stream << "Expected a maximum of " << std::to_string(size_) - << " fields, but got at least one more."; - return Error(stream.str()); + if (should_stop_on_field_error(*errors_)) { + return std::nullopt; + } + if (static_cast(i_) == size_) [[unlikely]] { + return make_stripped_view_too_many_fields_error(size_); + } + if constexpr (size_ > 0) { + (*found_)[static_cast(i_)] = true; + assign_by_index( + *r_, i_, _var, view_, errors_, set_, + std::make_integer_sequence(size_)>()); + ++i_; + } else { + static_cast(_var); } - assign_to_field_i(_var, std::make_integer_sequence()); - ++i_; return std::nullopt; } @@ -74,64 +87,37 @@ class ViewReaderWithStrippedFieldNames { static constexpr size_t size() { return size_; } private: - template - static void assign_if_field_is_field_i(const R& _r, const auto& _var, - auto* _view, auto* _errors, - auto* _found, auto* _set, int _i) { - using FieldType = tuple_element_t; - using OriginalType = typename FieldType::Type; - using T = - std::remove_cvref_t>; - constexpr auto name = FieldType::name(); - if (_i == i) { - std::get(*_found) = true; - auto res = Parser::read(_r, _var); - if (!res) { - _errors->emplace_back(wrap_field_error(name, std::move(res).error())); - return; - } - if constexpr (std::is_pointer_v) { - move_to(rfl::get(*_view), &(*res)); - } else { - rfl::get(*_view) = std::move(*res); - } - std::get(*_set) = true; - } - } - template - void assign_to_field_i(const InputVarType& _var, - std::integer_sequence) const { - using ReadField = - void (*)(const ViewReaderWithStrippedFieldNames*, const InputVarType&); - static constexpr std::array readers{ - +[](const ViewReaderWithStrippedFieldNames* self, - const InputVarType& var) { - assign_if_field_is_field_i(*self->r_, var, self->view_, - self->errors_, self->found_, - self->set_, is); - }...}; - readers[i_](this, _var); - } - - // TODO: Unnecessary code duplication. - 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])); - } + 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) { + 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) || + ...)); } else { - for (size_t i = 0; i < _s->size(); ++i) { - move_to(&((*_t)[i]), &((*_s)[i])); - } + 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) || + ...)); } } diff --git a/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp b/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp index ff8718345..fef16bbf0 100644 --- a/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp +++ b/include/rfl/parsing/call_destructors_on_tuple_where_necessary.hpp @@ -1,40 +1,61 @@ #ifndef RFL_PARSING_CALL_DESTRUCTORS_ON_TUPLE_WHERE_NECESSARY_HPP_ #define RFL_PARSING_CALL_DESTRUCTORS_ON_TUPLE_WHERE_NECESSARY_HPP_ +#include #include +#include #include "../Tuple.hpp" #include "call_destructors_where_necessary.hpp" namespace rfl::parsing { +template +inline constexpr bool needs_tuple_element_destructor_v = + !std::is_trivially_destructible_v< + std::remove_all_extents_t>>; + /** - * @brief Calls the destructor on a single element of a tuple, if necessary. - * - * @tparam TupleType The type of the tuple. - * @tparam _size The size of the tuple. - * @tparam _i The index of the element. - * @param _num_set The number of elements that have been set. - * @param _tup The tuple to call the destructor on. + * @brief Calls the destructor on a single tuple element if it was set. + * Free function parameterized only by ElemType (never TupleType or index) + * so instantiations do not carry the enclosing tuple in mangled symbol names + * or DWARF debug info. */ -template -void call_destructor_on_one_tuple_element_if_necessary(const size_t _num_set, - TupleType* _tup) { - using FieldType = tuple_element_t<_i, TupleType>; - using ValueType = std::remove_cv_t>; - if constexpr (!std::is_array_v && - std::is_destructible_v) { - if (_i < _num_set) { - rfl::get<_i>(*_tup).~ValueType(); +template +inline void destroy_tuple_element_if_set(const bool _is_set, + ElemType& _elem) noexcept { + using ValueType = std::remove_cv_t; + if constexpr (std::is_array_v) { + if constexpr (!std::is_trivially_destructible_v< + std::remove_all_extents_t>) { + if (_is_set) { + call_destructor_on_array(sizeof(_elem) / sizeof(_elem[0]), _elem); + } } - } else if constexpr (std::is_array_v) { - if (_i < _num_set) { - auto ptr = rfl::get<_i>(*_tup); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } else if constexpr (std::is_destructible_v && + !std::is_trivially_destructible_v) { + if (_is_set) { + _elem.~ValueType(); } } } +template +inline void call_destructors_on_tuple_impl( + const size_t _num_set, TupleType* _tup, + std::integer_sequence) noexcept { + if constexpr ((needs_tuple_element_destructor_v< + rfl::tuple_element_t> || + ...)) { + (destroy_tuple_element_if_set(static_cast(is) < _num_set, + rfl::get(*_tup)), + ...); + } else { + static_cast(_num_set); + static_cast(_tup); + } +} + /** * @brief Calls the destructors on the elements of a tuple, if necessary. * @@ -45,12 +66,15 @@ void call_destructor_on_one_tuple_element_if_necessary(const size_t _num_set, template void call_destructors_on_tuple_where_necessary(const size_t _num_set, TupleType* _tup) { - [&](std::integer_sequence) { - (call_destructor_on_one_tuple_element_if_necessary< - TupleType, rfl::tuple_size_v, is>(_num_set, _tup), - ...); + constexpr size_t size = rfl::tuple_size_v; + if constexpr (size > 0) { + call_destructors_on_tuple_impl( + _num_set, _tup, + std::make_integer_sequence(size)>()); + } else { + static_cast(_num_set); + static_cast(_tup); } - (std::make_integer_sequence>()); } } // namespace rfl::parsing diff --git a/include/rfl/parsing/call_destructors_where_necessary.hpp b/include/rfl/parsing/call_destructors_where_necessary.hpp index 403609760..9ead55733 100644 --- a/include/rfl/parsing/call_destructors_where_necessary.hpp +++ b/include/rfl/parsing/call_destructors_where_necessary.hpp @@ -2,9 +2,11 @@ #define RFL_PARSING_CALL_DESTRUCTORS_WHERE_NECESSARY_HPP_ #include +#include #include +#include -#include "../get.hpp" +#include "../Tuple.hpp" namespace rfl::parsing { @@ -17,45 +19,67 @@ namespace rfl::parsing { */ template void call_destructor_on_array(const size_t _size, T* _ptr) { - for (size_t i = 0; i < _size; ++i) { - if constexpr (std::is_array_v) { - call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); - } else if constexpr (std::is_destructible_v>) { - (_ptr + i)->~T(); + if constexpr (!std::is_trivially_destructible_v< + std::remove_all_extents_t>) { + for (size_t i = 0; i < _size; ++i) { + if constexpr (std::is_array_v) { + call_destructor_on_array(sizeof(*_ptr) / sizeof(**_ptr), *(_ptr + i)); + } else if constexpr (std::is_destructible_v>) { + using ValueType = std::remove_cv_t; + (_ptr + i)->~ValueType(); + } } } } +template +inline constexpr bool needs_field_destructor_v = + std::is_pointer_v> && + !std::is_trivially_destructible_v>>>>; + /** - * @brief Calls the destructor on a single element of a view, if necessary. - * - * @tparam ViewType The type of the view. - * @tparam _size The size of the set array. - * @tparam _i The index of the element. - * @param _set A boolean array indicating which elements have been set. - * @param _view The view to call the destructor on. + * @brief Calls the destructor on a single field pointer if it was set. + * Free function parameterized only by the field's pointer type (never + * ViewType) so instantiations do not carry the enclosing NamedTuple in their + * mangled symbol name or DWARF debug info. */ -template -void call_destructor_on_one_if_necessary(const std::array& _set, - ViewType* _view) { - using FieldType = tuple_element_t<_i, typename ViewType::Fields>; - using OriginalType = std::remove_cv_t; - using ValueType = - std::remove_cvref_t>; - if constexpr (!std::is_array_v && - std::is_pointer_v && - std::is_destructible_v) { - if (std::get<_i>(_set)) { - rfl::get<_i>(*_view)->~ValueType(); +template +inline void destroy_field_if_set(const bool _is_set, + const OriginalType& _slot) noexcept { + using CleanOriginal = std::remove_cvref_t; + using ValueType = std::remove_cvref_t>; + if constexpr (std::is_pointer_v && + std::is_array_v) { + if constexpr (!std::is_trivially_destructible_v< + std::remove_all_extents_t>) { + if (_is_set) { + call_destructor_on_array(sizeof(*_slot) / sizeof(**_slot), *_slot); + } } - } else if constexpr (std::is_array_v) { - if (std::get<_i>(_set)) { - auto ptr = rfl::get<_i>(*_view); - call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); + } else if constexpr (std::is_pointer_v && + std::is_destructible_v && + !std::is_trivially_destructible_v) { + if (_is_set) { + _slot->~ValueType(); } } } +template +inline void destroy_view_values_where_necessary( + const std::array& _set, ValuesTuple& _values, + std::integer_sequence) noexcept { + if constexpr ((needs_field_destructor_v(_values))> || + ...)) { + (destroy_field_if_set(std::get(_set), rfl::get(_values)), ...); + } else { + static_cast(_set); + static_cast(_values); + } +} + /** * @brief Calls the destructors on the elements of a view, if necessary. * @@ -67,10 +91,14 @@ void call_destructor_on_one_if_necessary(const std::array& _set, template void call_destructors_where_necessary(const std::array& _set, ViewType* _view) { - [&](std::integer_sequence) { - (call_destructor_on_one_if_necessary(_set, _view), - ...); - }(std::make_integer_sequence()); + if constexpr (_size > 0) { + destroy_view_values_where_necessary( + _set, _view->values(), + std::make_integer_sequence(_size)>()); + } else { + static_cast(_set); + static_cast(_view); + } } } // namespace rfl::parsing