#ifndef RFL_NAMEDTUPLE_HPP_ #define RFL_NAMEDTUPLE_HPP_ #include #include #include #include #include #include "rfl/Field.hpp" #include "rfl/get.hpp" #include "rfl/internal/StringLiteral.hpp" #include "rfl/internal/find_index.hpp" #include "rfl/internal/no_duplicate_field_names.hpp" namespace rfl { /// A named tuple behaves like std::tuple, /// but the fields have explicit names, which /// allows for reflection. template class NamedTuple { public: using Fields = std::tuple...>; using Values = std::tuple::type::Type...>; public: /// Construct from the values. NamedTuple(typename std::decay::type::Type&&... _values) : values_(std::forward::type::Type>( _values)...) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the values. NamedTuple(const typename std::decay::type::Type&... _values) : values_(std::make_tuple(_values...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the fields. NamedTuple(FieldTypes&&... _fields) : values_(std::make_tuple(std::move(_fields.value_)...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the fields. NamedTuple(const FieldTypes&... _fields) : values_(std::make_tuple(_fields.value_...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from a tuple containing fields. NamedTuple(std::tuple&& _tup) : NamedTuple(std::make_from_tuple>( std::forward>(_tup))) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from a tuple containing fields. NamedTuple(const std::tuple& _tup) : NamedTuple(std::make_from_tuple>(_tup)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Copy constructor. NamedTuple(const NamedTuple& _other) : values_(_other.values_) { // Duplicate field checks not required - has already been checked. } /// Move constructor. NamedTuple(NamedTuple&& _other) : values_(std::move(_other.values_)) { // Duplicate field checks not required - has already been checked. } /// Copy constructor. template NamedTuple(const NamedTuple& _other) : NamedTuple(retrieve_fields(_other.fields())) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Move constructor. template NamedTuple(NamedTuple&& _other) : NamedTuple(retrieve_fields(_other.fields())) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } ~NamedTuple() = default; /// Returns a new named tuple with additional fields. template auto add(Head&& _head, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( make_fields<1, Head>(std::forward(_head))) .add(std::forward(_tail)...); } else { return NamedTuple>( make_fields<1, Head>(std::forward(_head))); } } /// Returns a new named tuple with additional fields. template auto add(Head&& _head, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( make_fields<1, Head>(std::forward(_head))) .add(std::forward(_tail)...); } else { return NamedTuple>( make_fields<1, Head>(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) { if constexpr (sizeof...(Tail) > 0) { return add_tuple(std::forward>(_tuple)) .add(std::forward(_tail)...); } else { return add_tuple(std::forward>(_tuple)); } } /// 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 add_tuple(std::forward>(_tuple)) .add(std::forward(_tail)...); } else { return add_tuple(std::forward>(_tuple)); } } /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template auto add(NamedTuple&& _named_tuple, Tail&&... _tail) { return add( std::forward>(_named_tuple.fields()), std::forward(_tail)...); } /// 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 a tuple containing the fields. Fields fields() { return make_fields(); } /// Returns a tuple containing the fields. Fields fields() const { return make_fields(); } /// Gets a field by index. template inline auto& get() { return rfl::get<_index>(*this); } /// Gets a field by name. template inline auto& get() { return rfl::get<_field_name>(*this); } /// Gets a field by the field type. template inline auto& get() { return rfl::get(*this); } /// Gets a field by index. template inline const auto& get() const { return rfl::get<_index>(*this); } /// Gets a field by name. template inline const auto& get() const { return rfl::get<_field_name>(*this); } /// Gets a field by the field type. template inline const auto& get() const { return rfl::get(*this); } /// Returns the results wrapped in a field. template inline auto get_field() const { return rfl::make_field<_field_name>(rfl::get<_field_name>(*this)); } /// Copy assignment operator. NamedTuple& operator=( const NamedTuple& _other) { values_ = _other.values_; return *this; } /// Move assignment operator. NamedTuple& operator=( NamedTuple&& _other) noexcept { if (this == &_other) { return *this; } values_ = std::move(_other.values_); return *this; } /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. template NamedTuple replace(RField&& _field, OtherRFields&&... _other_fields) { constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { return replace_value(_field.value_); } else { return replace_value(_field.value_) .replace(std::forward(_other_fields)...); } } /// 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 { constexpr auto num_other_fields = sizeof...(OtherRFields); if constexpr (num_other_fields == 0) { return replace_value(_field.value_); } else { return replace_value(_field.value_) .replace(std::forward(_other_fields)...); } } /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template auto replace(std::tuple&& _tuple, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { return replace_tuple( std::forward>(_tuple)) .replace(std::forward(_tail)...); } else { return replace_tuple( std::forward>(_tuple)); } } /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template auto replace(std::tuple&& _tuple, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return replace_tuple( std::forward>(_tuple)) .replace(std::forward(_tail)...); } else { return replace_tuple( std::forward>(_tuple)); } } /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template auto replace(NamedTuple&& _named_tuple, Tail&&... _tail) { return replace( std::forward>(_named_tuple).fields(), std::forward(_tail)...); } /// 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)...); } /// Returns the underlying std::tuple. Values& values() { return values_; } /// Returns the underlying std::tuple. const Values& values() const { return values_; } private: /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template constexpr auto add_tuple(std::tuple&& _tuple) { const auto a = [this](auto&&... _fields) { return this->add(std::forward(_fields)...); }; return std::apply(a, std::forward>(_tuple)); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template constexpr auto add_tuple(std::tuple&& _tuple) const { const auto a = [this](auto&&... _fields) { return this->add(std::forward(_fields)...); }; return std::apply(a, std::forward>(_tuple)); } /// Generates the fields. template auto make_fields(Args&&... _args) { constexpr auto size = sizeof...(Args) - num_additional_fields; constexpr auto num_fields = std::tuple_size{}; constexpr auto i = num_fields - size - 1; constexpr bool retrieved_all_fields = size == num_fields; if constexpr (retrieved_all_fields) { return std::make_tuple(std::forward(_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; using T = std::decay_t; return make_fields( FieldType(std::forward(std::get(values_))), std::forward(_args)...); } } /// Generates the fields. template auto make_fields(Args&&... _args) const { constexpr auto size = sizeof...(Args) - num_additional_fields; constexpr auto num_fields = std::tuple_size{}; constexpr auto i = num_fields - size - 1; constexpr bool retrieved_all_fields = size == num_fields; if constexpr (retrieved_all_fields) { return std::make_tuple(std::forward(_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)...); } } /// Generates a new named tuple with one value replaced with a new value. template auto make_replaced(V&& _values, T&& _val, Args&&... _args) const { constexpr auto size = sizeof...(Args); constexpr bool retrieved_all_fields = size == std::tuple_size{}; if constexpr (retrieved_all_fields) { return NamedTuple(std::forward(_args)...); } else { using FieldType = typename std::tuple_element::type; if constexpr (size == _index) { return make_replaced<_index, V, T>( std::forward(_values), std::forward(_val), std::forward(_args)..., FieldType(std::forward(_val))); } else { using U = typename FieldType::Type; return make_replaced<_index, V, T>( std::forward(_values), std::forward(_val), std::forward(_args)..., FieldType(std::forward(std::get(_values)))); } } } /// We cannot allow duplicate field names. constexpr static bool no_duplicate_field_names() { return internal::no_duplicate_field_names(); } /// Replaced the field signified by the field type. template NamedTuple replace_value(T&& _val) { using FieldType = std::decay_t; constexpr auto index = internal::find_index(); return make_replaced(std::forward(values_), std::forward(_val)); } /// Replaced the field signified by the field type. template NamedTuple replace_value(T&& _val) const { using FieldType = std::decay_t; constexpr auto index = internal::find_index(); auto values = values_; return make_replaced(std::move(values), std::forward(_val)); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template auto replace_tuple(std::tuple&& _tuple) { const auto r = [this](auto&&... _fields) { return this->replace(std::forward(_fields)...); }; return std::apply(r, std::forward>(_tuple)); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template auto replace_tuple(std::tuple&& _tuple) const { const auto r = [this](auto&&... _fields) { return this->replace(std::forward(_fields)...); }; return std::apply(r, std::forward>(_tuple)); } /// Retrieves the fields from another tuple. template constexpr static Fields retrieve_fields( std::tuple&& _other_fields, Args&&... _args) { constexpr auto size = sizeof...(Args); constexpr bool retrieved_all_fields = size == std::tuple_size{}; if constexpr (retrieved_all_fields) { return std::make_tuple(std::forward(_args)...); } else { constexpr auto field_name = std::tuple_element::type::name_; constexpr auto index = internal::find_index>(); using FieldType = typename std::tuple_element::type; using T = std::decay_t; return retrieve_fields( std::forward>(_other_fields), std::forward(_args)..., FieldType( std::forward(std::get(_other_fields).value_))); } } private: /// The values actually contained in the named tuple. /// As you can see, a NamedTuple is just a normal tuple under-the-hood, /// everything else is resolved at compile time. It should have no /// runtime overhead over a normal std::tuple. Values values_; }; // ---------------------------------------------------------------------------- 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, const rfl::Field<_name2, Type2>& _f2) { return NamedTuple(_f1, _f2); } template inline auto operator*(const NamedTuple& _tup, const rfl::Field<_name, Type>& _f) { return _tup.add(_f); } template inline auto operator*(const rfl::Field<_name, Type>& _f, const NamedTuple& _tup) { return NamedTuple(_f).add(_tup); } template inline auto operator*(const NamedTuple& _tup1, const NamedTuple& _tup2) { return _tup1.add(_tup2); } template inline auto operator*(rfl::Field<_name1, Type1>&& _f1, rfl::Field<_name2, Type2>&& _f2) { return NamedTuple(std::forward>(_f1), std::forward>(_f2)); } template inline auto operator*(NamedTuple&& _tup, rfl::Field<_name, Type>&& _f) { return _tup.add(std::forward>(_f)); } template inline auto operator*(rfl::Field<_name, Type>&& _f, NamedTuple&& _tup) { return NamedTuple(std::forward>(_f)) .add(std::forward>(_tup)); } template inline auto operator*(NamedTuple&& _tup1, NamedTuple&& _tup2) { return _tup1.add(std::forward>(_tup2)); } } // namespace rfl #endif // RFL_NAMEDTUPLE_HPP_