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
77 changes: 61 additions & 16 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_NAMEDTUPLEPARSER_HPP_

#include <array>
#include <cstddef>
#include <exception>
#include <map>
#include <sstream>
Expand Down Expand Up @@ -35,6 +36,7 @@
#include "call_destructors_where_necessary.hpp"
#include "is_empty.hpp"
#include "is_required.hpp"
#include "peel_transparent_wrappers.hpp"
#include "schema/Type.hpp"
#include "to_single_error_message.hpp"
#include "wrap_error.hpp"
Expand Down Expand Up @@ -78,6 +80,27 @@ concept CanEmitDefault =

namespace rfl::parsing {

/// Wraps an inner schema::Type in peeled outer-to-inner Description/Deprecated
/// metadata nodes without instantiating Parser::to_schema per literal string.
inline schema::Type wrap_schema_type(schema::Type _inner,
const SchemaWrapperMeta* _metas,
const std::size_t _count) {
for (std::size_t k = _count; k > 0; --k) {
const auto& m = _metas[k - 1];
if (m.kind == SchemaWrapperKind::description) {
_inner = schema::Type{schema::Type::Description{
.description_ = std::string(m.description),
.type_ = Ref<schema::Type>::make(std::move(_inner))}};
} else {
_inner = schema::Type{schema::Type::Deprecated{
.deprecation_message_ = std::string(m.deprecation_message),
.description_ = std::string(m.description),
.type_ = Ref<schema::Type>::make(std::move(_inner))}};
}
}
return _inner;
}

template <class R, class W, bool _ignore_empty_containers, bool _all_required,
bool _no_field_names, class ProcessorsType, class... FieldTypes>
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
Expand Down Expand Up @@ -251,8 +274,8 @@ struct NamedTupleParser {
const NamedTuple<FieldTypes...>& _tup,
OutputObjectOrArrayType* _ptr) {
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
const auto value = rfl::get<_i>(_tup);
const auto& value = peel_write_value(rfl::get<_i>(_tup));
using ValueType = std::remove_cvref_t<decltype(value)>;
if constexpr (internal::is_extra_fields_v<ValueType>) {
for (const auto& [k, v] : *value) {
const auto new_parent = make_parent(k, _ptr);
Expand Down Expand Up @@ -291,22 +314,44 @@ struct NamedTupleParser {
using F = internal::nth_element_t<_i, FieldTypes...>;
using U = std::remove_cvref_t<typename F::Type>;
if constexpr (!internal::is_skip_v<U> && !internal::is_extra_fields_v<U>) {
// Add default value here
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
if constexpr (!std::is_same_v<View, void> && internal::CanEmitDefault<U>) {
s.variant_.visit([&](auto& value) {
if constexpr (std::is_same_v<std::remove_cvref_t<decltype(value)>,
schema::Type::DefaultVal>) {
value.default_value_ =
rfl::to_generic<ProcessorsType>((*rfl::get<_i>(*_view)).get());
}
});
}
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
if constexpr (is_peeled_field_v<U>) {
static_cast<void>(_view);
using PeeledU = peeled_type_t<U>;
auto s = Parser<R, W, PeeledU, ProcessorsType>::to_schema(_definitions);
if constexpr (peel_transparent_traits<U>::schema_meta_count > 0) {
static constexpr auto kMetas = make_field_schema_metas<U>();
s = wrap_schema_type(std::move(s), kMetas.data(), kMetas.size());
}
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
} else {
_schema->types_[std::string(F::name())] = std::move(s);
}
} else {
_schema->types_[std::string(F::name())] = std::move(s);
// Add default value here
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
if constexpr (!std::is_same_v<View, void> &&
internal::CanEmitDefault<U>) {
s.variant_.visit([&](auto& value) {
if constexpr (std::is_same_v<std::remove_cvref_t<decltype(value)>,
schema::Type::DefaultVal>) {
value.default_value_ =
rfl::to_generic<ProcessorsType>((*rfl::get<_i>(*_view)).get());
}
});
} else {
static_cast<void>(_view);
}
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
} else {
_schema->types_[std::string(F::name())] = std::move(s);
}
}
} else {
static_cast<void>(_definitions);
static_cast<void>(_schema);
static_cast<void>(_view);
}
};

Expand Down
68 changes: 58 additions & 10 deletions include/rfl/parsing/ViewReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -54,6 +55,23 @@ RFL_COLD void record_uninitialized_field_error(const std::string_view _name,
_errors->emplace_back(wrap_field_error(_name, std::move(_err)));
}

/// Parses a single unwrapped core type and records any error into `_errors`.
/// Parameterized only by (R, W, CoreType, ProcessorsType) so distinct
/// Description/Deprecated/Rename/Short wrapper strings share one compiled read
/// instantiation.
template <class R, class W, class CoreType, class ProcessorsType>
auto read_uninitialized_view_field_core(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, FieldErrors<ProcessorsType>* _errors,
const size_t _reserve_errors) {
auto res = Parser<R, W, CoreType, ProcessorsType>::read(_r, _var);
if (!res) [[unlikely]] {
record_uninitialized_field_error(_name, std::move(res).error(), _errors,
_reserve_errors);
}
return res;
}

/// 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
Expand All @@ -64,21 +82,40 @@ RFL_ALWAYS_INLINE void assign_uninitialized_view_field_inline(
const std::string_view _name, OriginalType& _slot, bool& _is_set,
FieldErrors<ProcessorsType>* _errors, const size_t _reserve_errors) {
using T = std::remove_cvref_t<std::remove_pointer_t<OriginalType>>;
auto res = Parser<R, W, T, ProcessorsType>::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<OriginalType>) {
placement_move_field(_slot, &(*res));
if constexpr (is_peeled_read_v<T, R>) {
using CoreType = peeled_read_type_t<T, R>;
auto res =
read_uninitialized_view_field_core<R, W, CoreType, ProcessorsType>(
_r, _var, _name, _errors, _reserve_errors);
if (!res) [[unlikely]] {
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
::new (const_cast<T*>(_slot))
T(wrap_peeled_read<T, R>(std::move(*res)));
} else {
_slot = wrap_peeled_read<T, R>(std::move(*res));
}
_is_set = true;
} else {
_slot = std::move(*res);
auto res = Parser<R, W, T, ProcessorsType>::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<OriginalType>) {
placement_move_field(_slot, &(*res));
} else {
_slot = std::move(*res);
}
_is_set = true;
}
_is_set = true;
}

template <class R, class W, class OriginalType, class ProcessorsType>
requires(!is_peeled_read_v<
std::remove_cvref_t<std::remove_pointer_t<OriginalType>>, R>)
inline void assign_uninitialized_view_field(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, OriginalType& _slot, bool& _is_set,
Expand All @@ -87,6 +124,17 @@ inline void assign_uninitialized_view_field(
_r, _var, _name, _slot, _is_set, _errors, _reserve_errors);
}

template <class R, class W, class OriginalType, class ProcessorsType>
requires(is_peeled_read_v<
std::remove_cvref_t<std::remove_pointer_t<OriginalType>>, R>)
RFL_PEEL_ALWAYS_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<ProcessorsType>* _errors, const size_t _reserve_errors) {
assign_uninitialized_view_field_inline<R, W, OriginalType, ProcessorsType>(
_r, _var, _name, _slot, _is_set, _errors, _reserve_errors);
}

