diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 4498d838..2fffbcaa 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -105,8 +105,9 @@ class NamedTuple { ~NamedTuple() = default; /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) { + template + auto add(Field<_name, FType>&& _head, Tail&&... _tail) { + using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( make_fields<1, Head>(std::forward(_head))) @@ -118,15 +119,16 @@ class NamedTuple { } /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) const { + template + auto add(Field<_name, FType> _head, const Tail&... _tail) const { + using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))) - .add(std::forward(_tail)...); + make_fields<1, Head>(_head)) + .add(_tail...); } else { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))); + make_fields<1, Head>(_head)); } } @@ -145,12 +147,11 @@ class NamedTuple { /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple&& _tuple, Tail&&... _tail) const { + auto add(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple)) - .add(std::forward(_tail)...); + return add_tuple(std::move(_tuple)).add(_tail...); } else { - return add_tuple(std::forward>(_tuple)); + return add_tuple(std::move(_tuple)); } } @@ -165,9 +166,8 @@ class NamedTuple { /// 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)...); + auto add(NamedTuple _named_tuple, const Tail&... _tail) const { + return add(_named_tuple.fields(), _tail...); } /// Creates a new named tuple by applying the supplied function to @@ -273,11 +273,20 @@ class NamedTuple { NamedTuple& operator=( NamedTuple&& _other) noexcept = default; + /// Equality operator + inline auto operator==(const rfl::NamedTuple& _other) const { + return values() == _other.values(); + } + + /// Inequality operator + inline auto operator!=(const rfl::NamedTuple& _other) const { + return !(*this == _other); + } /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. - template - NamedTuple replace(RField&& _field, - OtherRFields&&... _other_fields) { + template + auto replace(Field<_name, FType>&& _field, OtherRFields&&... _other_fields) { + using RField = Field<_name, FType>; constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { return replace_value(_field.value_); @@ -289,15 +298,16 @@ class NamedTuple { /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. - template - NamedTuple replace(RField&& _field, - OtherRFields&&... _other_fields) const { + template + auto replace(Field<_name, FType> _field, + const OtherRFields&... _other_fields) const { + using RField = Field<_name, FType>; constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { - return replace_value(_field.value_); + return replace_value(std::move(_field.value_)); } else { - return replace_value(_field.value_) - .replace(std::forward(_other_fields)...); + return replace_value(std::move(_field.value_)) + .replace(_other_fields...); } } @@ -316,12 +326,11 @@ class NamedTuple { /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto replace(std::tuple&& _tuple, Tail&&... _tail) const { + auto replace(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return replace_tuple(std::forward>(_tuple)) - .replace(std::forward(_tail)...); + return replace_tuple(std::move(_tuple)).replace(_tail...); } else { - return replace_tuple(std::forward>(_tuple)); + return replace_tuple(std::move(_tuple)); } } @@ -337,11 +346,9 @@ class NamedTuple { /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template - auto replace(const NamedTuple& _named_tuple, - Tail&&... _tail) const { - return replace( - std::forward>(_named_tuple).fields(), - std::forward(_tail)...); + auto replace(NamedTuple _named_tuple, + const Tail&... _tail) const { + return replace(_named_tuple.fields(), _tail...); } /// Returns the size of the named tuple @@ -426,7 +433,7 @@ class NamedTuple { /// Generates the fields. template - auto make_fields(Args&&... _args) const { + auto make_fields(Args... _args) const { constexpr auto size = sizeof...(Args) - num_additional_fields; constexpr auto num_fields = std::tuple_size_v; constexpr auto i = num_fields - size - 1; @@ -434,13 +441,13 @@ class NamedTuple { constexpr bool retrieved_all_fields = size == num_fields; if constexpr (retrieved_all_fields) { - return std::make_tuple(std::forward(_args)...); + return std::make_tuple(std::move(_args)...); } else { // When we add additional fields, it is more intuitive to add // them to the end, that is why we do it like this. using FieldType = typename std::tuple_element::type; return make_fields(FieldType(std::get(values_)), - std::forward(_args)...); + std::move(_args)...); } } @@ -564,36 +571,31 @@ class NamedTuple<> { ~NamedTuple() = default; /// Returns a new named tuple with additional fields. - template - auto add(Head&& _head, Tail&&... _tail) const { + template + auto add(Field<_name, FType> _head, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return NamedTuple>(std::forward(_head)) - .add(std::forward(_tail)...); + return NamedTuple>(std::move(_head)).add(_tail...); } else { - return NamedTuple>(std::forward(_head)); + return NamedTuple>(std::move(_head)); } } /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple&& _tuple, Tail&&... _tail) const { + auto add(std::tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return NamedTuple( - std::forward>(_tuple)) - .add(std::forward(_tail)...); + return NamedTuple(std::move(_tuple)).add(_tail...); } else { - return NamedTuple( - std::forward>(_tuple)); + return NamedTuple(std::move(_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)...); + auto add(NamedTuple _named_tuple, const Tail&... _tail) const { + return add(_named_tuple.fields(), _tail...); } /// Returns an empty named tuple. @@ -624,18 +626,6 @@ class NamedTuple<> { // ---------------------------------------------------------------------------- -template -inline bool operator==(const rfl::NamedTuple& _nt1, - const rfl::NamedTuple& _nt2) { - return _nt1.values() == _nt2.values(); -} - -template -inline bool operator!=(const rfl::NamedTuple& _nt1, - const rfl::NamedTuple& _nt2) { - return _nt1.values() != _nt2.values(); -} - template inline auto operator*(const rfl::Field<_name1, Type1>& _f1,