From d8dc19cada17cb459ea73cf2ea6420e07959bca9 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 15:23:15 +0100 Subject: [PATCH 01/19] Avoid recursion in TupleReader --- include/rfl/parsing/TupleReader.hpp | 40 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index dab0c7fd..8ba1b50f 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -62,8 +62,18 @@ 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<_i>(_var, &err), ...); + if (!err) { + ++num_set_; + } + }(std::make_integer_sequence()); return err; } @@ -106,30 +116,20 @@ class TupleReader { } } - template + 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>; + using CurrentType = + std::remove_cvref_t>; - auto res = Parser::read(*r_, _var); + auto res = Parser::read(*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; + if (res) { + move_to(&(rfl::get<_i>(*tuple_)), &(*res)); + } else { + *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", + _i, std::move(res).error()); } - read_impl<_i + 1>(_var, _err); - } else { - std::stringstream stream; - stream << "Expected " << size_ << " fields, but got at least one more."; - *_err = Error(stream.str()); return; } } From 21334f86d9190e66cd092726126d436aaf03f16b Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 16:46:18 +0100 Subject: [PATCH 02/19] Get rid of unnecessary recursion in the TupleReader --- include/rfl/parsing/TupleReader.hpp | 49 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 8ba1b50f..47f726fb 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -69,7 +69,7 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_i>(_var, &err), ...); + (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); if (!err) { ++num_set_; } @@ -77,6 +77,26 @@ class TupleReader { return err; } + /*template + requires((sizeof...(Args) == size_) && ... && + std::is_same_v, Args>) + std::optional read_all(const Args&... _vars) const { + std::optional err; + [&](std::integer_sequence) { + (read_impl<_i>(_vars, &err), ...); + if (!err) { + ++num_set_; + } + }(std::make_integer_sequence()); + }*/ + + /** + * @brief Returns the number of fields in the tuple. + * + * @return The number of fields. + */ + static constexpr size_t size() { return size_; } + private: template void handle_missing_fields_impl(std::optional* _err) const noexcept { @@ -116,22 +136,25 @@ class TupleReader { } } - template + template void read_impl(const InputVarType& _var, std::optional* _err) const { - if constexpr (_i < size_) { - using CurrentType = - std::remove_cvref_t>; + if constexpr (_check_num_set) { + if (num_set_ != _i) { + return; + } + } + using CurrentType = + std::remove_cvref_t>; - auto res = Parser::read(*r_, _var); + auto res = Parser::read(*r_, _var); - if (res) { - move_to(&(rfl::get<_i>(*tuple_)), &(*res)); - } else { - *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", - _i, std::move(res).error()); - } - return; + if (res) { + move_to(&(rfl::get<_i>(*tuple_)), &(*res)); + } else { + *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", + _i, std::move(res).error()); } + return; } template From c9af2b8068d25c2fb3202da2f9ce2e7d9843e6c2 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 18:22:17 +0100 Subject: [PATCH 03/19] Added read_all to the method --- include/rfl/json/Reader.hpp | 74 ++++++++++++++--------- include/rfl/parsing/TupleParser.hpp | 4 ++ include/rfl/parsing/TupleReader.hpp | 50 ++++++++++----- include/rfl/parsing/supports_read_all.hpp | 17 ++++++ 4 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 include/rfl/parsing/supports_read_all.hpp diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index fe4c576b..c19f9e9c 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -19,6 +19,7 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/base64.hpp" +#include "../parsing/supports_read_all.hpp" namespace rfl { namespace json { @@ -54,8 +55,9 @@ struct Reader { using InputObjectType = YYJSONInputObject; using InputVarType = YYJSONInputVar; - /// Compile-time flag indicating whether type T has a custom constructor from JSON. - /// If true, the type provides a static from_json_obj() method for custom deserialization. + /// Compile-time flag indicating whether type T has a custom constructor from + /// JSON. If true, the type provides a static from_json_obj() method for + /// custom deserialization. template static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_json_obj(var); }); @@ -63,7 +65,8 @@ struct Reader { /// Retrieves an element from a JSON array by index. /// @param _idx The zero-based index of the element to retrieve /// @param _arr The JSON array to read from - /// @return Result containing the element value, or an error if index is out of bounds + /// @return Result containing the element value, or an error if index is out + /// of bounds rfl::Result get_field_from_array( const size_t _idx, const InputArrayType _arr) const noexcept { const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx)); @@ -76,7 +79,8 @@ struct Reader { /// Retrieves a field from a JSON object by name. /// @param _name The name of the field to retrieve /// @param _obj The JSON object to read from - /// @return Result containing the field value, or an error if field is not found + /// @return Result containing the field value, or an error if field is not + /// found rfl::Result get_field_from_object( const std::string& _name, const InputObjectType _obj) const noexcept { const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str())); @@ -95,34 +99,44 @@ struct Reader { /// Reads all elements from a JSON array using a provided array reader. /// The array reader's read() method is called for each element in the array. - /// @tparam ArrayReader Type that provides a read() method for processing elements + /// @tparam ArrayReader Type that provides a read() method for processing + /// elements /// @param _array_reader The reader object used to process each array element /// @param _arr The JSON array to read from /// @return std::nullopt on success, or an Error if reading fails template std::optional read_array(const ArrayReader& _array_reader, const InputArrayType& _arr) const noexcept { - const auto size = yyjson_arr_size(_arr.val_); - if constexpr (requires { _array_reader.reserve(size); }) { - if (auto err = _array_reader.reserve(size)) { - return err; + if constexpr (parsing::supports_read_all) { + return [&](std::integer_sequence) { + return _array_reader.read_all(get_field_from_array(_is, _arr)...); + }(std::make_index_sequence()); + + } else { + const auto size = yyjson_arr_size(_arr.val_); + if constexpr (requires { _array_reader.reserve(size); }) { + if (auto err = _array_reader.reserve(size)) { + return err; + } } - } - yyjson_val* val; - yyjson_arr_iter iter; - yyjson_arr_iter_init(_arr.val_, &iter); - while ((val = yyjson_arr_iter_next(&iter))) { - auto err = _array_reader.read(InputVarType(val)); - if (err) { - return err; + yyjson_val* val; + yyjson_arr_iter iter; + yyjson_arr_iter_init(_arr.val_, &iter); + while ((val = yyjson_arr_iter_next(&iter))) { + auto err = _array_reader.read(InputVarType(val)); + if (err) { + return err; + } } + return std::nullopt; } - return std::nullopt; } - /// Reads all key-value pairs from a JSON object using a provided object reader. - /// The object reader's read() method is called for each key-value pair. - /// @tparam ObjectReader Type that provides a read() method for processing key-value pairs + /// Reads all key-value pairs from a JSON object using a provided object + /// reader. The object reader's read() method is called for each key-value + /// pair. + /// @tparam ObjectReader Type that provides a read() method for processing + /// key-value pairs /// @param _object_reader The reader object used to process each field /// @param _obj The JSON object to read from /// @return std::nullopt on success, or an Error if reading fails @@ -141,10 +155,12 @@ struct Reader { } /// Converts a JSON value to a basic C++ type. - /// Supports strings, booleans, floating-point numbers, and integers (signed/unsigned). + /// Supports strings, booleans, floating-point numbers, and integers + /// (signed/unsigned). /// @tparam T The target C++ type /// @param _var The JSON value to convert - /// @return Result containing the converted value, or an error if conversion fails + /// @return Result containing the converted value, or an error if conversion + /// fails template rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { @@ -179,7 +195,8 @@ struct Reader { return error("Could not cast to uint."); } const auto value = yyjson_get_uint(_var.val_); - if (value > static_cast(std::numeric_limits>::max())) { + if (value > static_cast( + std::numeric_limits>::max())) { return error("Integer is out of range for the target type."); } return static_cast(value); @@ -212,7 +229,8 @@ struct Reader { /// Converts a JSON value to an object. /// @param _var The JSON value to convert - /// @return Result containing the object, or an error if value is not an object + /// @return Result containing the object, or an error if value is not an + /// object rfl::Result to_object( const InputVarType _var) const noexcept { if (!yyjson_is_obj(_var.val_)) { @@ -222,10 +240,12 @@ struct Reader { } /// Uses a type's custom constructor to deserialize from JSON. - /// Calls the type's static from_json_obj() method for custom deserialization logic. + /// Calls the type's static from_json_obj() method for custom deserialization + /// logic. /// @tparam T The type to construct, must have a from_json_obj() static method /// @param _var The JSON value to deserialize from - /// @return Result containing the constructed object, or an error if construction fails + /// @return Result containing the constructed object, or an error if + /// construction fails template rfl::Result use_custom_constructor( const InputVarType _var) const noexcept { diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 0e3655df..0f52778a 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -19,6 +19,7 @@ #include "schemaful/IsSchemafulWriter.hpp" #include "schemaful/tuple_to_named_tuple.hpp" #include "schemaful/tuple_to_named_tuple_t.hpp" +#include "supports_read_all.hpp" namespace rfl::parsing { @@ -58,6 +59,9 @@ struct TupleParser { const auto tuple_reader = TupleReader(&_r, ptr); + static_assert( + supports_read_all>, + "Reader must support read_all()"); auto err = _r.read_array(tuple_reader, _arr); if (err) { call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 47f726fb..ef4697ea 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -69,26 +69,39 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); - if (!err) { - ++num_set_; - } + (read_impl<_i, /*_check_num_set=*/true>(_var, &err) || ...); }(std::make_integer_sequence()); return err; } - /*template - requires((sizeof...(Args) == size_) && ... && - std::is_same_v, Args>) + /** + * @brief Reads all variables from the input. + * + * @param _vars The input variables to read from. + * @return An optional error. + */ + template + requires(true && ... && std::is_same_v, Args>) std::optional read_all(const Args&... _vars) const { - std::optional err; - [&](std::integer_sequence) { - (read_impl<_i>(_vars, &err), ...); - if (!err) { - ++num_set_; + static_assert(sizeof...(Args) == size_, + "The number of input variables must match the number of " + "fields in the tuple."); + return [&]( + std::integer_sequence) -> std::optional { + try { + std::optional err; + (_vars + .transform([&](auto&& _v) { + return read_impl<_i, /*_check_num_set=*/false>(_v, &err); + }) + .value_or(false) && + ...); + return err; + } catch (const std::exception& e) { + return Error{e.what()}; } }(std::make_integer_sequence()); - }*/ + } /** * @brief Returns the number of fields in the tuple. @@ -137,10 +150,14 @@ class TupleReader { } template - void read_impl(const InputVarType& _var, std::optional* _err) const { + bool read_impl(const InputVarType& _var, std::optional* _err) const { if constexpr (_check_num_set) { if (num_set_ != _i) { - return; + return false; + } + } else { + if (*_err) { + return false; } } using CurrentType = @@ -150,11 +167,12 @@ class TupleReader { 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; + return true; } template diff --git a/include/rfl/parsing/supports_read_all.hpp b/include/rfl/parsing/supports_read_all.hpp new file mode 100644 index 00000000..81889d2c --- /dev/null +++ b/include/rfl/parsing/supports_read_all.hpp @@ -0,0 +1,17 @@ +#ifndef RFL_PARSING_SUPPORTS_READ_ALL_HPP_ +#define RFL_PARSING_SUPPORTS_READ_ALL_HPP_ + +#include +#include + +namespace rfl::parsing { + +template +concept supports_read_all = requires(T t) { + { T::size() } -> std::convertible_to; + { t.read_all() } -> std::convertible_to>; +}; + +} // namespace rfl::parsing + +#endif From 711acfe00034f456f8212639215301af40cc88ee Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 18:22:38 +0100 Subject: [PATCH 04/19] Revert "Added read_all to the method" This reverts commit c9af2b8068d25c2fb3202da2f9ce2e7d9843e6c2. --- include/rfl/json/Reader.hpp | 74 +++++++++-------------- include/rfl/parsing/TupleParser.hpp | 4 -- include/rfl/parsing/TupleReader.hpp | 50 +++++---------- include/rfl/parsing/supports_read_all.hpp | 17 ------ 4 files changed, 43 insertions(+), 102 deletions(-) delete mode 100644 include/rfl/parsing/supports_read_all.hpp diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index c19f9e9c..fe4c576b 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -19,7 +19,6 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/base64.hpp" -#include "../parsing/supports_read_all.hpp" namespace rfl { namespace json { @@ -55,9 +54,8 @@ struct Reader { using InputObjectType = YYJSONInputObject; using InputVarType = YYJSONInputVar; - /// Compile-time flag indicating whether type T has a custom constructor from - /// JSON. If true, the type provides a static from_json_obj() method for - /// custom deserialization. + /// Compile-time flag indicating whether type T has a custom constructor from JSON. + /// If true, the type provides a static from_json_obj() method for custom deserialization. template static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_json_obj(var); }); @@ -65,8 +63,7 @@ struct Reader { /// Retrieves an element from a JSON array by index. /// @param _idx The zero-based index of the element to retrieve /// @param _arr The JSON array to read from - /// @return Result containing the element value, or an error if index is out - /// of bounds + /// @return Result containing the element value, or an error if index is out of bounds rfl::Result get_field_from_array( const size_t _idx, const InputArrayType _arr) const noexcept { const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx)); @@ -79,8 +76,7 @@ struct Reader { /// Retrieves a field from a JSON object by name. /// @param _name The name of the field to retrieve /// @param _obj The JSON object to read from - /// @return Result containing the field value, or an error if field is not - /// found + /// @return Result containing the field value, or an error if field is not found rfl::Result get_field_from_object( const std::string& _name, const InputObjectType _obj) const noexcept { const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str())); @@ -99,44 +95,34 @@ struct Reader { /// Reads all elements from a JSON array using a provided array reader. /// The array reader's read() method is called for each element in the array. - /// @tparam ArrayReader Type that provides a read() method for processing - /// elements + /// @tparam ArrayReader Type that provides a read() method for processing elements /// @param _array_reader The reader object used to process each array element /// @param _arr The JSON array to read from /// @return std::nullopt on success, or an Error if reading fails template std::optional read_array(const ArrayReader& _array_reader, const InputArrayType& _arr) const noexcept { - if constexpr (parsing::supports_read_all) { - return [&](std::integer_sequence) { - return _array_reader.read_all(get_field_from_array(_is, _arr)...); - }(std::make_index_sequence()); - - } else { - const auto size = yyjson_arr_size(_arr.val_); - if constexpr (requires { _array_reader.reserve(size); }) { - if (auto err = _array_reader.reserve(size)) { - return err; - } + const auto size = yyjson_arr_size(_arr.val_); + if constexpr (requires { _array_reader.reserve(size); }) { + if (auto err = _array_reader.reserve(size)) { + return err; } - yyjson_val* val; - yyjson_arr_iter iter; - yyjson_arr_iter_init(_arr.val_, &iter); - while ((val = yyjson_arr_iter_next(&iter))) { - auto err = _array_reader.read(InputVarType(val)); - if (err) { - return err; - } + } + yyjson_val* val; + yyjson_arr_iter iter; + yyjson_arr_iter_init(_arr.val_, &iter); + while ((val = yyjson_arr_iter_next(&iter))) { + auto err = _array_reader.read(InputVarType(val)); + if (err) { + return err; } - return std::nullopt; } + return std::nullopt; } - /// Reads all key-value pairs from a JSON object using a provided object - /// reader. The object reader's read() method is called for each key-value - /// pair. - /// @tparam ObjectReader Type that provides a read() method for processing - /// key-value pairs + /// Reads all key-value pairs from a JSON object using a provided object reader. + /// The object reader's read() method is called for each key-value pair. + /// @tparam ObjectReader Type that provides a read() method for processing key-value pairs /// @param _object_reader The reader object used to process each field /// @param _obj The JSON object to read from /// @return std::nullopt on success, or an Error if reading fails @@ -155,12 +141,10 @@ struct Reader { } /// Converts a JSON value to a basic C++ type. - /// Supports strings, booleans, floating-point numbers, and integers - /// (signed/unsigned). + /// Supports strings, booleans, floating-point numbers, and integers (signed/unsigned). /// @tparam T The target C++ type /// @param _var The JSON value to convert - /// @return Result containing the converted value, or an error if conversion - /// fails + /// @return Result containing the converted value, or an error if conversion fails template rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { @@ -195,8 +179,7 @@ struct Reader { return error("Could not cast to uint."); } const auto value = yyjson_get_uint(_var.val_); - if (value > static_cast( - std::numeric_limits>::max())) { + if (value > static_cast(std::numeric_limits>::max())) { return error("Integer is out of range for the target type."); } return static_cast(value); @@ -229,8 +212,7 @@ struct Reader { /// Converts a JSON value to an object. /// @param _var The JSON value to convert - /// @return Result containing the object, or an error if value is not an - /// object + /// @return Result containing the object, or an error if value is not an object rfl::Result to_object( const InputVarType _var) const noexcept { if (!yyjson_is_obj(_var.val_)) { @@ -240,12 +222,10 @@ struct Reader { } /// Uses a type's custom constructor to deserialize from JSON. - /// Calls the type's static from_json_obj() method for custom deserialization - /// logic. + /// Calls the type's static from_json_obj() method for custom deserialization logic. /// @tparam T The type to construct, must have a from_json_obj() static method /// @param _var The JSON value to deserialize from - /// @return Result containing the constructed object, or an error if - /// construction fails + /// @return Result containing the constructed object, or an error if construction fails template rfl::Result use_custom_constructor( const InputVarType _var) const noexcept { diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 0f52778a..0e3655df 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -19,7 +19,6 @@ #include "schemaful/IsSchemafulWriter.hpp" #include "schemaful/tuple_to_named_tuple.hpp" #include "schemaful/tuple_to_named_tuple_t.hpp" -#include "supports_read_all.hpp" namespace rfl::parsing { @@ -59,9 +58,6 @@ struct TupleParser { const auto tuple_reader = TupleReader(&_r, ptr); - static_assert( - supports_read_all>, - "Reader must support read_all()"); auto err = _r.read_array(tuple_reader, _arr); if (err) { call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index ef4697ea..47f726fb 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -69,39 +69,26 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_i, /*_check_num_set=*/true>(_var, &err) || ...); + (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); + if (!err) { + ++num_set_; + } }(std::make_integer_sequence()); return err; } - /** - * @brief Reads all variables from the input. - * - * @param _vars The input variables to read from. - * @return An optional error. - */ - template - requires(true && ... && std::is_same_v, Args>) + /*template + requires((sizeof...(Args) == size_) && ... && + std::is_same_v, Args>) std::optional read_all(const Args&... _vars) const { - static_assert(sizeof...(Args) == size_, - "The number of input variables must match the number of " - "fields in the tuple."); - return [&]( - std::integer_sequence) -> std::optional { - try { - std::optional err; - (_vars - .transform([&](auto&& _v) { - return read_impl<_i, /*_check_num_set=*/false>(_v, &err); - }) - .value_or(false) && - ...); - return err; - } catch (const std::exception& e) { - return Error{e.what()}; + std::optional err; + [&](std::integer_sequence) { + (read_impl<_i>(_vars, &err), ...); + if (!err) { + ++num_set_; } }(std::make_integer_sequence()); - } + }*/ /** * @brief Returns the number of fields in the tuple. @@ -150,14 +137,10 @@ class TupleReader { } template - bool read_impl(const InputVarType& _var, std::optional* _err) const { + void read_impl(const InputVarType& _var, std::optional* _err) const { if constexpr (_check_num_set) { if (num_set_ != _i) { - return false; - } - } else { - if (*_err) { - return false; + return; } } using CurrentType = @@ -167,12 +150,11 @@ class TupleReader { 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 true; + return; } template diff --git a/include/rfl/parsing/supports_read_all.hpp b/include/rfl/parsing/supports_read_all.hpp deleted file mode 100644 index 81889d2c..00000000 --- a/include/rfl/parsing/supports_read_all.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef RFL_PARSING_SUPPORTS_READ_ALL_HPP_ -#define RFL_PARSING_SUPPORTS_READ_ALL_HPP_ - -#include -#include - -namespace rfl::parsing { - -template -concept supports_read_all = requires(T t) { - { T::size() } -> std::convertible_to; - { t.read_all() } -> std::convertible_to>; -}; - -} // namespace rfl::parsing - -#endif From 6253406224b2530cf00daf950da6974fdd86945b Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 18:31:10 +0100 Subject: [PATCH 05/19] Reapply "Added read_all to the method" This reverts commit 711acfe00034f456f8212639215301af40cc88ee. --- include/rfl/json/Reader.hpp | 74 ++++++++++++++--------- include/rfl/parsing/TupleParser.hpp | 4 ++ include/rfl/parsing/TupleReader.hpp | 50 ++++++++++----- include/rfl/parsing/supports_read_all.hpp | 17 ++++++ 4 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 include/rfl/parsing/supports_read_all.hpp diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index fe4c576b..c19f9e9c 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -19,6 +19,7 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/base64.hpp" +#include "../parsing/supports_read_all.hpp" namespace rfl { namespace json { @@ -54,8 +55,9 @@ struct Reader { using InputObjectType = YYJSONInputObject; using InputVarType = YYJSONInputVar; - /// Compile-time flag indicating whether type T has a custom constructor from JSON. - /// If true, the type provides a static from_json_obj() method for custom deserialization. + /// Compile-time flag indicating whether type T has a custom constructor from + /// JSON. If true, the type provides a static from_json_obj() method for + /// custom deserialization. template static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_json_obj(var); }); @@ -63,7 +65,8 @@ struct Reader { /// Retrieves an element from a JSON array by index. /// @param _idx The zero-based index of the element to retrieve /// @param _arr The JSON array to read from - /// @return Result containing the element value, or an error if index is out of bounds + /// @return Result containing the element value, or an error if index is out + /// of bounds rfl::Result get_field_from_array( const size_t _idx, const InputArrayType _arr) const noexcept { const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx)); @@ -76,7 +79,8 @@ struct Reader { /// Retrieves a field from a JSON object by name. /// @param _name The name of the field to retrieve /// @param _obj The JSON object to read from - /// @return Result containing the field value, or an error if field is not found + /// @return Result containing the field value, or an error if field is not + /// found rfl::Result get_field_from_object( const std::string& _name, const InputObjectType _obj) const noexcept { const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str())); @@ -95,34 +99,44 @@ struct Reader { /// Reads all elements from a JSON array using a provided array reader. /// The array reader's read() method is called for each element in the array. - /// @tparam ArrayReader Type that provides a read() method for processing elements + /// @tparam ArrayReader Type that provides a read() method for processing + /// elements /// @param _array_reader The reader object used to process each array element /// @param _arr The JSON array to read from /// @return std::nullopt on success, or an Error if reading fails template std::optional read_array(const ArrayReader& _array_reader, const InputArrayType& _arr) const noexcept { - const auto size = yyjson_arr_size(_arr.val_); - if constexpr (requires { _array_reader.reserve(size); }) { - if (auto err = _array_reader.reserve(size)) { - return err; + if constexpr (parsing::supports_read_all) { + return [&](std::integer_sequence) { + return _array_reader.read_all(get_field_from_array(_is, _arr)...); + }(std::make_index_sequence()); + + } else { + const auto size = yyjson_arr_size(_arr.val_); + if constexpr (requires { _array_reader.reserve(size); }) { + if (auto err = _array_reader.reserve(size)) { + return err; + } } - } - yyjson_val* val; - yyjson_arr_iter iter; - yyjson_arr_iter_init(_arr.val_, &iter); - while ((val = yyjson_arr_iter_next(&iter))) { - auto err = _array_reader.read(InputVarType(val)); - if (err) { - return err; + yyjson_val* val; + yyjson_arr_iter iter; + yyjson_arr_iter_init(_arr.val_, &iter); + while ((val = yyjson_arr_iter_next(&iter))) { + auto err = _array_reader.read(InputVarType(val)); + if (err) { + return err; + } } + return std::nullopt; } - return std::nullopt; } - /// Reads all key-value pairs from a JSON object using a provided object reader. - /// The object reader's read() method is called for each key-value pair. - /// @tparam ObjectReader Type that provides a read() method for processing key-value pairs + /// Reads all key-value pairs from a JSON object using a provided object + /// reader. The object reader's read() method is called for each key-value + /// pair. + /// @tparam ObjectReader Type that provides a read() method for processing + /// key-value pairs /// @param _object_reader The reader object used to process each field /// @param _obj The JSON object to read from /// @return std::nullopt on success, or an Error if reading fails @@ -141,10 +155,12 @@ struct Reader { } /// Converts a JSON value to a basic C++ type. - /// Supports strings, booleans, floating-point numbers, and integers (signed/unsigned). + /// Supports strings, booleans, floating-point numbers, and integers + /// (signed/unsigned). /// @tparam T The target C++ type /// @param _var The JSON value to convert - /// @return Result containing the converted value, or an error if conversion fails + /// @return Result containing the converted value, or an error if conversion + /// fails template rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { @@ -179,7 +195,8 @@ struct Reader { return error("Could not cast to uint."); } const auto value = yyjson_get_uint(_var.val_); - if (value > static_cast(std::numeric_limits>::max())) { + if (value > static_cast( + std::numeric_limits>::max())) { return error("Integer is out of range for the target type."); } return static_cast(value); @@ -212,7 +229,8 @@ struct Reader { /// Converts a JSON value to an object. /// @param _var The JSON value to convert - /// @return Result containing the object, or an error if value is not an object + /// @return Result containing the object, or an error if value is not an + /// object rfl::Result to_object( const InputVarType _var) const noexcept { if (!yyjson_is_obj(_var.val_)) { @@ -222,10 +240,12 @@ struct Reader { } /// Uses a type's custom constructor to deserialize from JSON. - /// Calls the type's static from_json_obj() method for custom deserialization logic. + /// Calls the type's static from_json_obj() method for custom deserialization + /// logic. /// @tparam T The type to construct, must have a from_json_obj() static method /// @param _var The JSON value to deserialize from - /// @return Result containing the constructed object, or an error if construction fails + /// @return Result containing the constructed object, or an error if + /// construction fails template rfl::Result use_custom_constructor( const InputVarType _var) const noexcept { diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 0e3655df..0f52778a 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -19,6 +19,7 @@ #include "schemaful/IsSchemafulWriter.hpp" #include "schemaful/tuple_to_named_tuple.hpp" #include "schemaful/tuple_to_named_tuple_t.hpp" +#include "supports_read_all.hpp" namespace rfl::parsing { @@ -58,6 +59,9 @@ struct TupleParser { const auto tuple_reader = TupleReader(&_r, ptr); + static_assert( + supports_read_all>, + "Reader must support read_all()"); auto err = _r.read_array(tuple_reader, _arr); if (err) { call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 47f726fb..ef4697ea 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -69,26 +69,39 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); - if (!err) { - ++num_set_; - } + (read_impl<_i, /*_check_num_set=*/true>(_var, &err) || ...); }(std::make_integer_sequence()); return err; } - /*template - requires((sizeof...(Args) == size_) && ... && - std::is_same_v, Args>) + /** + * @brief Reads all variables from the input. + * + * @param _vars The input variables to read from. + * @return An optional error. + */ + template + requires(true && ... && std::is_same_v, Args>) std::optional read_all(const Args&... _vars) const { - std::optional err; - [&](std::integer_sequence) { - (read_impl<_i>(_vars, &err), ...); - if (!err) { - ++num_set_; + static_assert(sizeof...(Args) == size_, + "The number of input variables must match the number of " + "fields in the tuple."); + return [&]( + std::integer_sequence) -> std::optional { + try { + std::optional err; + (_vars + .transform([&](auto&& _v) { + return read_impl<_i, /*_check_num_set=*/false>(_v, &err); + }) + .value_or(false) && + ...); + return err; + } catch (const std::exception& e) { + return Error{e.what()}; } }(std::make_integer_sequence()); - }*/ + } /** * @brief Returns the number of fields in the tuple. @@ -137,10 +150,14 @@ class TupleReader { } template - void read_impl(const InputVarType& _var, std::optional* _err) const { + bool read_impl(const InputVarType& _var, std::optional* _err) const { if constexpr (_check_num_set) { if (num_set_ != _i) { - return; + return false; + } + } else { + if (*_err) { + return false; } } using CurrentType = @@ -150,11 +167,12 @@ class TupleReader { 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; + return true; } template diff --git a/include/rfl/parsing/supports_read_all.hpp b/include/rfl/parsing/supports_read_all.hpp new file mode 100644 index 00000000..81889d2c --- /dev/null +++ b/include/rfl/parsing/supports_read_all.hpp @@ -0,0 +1,17 @@ +#ifndef RFL_PARSING_SUPPORTS_READ_ALL_HPP_ +#define RFL_PARSING_SUPPORTS_READ_ALL_HPP_ + +#include +#include + +namespace rfl::parsing { + +template +concept supports_read_all = requires(T t) { + { T::size() } -> std::convertible_to; + { t.read_all() } -> std::convertible_to>; +}; + +} // namespace rfl::parsing + +#endif From 53c99e013d13c1d929caf2ac704c1408897d4fde Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 18:36:38 +0100 Subject: [PATCH 06/19] Revert "Reapply "Added read_all to the method"" This reverts commit 6253406224b2530cf00daf950da6974fdd86945b. --- include/rfl/json/Reader.hpp | 74 +++++++++-------------- include/rfl/parsing/TupleParser.hpp | 4 -- include/rfl/parsing/TupleReader.hpp | 50 +++++---------- include/rfl/parsing/supports_read_all.hpp | 17 ------ 4 files changed, 43 insertions(+), 102 deletions(-) delete mode 100644 include/rfl/parsing/supports_read_all.hpp diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index c19f9e9c..fe4c576b 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -19,7 +19,6 @@ #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/base64.hpp" -#include "../parsing/supports_read_all.hpp" namespace rfl { namespace json { @@ -55,9 +54,8 @@ struct Reader { using InputObjectType = YYJSONInputObject; using InputVarType = YYJSONInputVar; - /// Compile-time flag indicating whether type T has a custom constructor from - /// JSON. If true, the type provides a static from_json_obj() method for - /// custom deserialization. + /// Compile-time flag indicating whether type T has a custom constructor from JSON. + /// If true, the type provides a static from_json_obj() method for custom deserialization. template static constexpr bool has_custom_constructor = (requires(InputVarType var) { T::from_json_obj(var); }); @@ -65,8 +63,7 @@ struct Reader { /// Retrieves an element from a JSON array by index. /// @param _idx The zero-based index of the element to retrieve /// @param _arr The JSON array to read from - /// @return Result containing the element value, or an error if index is out - /// of bounds + /// @return Result containing the element value, or an error if index is out of bounds rfl::Result get_field_from_array( const size_t _idx, const InputArrayType _arr) const noexcept { const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx)); @@ -79,8 +76,7 @@ struct Reader { /// Retrieves a field from a JSON object by name. /// @param _name The name of the field to retrieve /// @param _obj The JSON object to read from - /// @return Result containing the field value, or an error if field is not - /// found + /// @return Result containing the field value, or an error if field is not found rfl::Result get_field_from_object( const std::string& _name, const InputObjectType _obj) const noexcept { const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str())); @@ -99,44 +95,34 @@ struct Reader { /// Reads all elements from a JSON array using a provided array reader. /// The array reader's read() method is called for each element in the array. - /// @tparam ArrayReader Type that provides a read() method for processing - /// elements + /// @tparam ArrayReader Type that provides a read() method for processing elements /// @param _array_reader The reader object used to process each array element /// @param _arr The JSON array to read from /// @return std::nullopt on success, or an Error if reading fails template std::optional read_array(const ArrayReader& _array_reader, const InputArrayType& _arr) const noexcept { - if constexpr (parsing::supports_read_all) { - return [&](std::integer_sequence) { - return _array_reader.read_all(get_field_from_array(_is, _arr)...); - }(std::make_index_sequence()); - - } else { - const auto size = yyjson_arr_size(_arr.val_); - if constexpr (requires { _array_reader.reserve(size); }) { - if (auto err = _array_reader.reserve(size)) { - return err; - } + const auto size = yyjson_arr_size(_arr.val_); + if constexpr (requires { _array_reader.reserve(size); }) { + if (auto err = _array_reader.reserve(size)) { + return err; } - yyjson_val* val; - yyjson_arr_iter iter; - yyjson_arr_iter_init(_arr.val_, &iter); - while ((val = yyjson_arr_iter_next(&iter))) { - auto err = _array_reader.read(InputVarType(val)); - if (err) { - return err; - } + } + yyjson_val* val; + yyjson_arr_iter iter; + yyjson_arr_iter_init(_arr.val_, &iter); + while ((val = yyjson_arr_iter_next(&iter))) { + auto err = _array_reader.read(InputVarType(val)); + if (err) { + return err; } - return std::nullopt; } + return std::nullopt; } - /// Reads all key-value pairs from a JSON object using a provided object - /// reader. The object reader's read() method is called for each key-value - /// pair. - /// @tparam ObjectReader Type that provides a read() method for processing - /// key-value pairs + /// Reads all key-value pairs from a JSON object using a provided object reader. + /// The object reader's read() method is called for each key-value pair. + /// @tparam ObjectReader Type that provides a read() method for processing key-value pairs /// @param _object_reader The reader object used to process each field /// @param _obj The JSON object to read from /// @return std::nullopt on success, or an Error if reading fails @@ -155,12 +141,10 @@ struct Reader { } /// Converts a JSON value to a basic C++ type. - /// Supports strings, booleans, floating-point numbers, and integers - /// (signed/unsigned). + /// Supports strings, booleans, floating-point numbers, and integers (signed/unsigned). /// @tparam T The target C++ type /// @param _var The JSON value to convert - /// @return Result containing the converted value, or an error if conversion - /// fails + /// @return Result containing the converted value, or an error if conversion fails template rfl::Result to_basic_type(const InputVarType _var) const noexcept { if constexpr (std::is_same, std::string>()) { @@ -195,8 +179,7 @@ struct Reader { return error("Could not cast to uint."); } const auto value = yyjson_get_uint(_var.val_); - if (value > static_cast( - std::numeric_limits>::max())) { + if (value > static_cast(std::numeric_limits>::max())) { return error("Integer is out of range for the target type."); } return static_cast(value); @@ -229,8 +212,7 @@ struct Reader { /// Converts a JSON value to an object. /// @param _var The JSON value to convert - /// @return Result containing the object, or an error if value is not an - /// object + /// @return Result containing the object, or an error if value is not an object rfl::Result to_object( const InputVarType _var) const noexcept { if (!yyjson_is_obj(_var.val_)) { @@ -240,12 +222,10 @@ struct Reader { } /// Uses a type's custom constructor to deserialize from JSON. - /// Calls the type's static from_json_obj() method for custom deserialization - /// logic. + /// Calls the type's static from_json_obj() method for custom deserialization logic. /// @tparam T The type to construct, must have a from_json_obj() static method /// @param _var The JSON value to deserialize from - /// @return Result containing the constructed object, or an error if - /// construction fails + /// @return Result containing the constructed object, or an error if construction fails template rfl::Result use_custom_constructor( const InputVarType _var) const noexcept { diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 0f52778a..0e3655df 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -19,7 +19,6 @@ #include "schemaful/IsSchemafulWriter.hpp" #include "schemaful/tuple_to_named_tuple.hpp" #include "schemaful/tuple_to_named_tuple_t.hpp" -#include "supports_read_all.hpp" namespace rfl::parsing { @@ -59,9 +58,6 @@ struct TupleParser { const auto tuple_reader = TupleReader(&_r, ptr); - static_assert( - supports_read_all>, - "Reader must support read_all()"); auto err = _r.read_array(tuple_reader, _arr); if (err) { call_destructors_on_tuple_where_necessary(tuple_reader.num_set(), diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index ef4697ea..47f726fb 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -69,39 +69,26 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_i, /*_check_num_set=*/true>(_var, &err) || ...); + (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); + if (!err) { + ++num_set_; + } }(std::make_integer_sequence()); return err; } - /** - * @brief Reads all variables from the input. - * - * @param _vars The input variables to read from. - * @return An optional error. - */ - template - requires(true && ... && std::is_same_v, Args>) + /*template + requires((sizeof...(Args) == size_) && ... && + std::is_same_v, Args>) std::optional read_all(const Args&... _vars) const { - static_assert(sizeof...(Args) == size_, - "The number of input variables must match the number of " - "fields in the tuple."); - return [&]( - std::integer_sequence) -> std::optional { - try { - std::optional err; - (_vars - .transform([&](auto&& _v) { - return read_impl<_i, /*_check_num_set=*/false>(_v, &err); - }) - .value_or(false) && - ...); - return err; - } catch (const std::exception& e) { - return Error{e.what()}; + std::optional err; + [&](std::integer_sequence) { + (read_impl<_i>(_vars, &err), ...); + if (!err) { + ++num_set_; } }(std::make_integer_sequence()); - } + }*/ /** * @brief Returns the number of fields in the tuple. @@ -150,14 +137,10 @@ class TupleReader { } template - bool read_impl(const InputVarType& _var, std::optional* _err) const { + void read_impl(const InputVarType& _var, std::optional* _err) const { if constexpr (_check_num_set) { if (num_set_ != _i) { - return false; - } - } else { - if (*_err) { - return false; + return; } } using CurrentType = @@ -167,12 +150,11 @@ class TupleReader { 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 true; + return; } template diff --git a/include/rfl/parsing/supports_read_all.hpp b/include/rfl/parsing/supports_read_all.hpp deleted file mode 100644 index 81889d2c..00000000 --- a/include/rfl/parsing/supports_read_all.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef RFL_PARSING_SUPPORTS_READ_ALL_HPP_ -#define RFL_PARSING_SUPPORTS_READ_ALL_HPP_ - -#include -#include - -namespace rfl::parsing { - -template -concept supports_read_all = requires(T t) { - { T::size() } -> std::convertible_to; - { t.read_all() } -> std::convertible_to>; -}; - -} // namespace rfl::parsing - -#endif From e0d374c695b49a330a44fe7803f40c07338a674b Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sat, 21 Nov 2026 18:49:56 +0100 Subject: [PATCH 07/19] Remove recursive implementation from fields --- include/rfl/parsing/TupleReader.hpp | 82 ++++++++++++----------------- 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 47f726fb..f5cd4419 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -43,7 +43,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; } @@ -68,8 +70,8 @@ class TupleReader { return Error(stream.str()); } std::optional err; - [&](std::integer_sequence) { - (read_impl<_i, /*_check_num_set=*/true>(_var, &err), ...); + [&](std::integer_sequence) { + (read_impl<_is>(_var, &err), ...); if (!err) { ++num_set_; } @@ -77,19 +79,6 @@ class TupleReader { return err; } - /*template - requires((sizeof...(Args) == size_) && ... && - std::is_same_v, Args>) - std::optional read_all(const Args&... _vars) const { - std::optional err; - [&](std::integer_sequence) { - (read_impl<_i>(_vars, &err), ...); - if (!err) { - ++num_set_; - } - }(std::make_integer_sequence()); - }*/ - /** * @brief Returns the number of fields in the tuple. * @@ -100,48 +89,43 @@ class TupleReader { private: 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 + template void read_impl(const InputVarType& _var, std::optional* _err) const { - if constexpr (_check_num_set) { - if (num_set_ != _i) { - return; - } + if (num_set_ != _i) { + return; } using CurrentType = std::remove_cvref_t>; From 596e42c1027d606f9ae8ec3775f579a3cb24f648 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 14:36:06 +0100 Subject: [PATCH 08/19] Make read_impl static --- include/rfl/parsing/TupleReader.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index f5cd4419..97210835 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -71,7 +71,7 @@ class TupleReader { } std::optional err; [&](std::integer_sequence) { - (read_impl<_is>(_var, &err), ...); + (read_impl<_is>(*this, _var, &err), ...); if (!err) { ++num_set_; } @@ -123,17 +123,19 @@ class TupleReader { } template - void read_impl(const InputVarType& _var, std::optional* _err) const { - if (num_set_ != _i) { + 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)); + move_to(&(rfl::get<_i>(*_tuple_reader.tuple_)), &(*res)); } else { *_err = wrap_error("Failed to parse field " + std::to_string(_i) + ": ", _i, std::move(res).error()); @@ -142,7 +144,7 @@ class TupleReader { } 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 && From f406f65e3c38451e4225b115f65f489cc8a77db1 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 15:26:42 +0100 Subject: [PATCH 09/19] Removed unneeded default --- include/rfl/parsing/TupleReader.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index 97210835..2de7bed4 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" @@ -87,7 +88,7 @@ class TupleReader { static constexpr size_t size() { return size_; } private: - template + template void handle_missing_fields_impl(std::optional* _err) const noexcept { if (num_set_ == _i) { using CurrentType = From 93f0d66f04b8e3dbc7e3dd46899af98f1f799ecd Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 16:15:25 +0100 Subject: [PATCH 10/19] Use hashing --- include/rfl/parsing/ViewReader.hpp | 43 ++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 84ee4b82..ab7ea9f0 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -84,13 +84,15 @@ class ViewReader { 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(); + static bool is_matching(const auto _current_name_or_index, + size_t _current_hash) { + if constexpr (std::is_integral_v< + std::remove_cvref_t>) { + return _current_name_or_index == i; + } else { + return fnv1a(FieldType::name_.string_view()) == _current_hash && + _current_name_or_index == FieldType::name(); + } } static ErrorDetail::PathSegment to_path_segment(const int _current_index) { @@ -102,17 +104,28 @@ class ViewReader { return std::string(_current_name); } + constexpr static size_t fnv1a(std::string_view _sv) noexcept { + constexpr size_t fnv_offset_basis = 1469598103934665603ULL; + constexpr size_t fnv_prime = 1099511628211ULL; + size_t h = fnv_offset_basis; + for (size_t i = 0; i < _sv.size(); ++i) { + h ^= static_cast(static_cast(_sv[i])); + h *= fnv_prime; + } + return h; + } + template 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) { + bool* _already_assigned, size_t _current_hash) { 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)) { + is_matching(_current_name_or_index, _current_hash)) { std::get(*_found) = true; *_already_assigned = true; auto res = Parser::read(_r, _var); @@ -165,9 +178,17 @@ class ViewReader { std::integer_sequence) { bool already_assigned = false; + size_t current_hash = 0; + if constexpr (std::is_same_v, + std::string_view>) { + current_hash = fnv1a(_current_name_or_index); + } + (assign_if_field_matches(_r, _current_name_or_index, _var, _view, - _errors, _found, _set, &already_assigned), - ...); + _errors, _found, _set, &already_assigned, + current_hash), + ...); if constexpr (ViewType::pos_extra_fields() != -1) { static_assert(!schemaful::IsSchemafulReader, From 29c858cb9ace41221c93722e3404eb9a304220dc Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 16:15:40 +0100 Subject: [PATCH 11/19] Revert "Use hashing" This reverts commit 93f0d66f04b8e3dbc7e3dd46899af98f1f799ecd. --- include/rfl/parsing/ViewReader.hpp | 43 ++++++++---------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index ab7ea9f0..84ee4b82 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -84,15 +84,13 @@ class ViewReader { private: template - static bool is_matching(const auto _current_name_or_index, - size_t _current_hash) { - if constexpr (std::is_integral_v< - std::remove_cvref_t>) { - return _current_name_or_index == i; - } else { - return fnv1a(FieldType::name_.string_view()) == _current_hash && - _current_name_or_index == FieldType::name(); - } + 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(); } static ErrorDetail::PathSegment to_path_segment(const int _current_index) { @@ -104,28 +102,17 @@ class ViewReader { return std::string(_current_name); } - constexpr static size_t fnv1a(std::string_view _sv) noexcept { - constexpr size_t fnv_offset_basis = 1469598103934665603ULL; - constexpr size_t fnv_prime = 1099511628211ULL; - size_t h = fnv_offset_basis; - for (size_t i = 0; i < _sv.size(); ++i) { - h ^= static_cast(static_cast(_sv[i])); - h *= fnv_prime; - } - return h; - } - template 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, size_t _current_hash) { + 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, _current_hash)) { + is_matching(_current_name_or_index)) { std::get(*_found) = true; *_already_assigned = true; auto res = Parser::read(_r, _var); @@ -178,17 +165,9 @@ class ViewReader { std::integer_sequence) { bool already_assigned = false; - size_t current_hash = 0; - if constexpr (std::is_same_v, - std::string_view>) { - current_hash = fnv1a(_current_name_or_index); - } - (assign_if_field_matches(_r, _current_name_or_index, _var, _view, - _errors, _found, _set, &already_assigned, - current_hash), - ...); + _errors, _found, _set, &already_assigned), + ...); if constexpr (ViewType::pos_extra_fields() != -1) { static_assert(!schemaful::IsSchemafulReader, From 9522ed73141bd12a8d202e2743f6572e92420e43 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 19:26:23 +0100 Subject: [PATCH 12/19] More non-recursive implementations --- include/rfl/internal/move_from_tuple.hpp | 61 +++++++++++------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index 0905a567..0b3d394e 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -12,22 +12,22 @@ #include "tup_to_ptr_tuple.hpp" #include "tuple_t.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { -template +template constexpr int calc_flattened_size() { - if constexpr (_i == rfl::tuple_size_v) { - return 0; - } else { + constexpr auto calc_one = + [](std::integral_constant) -> int { using T = std::remove_pointer_t>; if constexpr (is_flatten_field_v) { - return calc_flattened_size>() + - calc_flattened_size(); + return calc_flattened_size>(); } else { - return 1 + calc_flattened_size(); + return 1; } - } + }; + return [&](std::integer_sequence) { + return (calc_one(std::integral_constant{}) + ...); + }(std::make_integer_sequence>()); } // TODO: Non-recursive implementation @@ -61,28 +61,27 @@ auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) { } } -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>; +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>()); } template @@ -92,10 +91,8 @@ auto flatten_array(T* _v) { 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); } template @@ -109,10 +106,9 @@ auto make_tuple_from_element(Array* _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); + 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 @@ -129,7 +125,6 @@ auto move_from_tuple(TupleType&& _t) { return move_from_pointers(pointers); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif From 967049d13bbdec5741edc30ccc815edd9e98cd16 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 20:27:15 +0100 Subject: [PATCH 13/19] Non-recursive implementation for element_index --- include/rfl/internal/element_index.hpp | 30 +++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/include/rfl/internal/element_index.hpp b/include/rfl/internal/element_index.hpp index c02b5a29..6d008c8f 100644 --- a/include/rfl/internal/element_index.hpp +++ b/include/rfl/internal/element_index.hpp @@ -5,23 +5,29 @@ 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; + } } -} +}; 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 From 2a0343918ea0dcfc5b6ea17ab97da837b809cccc Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Sun, 22 Nov 2026 21:36:29 +0100 Subject: [PATCH 14/19] Minor beautification --- include/rfl/internal/VisitTree.hpp | 35 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 19 deletions(-) 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 From b2e476832f9e5b5805b5b61437869199d4e3afdc Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 23 Nov 2026 09:24:16 +0100 Subject: [PATCH 15/19] Fixed headers --- include/rfl/internal/element_index.hpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/rfl/internal/element_index.hpp b/include/rfl/internal/element_index.hpp index 6d008c8f..09085a7b 100644 --- a/include/rfl/internal/element_index.hpp +++ b/include/rfl/internal/element_index.hpp @@ -2,6 +2,7 @@ #define RFL_INTERNAL_ELEMENT_INDEX_HPP_ #include +#include namespace rfl::internal { @@ -18,16 +19,20 @@ struct ElementIndexWrapper { } }; +/** + * 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 [](std::integer_sequence) { + return [](std::integer_sequence) { return ((ElementIndexWrapper, std::remove_cvref_t, _is>{} || ...) || ElementIndexWrapper{}) .index; - }(std::make_integer_sequence()); + }(std::make_integer_sequence()); } } // namespace rfl::internal From f3ce46dece7e9e2c724169183c156c1a6700bf47 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 23 Nov 2026 12:24:40 +0100 Subject: [PATCH 16/19] Use non-recursive version --- include/rfl/internal/has_arrays.hpp | 28 ++++ include/rfl/internal/has_flatten_fields.hpp | 9 +- include/rfl/internal/move_from_tuple.hpp | 175 +++++++++++++++----- include/rfl/internal/tup_to_ptr_tuple.hpp | 12 +- 4 files changed, 167 insertions(+), 57 deletions(-) create mode 100644 include/rfl/internal/has_arrays.hpp diff --git a/include/rfl/internal/has_arrays.hpp b/include/rfl/internal/has_arrays.hpp new file mode 100644 index 00000000..16648207 --- /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 0b3d394e..ae287aba 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -1,68 +1,115 @@ #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::internal { -template -constexpr int calc_flattened_size() { - constexpr auto calc_one = - [](std::integral_constant) -> int { - using T = std::remove_pointer_t>; - if constexpr (is_flatten_field_v) { - return calc_flattened_size>(); - } else { - return 1; - } - }; +/** + * 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 { + return 1; + } +} + +/** + * 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 (calc_one(std::integral_constant{}) + ...); + return (IndptrHelper<_begin>{} + ... + + IndptrHelper>()>{}) + .indptr_; }(std::make_integer_sequence>()); } -// TODO: Non-recursive implementation -template -auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) { - constexpr auto i = sizeof...(Args); - +/** + * 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()); } +/** + * 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) { +auto move_from_pointers(Pointers&& _ptrs) { const auto move_one = [&](std::integral_constant) { using FieldType = tuple_element_t<_i, std::remove_cvref_t>; @@ -84,45 +131,87 @@ auto move_from_pointers(Pointers& _ptrs) { }(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) { 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_)); } +/** + * Flattens C-style arrays in a tuple. + * @param _tup Source tuple. + * @return A flattened tuple. + */ auto flatten_c_arrays(const 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)); + if constexpr (has_flatten_fields()) { + if constexpr (has_arrays()) { + return move_from_pointers(flatten_c_arrays( + unflatten_ptr_tuple(std::move(ptr_tuple)))); + } else { + return move_from_pointers( + unflatten_ptr_tuple(std::move(ptr_tuple))); + } - return move_from_pointers(pointers); + } else { + if constexpr (has_arrays()) { + return move_from_pointers(flatten_c_arrays(std::move(ptr_tuple))); + } else { + // Simplest and most common case: Most structs contain neither flatten + // fields nor C-style arrays. + return move_from_pointers(std::move(ptr_tuple)); + } + } } } // namespace rfl::internal 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 From 8c6935280d8d6fe14619fff74b9b3c0c3a25132f Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 23 Nov 2026 14:33:18 +0100 Subject: [PATCH 17/19] Minor fixes --- include/rfl/internal/has_arrays.hpp | 2 +- include/rfl/internal/move_from_tuple.hpp | 25 ++++-------------------- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/include/rfl/internal/has_arrays.hpp b/include/rfl/internal/has_arrays.hpp index 16648207..89669205 100644 --- a/include/rfl/internal/has_arrays.hpp +++ b/include/rfl/internal/has_arrays.hpp @@ -7,7 +7,7 @@ #include "../Tuple.hpp" #include "is_array.hpp" -namespace rfl ::internal { +namespace rfl::internal { template constexpr bool has_arrays() { diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index ae287aba..c5edf904 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -178,7 +178,7 @@ auto make_tuple_from_element(Array* _arr) { * @param _tup Source tuple. * @return A flattened tuple. */ -auto flatten_c_arrays(const auto& _tup) { +auto flatten_c_arrays(auto&& _tup) { return rfl::apply( [](auto... _v) { return rfl::tuple_cat(make_tuple_from_element(_v)...); }, _tup); @@ -191,27 +191,10 @@ auto flatten_c_arrays(const auto& _tup) { */ template auto move_from_tuple(TupleType&& _t) { - auto ptr_tuple = tup_to_ptr_tuple(_t); using TargetTupleType = tuple_t>; - - if constexpr (has_flatten_fields()) { - if constexpr (has_arrays()) { - return move_from_pointers(flatten_c_arrays( - unflatten_ptr_tuple(std::move(ptr_tuple)))); - } else { - return move_from_pointers( - unflatten_ptr_tuple(std::move(ptr_tuple))); - } - - } else { - if constexpr (has_arrays()) { - return move_from_pointers(flatten_c_arrays(std::move(ptr_tuple))); - } else { - // Simplest and most common case: Most structs contain neither flatten - // fields nor C-style arrays. - return move_from_pointers(std::move(ptr_tuple)); - } - } + auto ptr_tuple = tup_to_ptr_tuple(_t); + return move_from_pointers( + flatten_c_arrays(unflatten_ptr_tuple(ptr_tuple))); } } // namespace rfl::internal From 1ea7c2c410c062fc611251a4f674d98c25f93543 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 23 Nov 2026 14:33:46 +0100 Subject: [PATCH 18/19] Non-recursive implementation of get_meta_fields --- include/rfl/internal/get_meta_fields.hpp | 29 ++++++++++++------------ 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/include/rfl/internal/get_meta_fields.hpp b/include/rfl/internal/get_meta_fields.hpp index 6468210f..49b4804d 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; +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 From d8f541799b91bf5214c57261179b003f080f14f5 Mon Sep 17 00:00:00 2001 From: "Dr. Patrick Urbanke" Date: Mon, 23 Nov 2026 14:35:17 +0100 Subject: [PATCH 19/19] Removed AlreadyExtracted --- include/rfl/internal/get_meta_fields.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rfl/internal/get_meta_fields.hpp b/include/rfl/internal/get_meta_fields.hpp index 49b4804d..34b898c3 100644 --- a/include/rfl/internal/get_meta_fields.hpp +++ b/include/rfl/internal/get_meta_fields.hpp @@ -11,7 +11,7 @@ namespace rfl::internal { -template +template auto get_meta_fields() { const auto get_one = [&](std::integral_constant) { using FieldType = tuple_element_t<_i, typename NamedTupleType::Fields>;