/// Reads one extra field into an uninitialized ExtraFields slot.
template <class R, class W, class ExtraFieldsType, class ProcessorsType>
void assign_extra_uninitialized_field(
Expand Down
98 changes: 90 additions & 8 deletions include/rfl/parsing/ViewReaderWithDefault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "../internal/no_extra_fields_v.hpp"
#include "FieldErrors.hpp"
#include "Parser_base.hpp"
#include "peel_transparent_wrappers.hpp"
#include "wrap_error.hpp"

namespace rfl::parsing {
Expand Down Expand Up @@ -58,6 +59,69 @@ RFL_COLD void record_default_field_error(const std::string_view _name,
_errors->emplace_back(wrap_field_error(_name, std::move(_err)));
}

/// Parses a single unwrapped core type and records any error into `_errors`.
/// Parameterized only by (R, W, CoreType, ProcessorsType) so distinct
/// Description/Deprecated/Rename/Short wrapper strings share one compiled read
/// instantiation.
template <class R, class W, class CoreType, class ProcessorsType>
auto read_default_view_field_core(const R& _r,
const typename R::InputVarType& _var,
const std::string_view _name,
FieldErrors<ProcessorsType>* _errors) {
auto res = Parser<R, W, CoreType, ProcessorsType>::read(_r, _var);
if (!res) [[unlikely]] {
record_default_field_error(_name, std::move(res).error(), _errors);
}
return res;
}

template <class R, class W, class OriginalType, class ProcessorsType>
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<ProcessorsType>* _errors);

template <class R, class W, class OriginalType, class ProcessorsType>
requires(!is_peeled_read_v<
std::remove_cvref_t<std::remove_pointer_t<OriginalType>>, R>)
inline void assign_default_view_field(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, OriginalType& _slot,
FieldErrors<ProcessorsType>* _errors);

template <bool _inline_core, class R, class W, class OriginalType,
class ProcessorsType>
RFL_PEEL_ALWAYS_INLINE void assign_default_peeled_view_field(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, OriginalType& _slot,
FieldErrors<ProcessorsType>* _errors) {
using Pointee = std::remove_pointer_t<OriginalType>;
using T = std::remove_cvref_t<Pointee>;
using CoreType = peeled_read_type_t<T, R>;
if constexpr (std::is_pointer_v<OriginalType> && !std::is_const_v<Pointee>) {
auto* core_ptr = peel_read_mut_ptr<R>(*_slot);
if constexpr (_inline_core) {
assign_default_view_field_inline<R, W, CoreType*, ProcessorsType>(
_r, _var, _name, core_ptr, _errors);
} else {
assign_default_view_field<R, W, CoreType*, ProcessorsType>(
_r, _var, _name, core_ptr, _errors);
}
} else {
auto res = read_default_view_field_core<R, W, CoreType, ProcessorsType>(
_r, _var, _name, _errors);
if (!res) [[unlikely]] {
return;
}
auto wrapped = wrap_peeled_read<T, R>(std::move(*res));
if constexpr (std::is_pointer_v<OriginalType>) {
move_assign_field(_slot, &wrapped);
} else {
_slot = std::move(wrapped);
}
}
}

/// 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
Expand All @@ -68,19 +132,26 @@ RFL_ALWAYS_INLINE void assign_default_view_field_inline(
const std::string_view _name, OriginalType& _slot,
FieldErrors<ProcessorsType>* _errors) {
using T = std::remove_cvref_t<std::remove_pointer_t<OriginalType>>;
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) [[unlikely]] {
record_default_field_error(_name, std::move(res).error(), _errors);
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
move_assign_field(_slot, &(*res));
if constexpr (is_peeled_read_v<T, R>) {
assign_default_peeled_view_field<true, R, W, OriginalType, ProcessorsType>(
_r, _var, _name, _slot, _errors);
} else {
_slot = std::move(*res);
auto res = Parser<R, W, T, ProcessorsType>::read(_r, _var);
if (!res) [[unlikely]] {
record_default_field_error(_name, std::move(res).error(), _errors);
return;
}
if constexpr (std::is_pointer_v<OriginalType>) {
move_assign_field(_slot, &(*res));
} else {
_slot = std::move(*res);
}
}
}

template <class R, class W, class OriginalType, class ProcessorsType>
requires(!is_peeled_read_v<
std::remove_cvref_t<std::remove_pointer_t<OriginalType>>, R>)
inline void assign_default_view_field(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, OriginalType& _slot,
Expand All @@ -89,6 +160,17 @@ inline void assign_default_view_field(
_r, _var, _name, _slot, _errors);
}

template <class R, class W, class OriginalType, class ProcessorsType>
requires(is_peeled_read_v<
std::remove_cvref_t<std::remove_pointer_t<OriginalType>>, R>)
RFL_PEEL_ALWAYS_INLINE void assign_default_view_field(
const R& _r, const typename R::InputVarType& _var,
const std::string_view _name, OriginalType& _slot,
FieldErrors<ProcessorsType>* _errors) {
assign_default_peeled_view_field<false, R, W, OriginalType, ProcessorsType>(
_r, _var, _name, _slot, _errors);
}

/// Reads one extra field into an already-constructed ExtraFields slot.
template <class R, class W, class ExtraFieldsType, class ProcessorsType>
void assign_extra_default_field(
Expand Down
Loading
Loading