Skip to content
Closed
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
68 changes: 25 additions & 43 deletions include/rfl/Tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <array>
#include <cstddef>
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
Expand Down Expand Up @@ -68,7 +69,7 @@ class Tuple {
template <int _index>
constexpr auto& get() {
using Type = internal::nth_element_t<_index, Types...>;
return *internal::ptr_cast<Type*>(data_ + pos<_index>());
return *internal::ptr_cast<Type*>(data_ + std::get<_index>(positions_));
}

/// Gets an element by index (const).
Expand All @@ -77,7 +78,8 @@ class Tuple {
template <int _index>
constexpr const auto& get() const {
using Type = internal::nth_element_t<_index, Types...>;
return *internal::ptr_cast<const Type*>(data_ + pos<_index>());
return *internal::ptr_cast<const Type*>(data_ +
std::get<_index>(positions_));
}

/// Copy assignment operator. Assigns from another Tuple by copy.
Expand Down Expand Up @@ -154,13 +156,10 @@ class Tuple {
template <int... _is>
void copy_from_other(const Tuple& _other,
std::integer_sequence<int, _is...>) {
const auto copy_one = [this]<int _i>(const auto& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>()))
Type(_other.template get<_i>());
};
(copy_one(_other, std::integral_constant<int, _is>{}), ...);
((::new (static_cast<void*>(data_ + std::get<_is>(positions_)))
Types(*internal::ptr_cast<const Types*>(
_other.data_ + std::get<_is>(positions_)))),
...);
}

/// Copies all elements from provided arguments.
Expand All @@ -169,60 +168,43 @@ class Tuple {
template <int... _is>
void copy_from_types(const Types&... _types,
std::integer_sequence<int, _is...>) {
const auto copy_one = [this]<int _i>(const auto& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>())) Type(_t);
};
(copy_one(_types, std::integral_constant<int, _is>{}), ...);
((::new (static_cast<void*>(data_ + std::get<_is>(positions_)))
Types(_types)),
...);
}

/// Destroys all elements if they are destructible.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void destroy_if_necessary(std::integer_sequence<int, _is...>) {
const auto destroy_one = [](auto& _t) {
using Type = std::remove_cvref_t<decltype(_t)>;
if constexpr (std::is_destructible_v<Type>) {
_t.~Type();
}
};
(destroy_one(get<_is>()), ...);
if constexpr (!(std::is_trivially_destructible_v<Types> && ...)) {
((std::is_trivially_destructible_v<Types>
? void()
: std::destroy_at(
internal::ptr_cast<Types*>(data_ + std::get<_is>(positions_)))),
...);
}
}

/// Moves all elements from another Tuple.
/// @param _other The Tuple to move from.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void move_from_other(Tuple&& _other, std::integer_sequence<int, _is...>) {
const auto move_one = [this]<int _i>(auto&& _other,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>()))
Type(std::move(_other.template get<_i>()));
};
(move_one(_other, std::integral_constant<int, _is>{}), ...);
((::new (static_cast<void*>(data_ + std::get<_is>(positions_)))
Types(std::move(*internal::ptr_cast<Types*>(
_other.data_ + std::get<_is>(positions_))))),
...);
}

/// Moves all elements from provided arguments.
/// @param _types The values to move.
/// @param seq_ Integer sequence for element indices.
template <int... _is>
void move_from_types(Types&&... _types, std::integer_sequence<int, _is...>) {
const auto move_one = [this]<int _i>(auto&& _t,
std::integral_constant<int, _i>) {
using Type = internal::nth_element_t<_i, Types...>;
::new (static_cast<void*>(data_ + pos<_i>())) Type(std::move(_t));
};
(move_one(std::move(_types), std::integral_constant<int, _is>{}), ...);
}

/// Returns the byte offset of the element at index _i.
/// @tparam _i The index of the element.
/// @return The byte offset of the element.
template <int _i>
static consteval unsigned int pos() {
return std::get<_i>(positions_);
((::new (static_cast<void*>(data_ + std::get<_is>(positions_)))
Types(std::move(_types))),
...);
}

private:
Expand Down
6 changes: 5 additions & 1 deletion include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,11 @@ class UsageWalker {
}

