-
Notifications
You must be signed in to change notification settings - Fork 178
Add unified rfl::Settings::with<>() API for format-specific settings #662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a051768
Add unified rfl::Settings::with<>() API for format-specific settings
Centimo 5c17f35
docs: update csv and parquet Settings examples for the new with<>() API
Centimo 98656e1
Fix MSVC C2131 in field_index_from_ptm by using a local object
Centimo 85a08ef
Make ith_field_is_const detect non-const Settings fields
Centimo 0eb58cb
Surface RFL_SETTINGS_OPS constraints at the call site
Centimo 53b3b70
Rename field_type_at to field_value_type_at
Centimo cebcba2
Use local object only on MSVC to avoid GCC 11 literal-type errors
Centimo bef1f8c
Restore legacy with_<field>() setters as [[deprecated]] shims
Centimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| #ifndef RFL_SETTINGS_HPP_ | ||
| #define RFL_SETTINGS_HPP_ | ||
|
|
||
| #include <cstddef> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| #include "Field.hpp" | ||
| #include "internal/StringLiteral.hpp" | ||
| #include "internal/field_index_by_name.hpp" | ||
| #include "internal/field_index_from_ptm.hpp" | ||
| #include "internal/get_field_names.hpp" | ||
| #include "internal/get_ith_field_from_fake_object.hpp" | ||
| #include "replace.hpp" | ||
|
|
||
| namespace rfl::internal { | ||
|
|
||
| template <auto FieldPtr, class ExpectedOwner> | ||
| struct member_ptr_traits : std::false_type {}; | ||
|
|
||
| template <class Owner, class FieldType, FieldType Owner::* FieldPtr, | ||
| class ExpectedOwner> | ||
| struct member_ptr_traits<FieldPtr, ExpectedOwner> | ||
| : std::bool_constant<std::is_same_v<Owner, ExpectedOwner> && | ||
| std::is_const_v<FieldType>> {}; | ||
|
|
||
| template <auto FieldPtr, class ExpectedOwner> | ||
| concept const_member_of = member_ptr_traits<FieldPtr, ExpectedOwner>::value; | ||
|
|
||
| /// Returns a copy of `_obj` with the field designated by `FieldPtr` replaced | ||
| /// by `_value`. Used as the body of the with() method generated by | ||
| /// RFL_SETTINGS_OPS. Field name is recovered through the fake-object path | ||
| /// that the rest of reflect-cpp uses, so MSVC parses it correctly. | ||
| template <class T, auto FieldPtr> | ||
| requires const_member_of<FieldPtr, T> | ||
| T settings_with_replaced( | ||
| const T& _obj, | ||
| std::remove_const_t< | ||
| std::remove_reference_t<decltype(std::declval<T>().*FieldPtr)>> | ||
| _value) { | ||
| constexpr std::size_t I = field_index_v<T, FieldPtr>; | ||
| static_assert( | ||
| I != static_cast<std::size_t>(-1), | ||
| "FieldPtr does not refer to a non-static data member of T. " | ||
| "Make sure you pass &T::field, where field is declared inside T."); | ||
| constexpr auto name = | ||
| get_field_name_str_lit<T, fake_field_ptr_for_name_lookup<T, I>()>(); | ||
| return rfl::replace(_obj, rfl::make_field<name>(std::move(_value))); | ||
| } | ||
|
|
||
| /// Returns a copy of `_obj` with the field with the given Name replaced by | ||
| /// `_value`. Used as the body of the with<"name">() overload generated by | ||
| /// RFL_SETTINGS_OPS. | ||
| template <class T, StringLiteral Name> | ||
| T settings_with_replaced_by_name( | ||
| const T& _obj, | ||
| field_value_type_at_t<T, field_index_by_name_v<T, Name>> _value) { | ||
| constexpr std::size_t I = field_index_by_name_v<T, Name>; | ||
| static_assert(I != static_cast<std::size_t>(-1), | ||
| "No field with the given name exists in T."); | ||
| #ifdef _MSC_VER | ||
| static_assert(ith_field_is_const<T, static_cast<int>(I)>(), | ||
| "Fields in Settings structs must be const."); | ||
| #endif | ||
| return rfl::replace(_obj, rfl::make_field<Name>(std::move(_value))); | ||
| } | ||
|
|
||
| } // namespace rfl::internal | ||
|
|
||
| /// Defines the standard with<&T::field>(value) and with<"field">(value) | ||
| /// accessors inside a settings struct. The struct must be a flat aggregate | ||
| /// (no base classes). Fields should be declared const so the only way to | ||
| /// mutate them is via with(), which returns a new copy with the chosen field | ||
| /// replaced. Place the macro at the end of the struct body, after all data | ||
| /// members. | ||
| /// | ||
| /// Usage: | ||
| /// struct MySettings { | ||
| /// const int some_option = 42; | ||
| /// RFL_SETTINGS_OPS(MySettings) | ||
| /// }; | ||
| /// | ||
| /// auto a = MySettings{}.with<&MySettings::some_option>(100); | ||
| /// auto b = MySettings{}.with<"some_option">(100); | ||
| #define RFL_SETTINGS_OPS(Derived) \ | ||
| template <auto FieldPtr> \ | ||
| requires ::rfl::internal::const_member_of<FieldPtr, Derived> \ | ||
| Derived with(std::remove_const_t<std::remove_reference_t< \ | ||
| decltype(std::declval<Derived>().*FieldPtr)>> \ | ||
| _value) const { \ | ||
| return ::rfl::internal::settings_with_replaced<Derived, FieldPtr>( \ | ||
| *this, std::move(_value)); \ | ||
| } \ | ||
| template <::rfl::internal::StringLiteral Name> \ | ||
| Derived with(::rfl::internal::field_value_type_at_t< \ | ||
| Derived, \ | ||
| ::rfl::internal::field_index_by_name_v<Derived, Name>> \ | ||
| _value) const { \ | ||
| return ::rfl::internal::settings_with_replaced_by_name<Derived, Name>( \ | ||
| *this, std::move(_value)); \ | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #ifndef RFL_INTERNAL_DEPRECATED_WITH_HPP_ | ||
| #define RFL_INTERNAL_DEPRECATED_WITH_HPP_ | ||
|
|
||
| /// Marks a legacy with_<field>() method as deprecated, pointing at the | ||
| /// unified RFL_SETTINGS_OPS-generated with<&T::field>() / with<"field">() | ||
| /// API. Used in csv::Settings and parquet::Settings to keep the old | ||
| /// per-field setter names working for existing users. | ||
| #define RFL_DEPRECATED_WITH(field_name) \ | ||
| [[deprecated("Use .with<&Settings::" #field_name ">(value) or " \ | ||
| ".with<\"" #field_name "\">(value) instead.")]] | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
with<"name">()overload doesn't enforce that fields in aSettingsstruct must beconst, unlike the pointer-to-member overloadwith<&T::field>(). This could lead to mutable settings fields being used, which goes against the design goal of this PR.To ensure consistency and enforce immutability for all
Settingsfields, I suggest adding astatic_assertto check that the field isconst.