diff --git a/docs/named_tuple.md b/docs/named_tuple.md index 0785f92b..be2943d6 100644 --- a/docs/named_tuple.md +++ b/docs/named_tuple.md @@ -114,6 +114,65 @@ person.apply([](Field& f) { }); ``` +### Monadic operations: `.transform` and `.and_then` + +Named tuples also contain compile-time monadic operations. + +`.transform(f)` expects a function `f` of type `Field -> Field`. +`transform` then applies that function to each field of the named tuple. +It can be used to change either the values or the names of the fields, but +not their overall number. + +```cpp +const auto lisa = + Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; + +const auto to_bart = [](auto field) { +if constexpr (decltype(field)::name() == "first_name") { + field = "Bart"; + return field; +} else if constexpr (decltype(field)::name() == "age") { + field = 10; + return field; +} else { + return field; +} +}; + +// bart will now be a named tuple with first_name="Bart", +// last_name="Simpson", age=10 +const auto bart = rfl::to_named_tuple(lisa).transform(to_bart); +``` + +`.and_then(f)` expects a function `f` of type `Field -> NamedTuple`. +`and_then` then applies that function to each field of the named tuple +and finally concatenates the resulting named tuple to form a new named tuple. +Note that the named tuple returned by `f` may be empty. `.and_then(f)` can be used +to change either the values or the names of the fields, and can also affect +their overall number. + +```cpp +const auto lisa = + Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; + +const auto to_bart = [](auto field) { +if constexpr (decltype(field)::name() == "first_name") { + field = "Bart"; + return rfl::make_named_tuple(field); +} else if constexpr (decltype(field)::name() == "age") { + return rfl::make_named_tuple(); +} else { + return rfl::make_named_tuple(field); +} +}; + +// bart will now be a named tuple with first_name="Bart", +// last_name="Simpson". Since we have returned and empty +// named tuple for the field "age", there will be no such +// field in bart. +const auto bart = rfl::to_named_tuple(lisa).and_then(to_bart); +``` + ### `rfl::replace` `rfl::replace` works for `rfl::NamedTuple` as well: diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index e5e77cc2..1edd4d73 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -18,6 +18,13 @@ namespace rfl { /// A named tuple behaves like std::tuple, /// but the fields have explicit names, which /// allows for reflection. +/// IMPORTANT: We have two template specializations. One with fields, one +/// without fields. +template +class NamedTuple; + +// ---------------------------------------------------------------------------- + template class NamedTuple { public: @@ -162,24 +169,51 @@ class NamedTuple { std::forward(_tail)...); } - /// Invokes a callable object once for each field in order. + /// Creates a new named tuple by applying the supplied function to + /// field. The function is expected to return a named tuple itself. template - void apply(F&& f) { - std::apply( - [&f](AFields&&... fields) { - ((f(std::forward(fields))), ...); - }, - fields()); + auto and_then(const F& _f) { + const auto transform_field = [&_f](auto... _fields) { + return std::tuple_cat(_f(std::move(_fields)).fields()...); + }; + const auto to_nt = [](std::tuple&& _tup) { + return NamedTuple(_tup); + }; + auto new_fields = std::apply(transform_field, std::move(fields())); + return to_nt(std::move(new_fields)); + } + + /// Creates a new named tuple by applying the supplied function to + /// field. The function is expected to return a named tuple itself. + template + auto and_then(const F& _f) const { + const auto transform_field = [&_f](auto... _fields) { + return std::tuple_cat(_f(std::move(_fields)).fields()...); + }; + const auto to_nt = [](std::tuple&& _tup) { + return NamedTuple(_tup); + }; + auto new_fields = std::apply(transform_field, std::move(fields())); + return to_nt(std::move(new_fields)); } /// Invokes a callable object once for each field in order. template - void apply(F&& f) const { - std::apply( - [&f](AFields&&... fields) { - ((f(std::forward(fields))), ...); - }, - fields()); + void apply(const F& _f) { + const auto apply_to_field = + [&_f](AFields&&... fields) { + ((_f(std::forward(fields))), ...); + }; + std::apply(apply_to_field, fields()); + } + + /// Invokes a callable object once for each field in order. + template + void apply(const F& _f) const { + const auto apply_to_field = [&_f](const auto&... fields) { + ((_f(fields)), ...); + }; + std::apply(apply_to_field, fields()); } /// Returns a tuple containing the fields. @@ -312,6 +346,34 @@ class NamedTuple { /// Returns the size of the named tuple static constexpr size_t size() { return std::tuple_size_v; } + /// Creates a new named tuple by applying the supplied function to every + /// field. + template + auto transform(const F& _f) { + const auto transform_field = [&_f](auto... fields) { + return std::make_tuple(_f(std::move(fields))...); + }; + const auto to_nt = [](std::tuple&& _tup) { + return NamedTuple(_tup); + }; + auto new_fields = std::apply(transform_field, std::move(fields())); + return to_nt(std::move(new_fields)); + } + + /// Creates a new named tuple by applying the supplied function to every + /// field. + template + auto transform(const F& _f) const { + const auto transform_field = [&_f](auto... fields) { + return std::make_tuple(_f(std::move(fields))...); + }; + const auto to_nt = [](std::tuple&& _tup) { + return NamedTuple(_tup); + }; + auto new_fields = std::apply(transform_field, std::move(fields())); + return to_nt(std::move(new_fields)); + } + /// Returns the underlying std::tuple. Values& values() { return values_; } @@ -488,6 +550,78 @@ class NamedTuple { // ---------------------------------------------------------------------------- +/// We need a special template instantiation for empty named tuples. +template <> +class NamedTuple<> { + public: + using Fields = std::tuple<>; + using Values = std::tuple<>; + + NamedTuple(){}; + + ~NamedTuple() = default; + + /// Returns a new named tuple with additional fields. + template + auto add(Head&& _head, Tail&&... _tail) const { + if constexpr (sizeof...(Tail) > 0) { + return NamedTuple>(std::forward(_head)) + .add(std::forward(_tail)...); + } else { + return NamedTuple>(std::forward(_head)); + } + } + + /// Template specialization for std::tuple, so we can pass fields from other + /// named tuples. + template + auto add(std::tuple&& _tuple, Tail&&... _tail) const { + if constexpr (sizeof...(Tail) > 0) { + return NamedTuple( + std::forward>(_tuple)) + .add(std::forward(_tail)...); + } else { + return NamedTuple( + std::forward>(_tuple)); + } + } + + /// Template specialization for NamedTuple, so we can pass fields from other + /// named tuples. + template + auto add(NamedTuple&& _named_tuple, Tail&&... _tail) const { + return add(std::forward>(_named_tuple.fields()), + std::forward(_tail)...); + } + + /// Returns an empty named tuple. + template + auto and_then(const F& _f) const { + return NamedTuple<>(); + } + + /// Does nothing at all. + template + void apply(const F& _f) const {} + + /// Returns an empty tuple. + auto fields() const { return std::tuple(); } + + /// Must always be 0. + static constexpr size_t size() { return 0; } + + /// Returns an empty named tuple. + template + auto transform(const F& _f) const { + return NamedTuple<>(); + } + + /// Returns an empty tuple. + auto values() const { return std::tuple(); } +}; + +// ---------------------------------------------------------------------------- + template inline bool operator==(const rfl::NamedTuple& _nt1, const rfl::NamedTuple& _nt2) { diff --git a/include/rfl/from_named_tuple.hpp b/include/rfl/from_named_tuple.hpp index 63a29838..8a114e59 100644 --- a/include/rfl/from_named_tuple.hpp +++ b/include/rfl/from_named_tuple.hpp @@ -14,7 +14,7 @@ namespace rfl { /// Generates the struct T from a named tuple. template -auto from_named_tuple(NamedTupleType&& _n) { +T from_named_tuple(NamedTupleType&& _n) { using RequiredType = std::remove_cvref_t>; if constexpr (!std::is_same, RequiredType>()) { @@ -36,7 +36,7 @@ auto from_named_tuple(NamedTupleType&& _n) { /// Generates the struct T from a named tuple. template -auto from_named_tuple(const NamedTupleType& _n) { +T from_named_tuple(const NamedTupleType& _n) { using RequiredType = std::remove_cvref_t>; if constexpr (!std::is_same, RequiredType>()) { diff --git a/include/rfl/make_named_tuple.hpp b/include/rfl/make_named_tuple.hpp index 1b620534..aa78ce69 100644 --- a/include/rfl/make_named_tuple.hpp +++ b/include/rfl/make_named_tuple.hpp @@ -8,20 +8,23 @@ namespace rfl { /// Convenience constructor that doesn't require you -/// to explitly defined the field types. +/// to explitly define the field types. template -auto make_named_tuple(FieldTypes&&... _args) { +inline auto make_named_tuple(FieldTypes&&... _args) { return NamedTuple...>( std::forward(_args)...); } /// Convenience constructor that doesn't require you -/// to explitly defined the field types. -template -auto make_named_tuple(const Head& _h, const Tail&... _t) { - return NamedTuple(_h, _t...); +/// to explitly define the field types. +template +inline auto make_named_tuple(const FieldTypes&... _args) { + return NamedTuple(_args...); } +/// Explicit overload for creating empty named tuples. +inline auto make_named_tuple() { return NamedTuple<>(); } + } // namespace rfl #endif // RFL_MAKENAMEDTUPLE_HPP_ diff --git a/tests/json/test_and_then.cpp b/tests/json/test_and_then.cpp new file mode 100644 index 00000000..835fd99c --- /dev/null +++ b/tests/json/test_and_then.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_replace.hpp" +#include "write_and_read.hpp" + +namespace test_and_then { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto lisa = + Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; + + const auto to_bart = [](auto field) { + if constexpr (decltype(field)::name() == "first_name") { + field = "Bart"; + return rfl::make_named_tuple(field); + } else if constexpr (decltype(field)::name() == "age") { + return rfl::make_named_tuple(); + } else { + return rfl::make_named_tuple(field); + } + }; + + const auto bart = rfl::to_named_tuple(lisa).and_then(to_bart); + + write_and_read(bart, R"({"first_name":"Bart","last_name":"Simpson"})"); +} + +} // namespace test_and_then diff --git a/tests/json/test_and_then.hpp b/tests/json/test_and_then.hpp new file mode 100644 index 00000000..8e0bde5a --- /dev/null +++ b/tests/json/test_and_then.hpp @@ -0,0 +1,4 @@ +namespace test_and_then { +void test(); +} + diff --git a/tests/json/test_transform.cpp b/tests/json/test_transform.cpp new file mode 100644 index 00000000..9c70c48f --- /dev/null +++ b/tests/json/test_transform.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_replace.hpp" +#include "write_and_read.hpp" + +namespace test_transform { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto lisa = + Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; + + const auto to_bart = [](auto field) { + if constexpr (decltype(field)::name() == "first_name") { + field = "Bart"; + return field; + } else if constexpr (decltype(field)::name() == "age") { + field = 10; + return field; + } else { + return field; + } + }; + + const auto bart = rfl::to_named_tuple(lisa).transform(to_bart); + + write_and_read(bart, + R"({"first_name":"Bart","last_name":"Simpson","age":10})"); +} + +} // namespace test_transform diff --git a/tests/json/test_transform.hpp b/tests/json/test_transform.hpp new file mode 100644 index 00000000..c02101da --- /dev/null +++ b/tests/json/test_transform.hpp @@ -0,0 +1,4 @@ +namespace test_transform { +void test(); +} + diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 5751f340..e388772d 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -1,6 +1,7 @@ #include "test_all_of.hpp" #include "test_alphanumeric_map.hpp" #include "test_alphanumeric_unordered_map.hpp" +#include "test_and_then.hpp" #include "test_anonymous_fields.hpp" #include "test_any_of.hpp" #include "test_apply.hpp" @@ -69,6 +70,7 @@ #include "test_tagged_union.hpp" #include "test_tagged_union2.hpp" #include "test_timestamp.hpp" +#include "test_transform.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" #include "test_unnamed_namespace.hpp" @@ -154,8 +156,11 @@ int main() { test_as::test(); test_as2::test(); test_as_flatten::test(); + test_view::test(); test_apply::test(); + test_transform::test(); + test_and_then::test(); test_custom_constructor::test();