if (def_obj) {
auto view = ProcessorsType::template process<S>(rfl::to_view(*def_obj));
// Reuse the non-const to_view<S> instantiation already created by read()
// instead of duplicating the entire reflection tree for `const S`.
// The view is only read through const pointers below.
auto view = ProcessorsType::template process<S>(
rfl::to_view(const_cast<S&>(*def_obj)));
using ViewType = decltype(view);
[&]<size_t... _is>(std::index_sequence<_is...>) {
auto visit = [&]<size_t _i>() {
Expand Down
145 changes: 71 additions & 74 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,38 +352,75 @@ struct NamedTupleParser {
}

/// Generates error messages for when fields are missing.
template <int _i>
template <int _i, bool _defaults_constructed>
static RFL_ALWAYS_INLINE void handle_one_missing_field(
const std::array<bool, size_>& _found, const NamedTupleType& _view,
std::array<bool, size_>* _set, FieldErrors<ProcessorsType>* _errors,
const bool _value_initialize_default_vals) noexcept {
if (should_stop_on_field_error<ProcessorsType>(*_errors)) return;
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
using ValueType = std::remove_reference_t<
std::remove_pointer_t<typename FieldType::Type>>;
constexpr bool is_default_val_field =
internal::is_default_val_v<ValueType>;
constexpr bool is_required_field =
!is_default_val_field && !internal::is_extra_fields_v<ValueType> &&
(_all_required || is_required<ValueType, _ignore_empty_containers>());

// When the struct was default-constructed, missing optional or DefaultVal
// fields already hold their default values, so nothing is needed at all.
if constexpr (_defaults_constructed && !is_required_field) {
return;
} else {
if (should_stop_on_field_error<ProcessorsType>(*_errors)) return;
if (!std::get<_i>(_found)) {
if constexpr (!_defaults_constructed &&
internal::is_unwrapped_default_val_v<ValueType>) {
if constexpr (can_value_initialize_default_val<ValueType>()) {
if (_value_initialize_default_vals) {
// The value's constructor can throw, for instance a Validator
// whose zero value is invalid.
try {
::new (rfl::get<_i>(_view)) ValueType();
} catch (std::exception& e) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
_errors->emplace_back(make_nested_error(
std::string(current_name),
"Field named '" + std::string(current_name) +
"' not found, and its default value could not be "
"constructed: " +
e.what()));
return;
}
std::get<_i>(*_set) = true;
return;
}
}
}

if (!std::get<_i>(_found)) {
constexpr bool is_default_val_field =
internal::is_default_val_v<ValueType>;
constexpr bool is_required_field =
!is_default_val_field && !internal::is_extra_fields_v<ValueType> &&
(_all_required || is_required<ValueType, _ignore_empty_containers>());

// Without _set, the struct was default-constructed before it was read,
// so the field already holds its default value. With _set, the fields
// are constructed in raw memory. An rfl::NamedTuple has no in-class
// initializers, so a missing bare rfl::DefaultVal field is
// value-initialized there. Otherwise the field's default value is not
// available, so the field is required.
const bool defaults_constructed = _set == nullptr;

if constexpr (internal::is_unwrapped_default_val_v<ValueType>) {
if constexpr (can_value_initialize_default_val<ValueType>()) {
if (_value_initialize_default_vals && !defaults_constructed) {
// The value's constructor can throw, for instance a Validator
// whose zero value is invalid.
if constexpr (is_required_field ||
(is_default_val_field && !_defaults_constructed)) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
std::stringstream stream;
stream << "Field named '" << std::string(current_name)
<< "' not found.";
_errors->emplace_back(
make_nested_error(std::string(current_name), stream.str()));
return;
}

if constexpr (!_defaults_constructed && !is_required_field &&
!is_default_val_field) {
using NonConstT = std::remove_const_t<ValueType>;
const auto ptr = const_cast<NonConstT*>(rfl::get<_i>(_view));
if constexpr (std::is_nothrow_default_constructible_v<NonConstT>) {
::new (ptr) NonConstT();
} else {
// The default value can throw, for instance a Validator whose
// default value is invalid.
try {
::new (rfl::get<_i>(_view)) ValueType();
::new (ptr) NonConstT();
} catch (std::exception& e) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
Expand All @@ -395,51 +432,10 @@ struct NamedTupleParser {
e.what()));
return;
}
std::get<_i>(*_set) = true;
return;
}
std::get<_i>(*_set) = true;
}
}

if (is_required_field ||
(is_default_val_field && !defaults_constructed)) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
std::stringstream stream;
stream << "Field named '" << std::string(current_name)
<< "' not found.";
_errors->emplace_back(
make_nested_error(std::string(current_name), stream.str()));
return;
}

if constexpr (!is_required_field && !is_default_val_field) {
if (defaults_constructed) {
return;
}
using NonConstT = std::remove_const_t<ValueType>;
const auto ptr = const_cast<NonConstT*>(rfl::get<_i>(_view));
if constexpr (std::is_nothrow_default_constructible_v<NonConstT>) {
::new (ptr) NonConstT();
} else {
// The default value can throw, for instance a Validator whose
// default value is invalid.
try {
::new (ptr) NonConstT();
} catch (std::exception& e) {
constexpr auto current_name =
internal::nth_element_t<_i, FieldTypes...>::name();
_errors->emplace_back(make_nested_error(
std::string(current_name),
"Field named '" + std::string(current_name) +
"' not found, and its default value could not be "
"constructed: " +
e.what()));
return;
}
}
std::get<_i>(*_set) = true;
}
}
}

Expand All @@ -454,14 +450,14 @@ struct NamedTupleParser {
}

/// Generates error messages for when fields are missing.
template <int... _is>
template <bool _defaults_constructed, int... _is>
static RFL_ALWAYS_INLINE void handle_missing_fields(
const std::array<bool, size_>& _found, const NamedTupleType& _view,
std::array<bool, size_>* _set, FieldErrors<ProcessorsType>* _errors,
const bool _value_initialize_default_vals,
std::integer_sequence<int, _is...>) noexcept {
(handle_one_missing_field<_is>(_found, _view, _set, _errors,
_value_initialize_default_vals),
(handle_one_missing_field<_is, _defaults_constructed>(
_found, _view, _set, _errors, _value_initialize_default_vals),
...);
}

Expand Down Expand Up @@ -498,9 +494,9 @@ struct NamedTupleParser {
}
}
if (!should_stop_on_field_error<ProcessorsType>(errors)) {
handle_missing_fields(found, *_view, &set, &errors,
_value_initialize_default_vals,
std::make_integer_sequence<int, size_>());
handle_missing_fields</*_defaults_constructed=*/false>(
found, *_view, &set, &errors, _value_initialize_default_vals,
std::make_integer_sequence<int, size_>());
}
if constexpr (internal::fail_fast_v<ProcessorsType>) {
return std::make_pair(set, std::move(errors).error());
Expand Down Expand Up @@ -531,9 +527,10 @@ struct NamedTupleParser {
if constexpr (internal::has_default_val_v<NamedTupleType> &&
!internal::default_if_missing_v<ProcessorsType>) {
if (!should_stop_on_field_error<ProcessorsType>(errors)) {
handle_missing_fields(reader.found(), *_view, nullptr, &errors,
/*_value_initialize_default_vals=*/false,
std::make_integer_sequence<int, size_>());
handle_missing_fields</*_defaults_constructed=*/true>(
reader.found(), *_view, nullptr, &errors,
/*_value_initialize_default_vals=*/false,
std::make_integer_sequence<int, size_>());
}
}
if constexpr (internal::fail_fast_v<ProcessorsType>) {
Expand Down
40 changes: 12 additions & 28 deletions include/rfl/parsing/ViewReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "../internal/no_extra_fields_v.hpp"
#include "FieldErrors.hpp"
#include "Parser_base.hpp"
#include "peel_transparent_wrappers.hpp"
#include "schemaful/IsSchemafulReader.hpp"
#include "wrap_error.hpp"

Expand Down Expand Up @@ -115,19 +116,22 @@ class ViewReader {
is_matching<i, FieldType>(_current_name_or_index)) {
std::get<i>(*_found) = true;
*_already_assigned = true;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
using Peel = peel_transparent<T, R>;
auto res = read_field_core<R, W, typename Peel::CoreType,
ProcessorsType>(_r, _var, FieldType::name(),
_errors, size_);
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;
}
if constexpr (std::is_pointer_v<OriginalType>) {
move_to(rfl::get<i>(*_view), &(*res));
if constexpr (Peel::peeled) {
auto wrapped = Peel::wrap(std::move(*res));
placement_move_field(rfl::get<i>(*_view), &wrapped);
} else {
placement_move_field(rfl::get<i>(*_view), &(*res));
}
} else {
rfl::get<i>(*_view) = std::move(*res);
rfl::get<i>(*_view) = Peel::wrap(std::move(*res));
}
std::get<i>(*_set) = true;
}
Expand Down Expand Up @@ -192,26 +196,6 @@ class ViewReader {
}
}

template <class Target, class Source>
static void move_to(Target* _t, Source* _s) {
if constexpr (std::is_const_v<Target>) {
return move_to(const_cast<std::remove_const_t<Target>*>(_t), _s);
} else if constexpr (!rfl::internal::is_array_v<Source> &&
!std::is_array_v<Target>) {
::new (_t) Target(std::move(*_s));
} else if constexpr (rfl::internal::is_array_v<Source>) {
static_assert(std::is_array_v<Target>,
"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]));
}
}
}

private:
/// The underlying reader.
const R* r_;
Expand Down
Loading