From a5504d7db77f9b4b831dc8e74db0721854590224 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 8 Oct 2023 03:21:38 +0200 Subject: [PATCH] Make sure that we are consistently following the Rule of 5 --- include/rfl/Box.hpp | 13 +++++++ include/rfl/Field.hpp | 41 ++++++++++++++------- include/rfl/Flatten.hpp | 31 +++++++++++----- include/rfl/Literal.hpp | 14 ++++--- include/rfl/NamedTuple.hpp | 73 +++++++++++++++++++++++++++++++++++++ include/rfl/Ref.hpp | 31 ++++++++++------ include/rfl/Result.hpp | 19 +++++++++- include/rfl/TaggedUnion.hpp | 68 +++++++++++++++++++++------------- 8 files changed, 222 insertions(+), 68 deletions(-) diff --git a/include/rfl/Box.hpp b/include/rfl/Box.hpp index 27819a4e..bb628334 100644 --- a/include/rfl/Box.hpp +++ b/include/rfl/Box.hpp @@ -18,6 +18,8 @@ class Box { return Box(std::make_unique(std::forward(_args)...)); } + Box(const Box& _other) = delete; + Box(Box&& _other) : ptr_(std::move(_other.ptr_)) {} ~Box() = default; @@ -25,6 +27,17 @@ class Box { /// Returns a pointer to the underlying object inline T* get() const { return ptr_.get(); } + /// Copy assignment operator + inline Box& operator=(const Box& _other) = delete; + + /// Move assignment operator + inline Box& operator=(Box&& _other) noexcept { + if (&_other != this) { + ptr_ = std::move(_other.ptr_); + } + return *this; + } + /// Returns the underlying object. inline T& operator*() const { return *ptr_; } diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index f239dfbf..2a19a923 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -70,48 +70,61 @@ struct Field { inline const Type& operator()() const { return value_; } /// Assigns the underlying object. - inline void operator=(const Type& _value) { value_ = _value; } + inline auto& operator=(const Type& _value) { + value_ = _value; + return *this; + } /// Assigns the underlying object. - inline void operator=(Type&& _value) noexcept { + inline auto& operator=(Type&& _value) noexcept { value_ = std::move(_value); + return *this; } /// Assigns the underlying object. template , bool>::type = true> - inline void operator=(const T& _value) { + inline auto& operator=(const T& _value) { value_ = _value; + return *this; } /// Assigns the underlying object to its default value. template , bool>::type = true> - inline void operator=(const Default& _default) { + inline auto& operator=(const Default& _default) { value_ = Type(); + return *this; + } + /// Assigns the underlying object. + inline Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) { + if (&_field != this) { + value_ = _field.get(); + } + return *this; } /// Assigns the underlying object. - inline void operator=(const Field<_name, Type>& _field) { - value_ = _field.get(); - } - - /// Assigns the underlying object. - inline void operator=(Field<_name, Type>&& _field) noexcept { - value_ = std::move(_field.value_); + inline Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) { + if (&_field != this) { + value_ = std::move(_field.value_); + } + return *this; } /// Assigns the underlying object. template - inline void operator=(const Field<_name, T>& _field) { + inline auto& operator=(const Field<_name, T>& _field) { value_ = _field.get(); + return *this; } /// Assigns the underlying object. template - inline void operator=(Field<_name, T>&& _field) { - value_ = _field.get(); + inline auto& operator=(Field<_name, T>&& _field) { + value_ = std::forward(_field.value_); + return *this; } /// Assigns the underlying object. diff --git a/include/rfl/Flatten.hpp b/include/rfl/Flatten.hpp index eaafdc39..ca13d944 100644 --- a/include/rfl/Flatten.hpp +++ b/include/rfl/Flatten.hpp @@ -19,9 +19,9 @@ struct Flatten { Flatten(Type&& _value) noexcept : value_(std::forward(_value)) {} - Flatten(const Flatten& _f) : value_(_f.get()) {} + Flatten(const Flatten<_Type>& _f) : value_(_f.get()) {} - Flatten(Flatten&& _f) noexcept + Flatten(Flatten<_Type>&& _f) noexcept : value_(std::forward(_f.value_)) {} template @@ -50,13 +50,13 @@ struct Flatten { inline const Type& operator()() const { return value_; } /// Assigns the underlying object. - inline Flatten operator=(const Type& _value) { + inline Flatten<_Type>& operator=(const _Type& _value) { value_ = _value; return *this; } /// Assigns the underlying object. - inline Flatten operator=(Type&& _value) { + inline Flatten<_Type>& operator=(_Type&& _value) { value_ = std::forward(_value); return *this; } @@ -64,28 +64,39 @@ struct Flatten { /// Assigns the underlying object. template , bool>::type = true> - inline void operator=(const T& _value) { + inline Flatten<_Type>& operator=(const T& _value) { value_ = _value; + return *this; } /// Assigns the underlying object. - inline void operator=(const Flatten& _f) { value_ = _f.get(); } + inline Flatten<_Type>& operator=(const Flatten<_Type>& _f) { + if (&_f != this) { + value_ = _f.get(); + } + return *this; + } /// Assigns the underlying object. - inline void operator=(Flatten&& _f) { - value_ = std::forward(_f.value_); + inline Flatten<_Type>& operator=(Flatten<_Type>&& _f) { + if (&_f != this) { + value_ = std::move(_f.value_); + } + return *this; } /// Assigns the underlying object. template - inline void operator=(const Flatten& _f) { + inline Flatten<_Type>& operator=(const Flatten& _f) { value_ = _f.get(); + return *this; } /// Assigns the underlying object. template - inline void operator=(Flatten&& _f) { + inline Flatten<_Type>& operator=(Flatten&& _f) { value_ = std::forward(_f); + return *this; } /// Assigns the underlying object. diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index acbd73f7..f1019fd5 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -119,19 +119,23 @@ class Literal { } /// Assigns from another literal. - Literal operator=(const Literal& _other) { - value_ = _other.value_; + Literal& operator=(const Literal& _other) { + if (this != &_other) { + value_ = _other.value_; + } return *this; } /// Assigns from another literal. - Literal operator=(Literal&& _other) noexcept { - value_ = _other.value_; + Literal& operator=(Literal&& _other) noexcept { + if (this != &_other) { + value_ = _other.value_; + } return *this; } /// Assigns the literal from a string - Literal operator=(const std::string& _str) { + Literal& operator=(const std::string& _str) { value_ = find_value(_str); return *this; } diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 48bd252b..ee29b215 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -98,6 +98,19 @@ class NamedTuple { ~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 { @@ -111,6 +124,20 @@ class NamedTuple { } } + /// 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), + std::make_index_sequence{}) + .replace(_tail...); + } else { + return add_tuple(std::forward>(_tuple), + std::make_index_sequence{}); + } + } + /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template @@ -139,6 +166,13 @@ class NamedTuple { } } + /// Template specialization for NamedTuple, so we can pass fields from other + /// named tuples. + template + auto add(NamedTuple&& _named_tuple, Tail&&... _tail) { + return add(_named_tuple.fields(), std::forward(_tail)...); + } + /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template @@ -146,6 +180,13 @@ class NamedTuple { return add(_named_tuple.fields(), std::forward(_tail)...); } + /// Template specialization for NamedTuple, so we can pass fields from other + /// named tuples. + template + auto add(const NamedTuple& _named_tuple, Tail&&... _tail) { + return add(_named_tuple.fields(), std::forward(_tail)...); + } + /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template @@ -154,6 +195,9 @@ class NamedTuple { return add(_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(); } @@ -300,6 +344,13 @@ class NamedTuple { 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(Tuple&& _tuple, const std::index_sequence) { + return add(std::get(_tuple)...); + } + /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template @@ -308,6 +359,28 @@ class NamedTuple { return add(std::get(_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 { diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index 8a591765..229ec0ce 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -18,18 +18,9 @@ class Ref { return Ref(std::make_shared(std::forward(_args)...)); } - /// Wrapper around a shared_ptr, leads to a runtime error, - /// if the shared_ptr is not allocated. - explicit Ref(const std::shared_ptr& _ptr) { - assert_true(_ptr); - if (!_ptr) { - throw std::runtime_error( - "Could not create Ref from shared_ptr, because shared_ptr was " - "not " - "set."); - } - ptr_ = _ptr; - } + Ref(const Ref& _other) : ptr_(_other.ptr_){}; + + Ref(Ref&& _other) : ptr_(std::move(_other.ptr_)) {} template Ref(const Ref& _other) : ptr_(_other.ptr()) {} @@ -55,6 +46,22 @@ class Ref { return *this; } + /// Move assignment operator + inline Ref& operator=(Ref&& _other) noexcept { + if (&_other != this) { + ptr_ = std::move(_other.ptr_); + } + return *this; + } + + /// Copy assignment operator + inline Ref& operator=(const Ref& _other) { + if (&_other != this) { + ptr_ = _other.ptr_; + } + return *this; + } + private: /// Only make is allowed to use this constructor. explicit Ref(std::shared_ptr&& _ptr) : ptr_(std::move(_ptr)) {} diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index c4046d38..d12b6b19 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -182,12 +182,29 @@ class Result { /// undefined behavior, if the result contains an error. const T& operator*() const { return *std::get_if(&t_or_err_); } + /// Assigns the underlying object. + inline Result& operator=(const Result& _other) { + if (this != &_other) { + t_or_err_ = _other.t_or_err_; + } + return *this; + } + + /// Assigns the underlying object. + inline Result& operator=(Result&& _other) { + if (this != &_other) { + t_or_err_ = std::move(_other.t_or_err_); + } + return *this; + } + /// Assigns the underlying object. template , bool>::type = true> - inline void operator=(const Result& _other) { + inline auto& operator=(const Result& _other) { const auto to_t = [](const U& _u) -> T { return _u; }; t_or_err_ = _other.transform(to_t).t_or_err_; + return *this; } /// Expects a function that takes of type Error -> Result and returns diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index ffda387e..c832c456 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -41,39 +41,55 @@ struct TaggedUnion { ~TaggedUnion() = default; /// Assigns the underlying object. - inline void operator=(const VariantType& _variant) { variant_ = _variant; } - - /// Assigns the underlying object. - inline void operator=(VariantType&& _variant) noexcept { - variant_ = std::move(_variant); - } - - /// Assigns the underlying object. - template , - bool>::type = true> - inline void operator=(T&& _variant) { - variant_ = std::forward(_variant); - } - - /// Assigns the underlying object. - template , - bool>::type = true> - inline void operator=(const T& _variant) { + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + const VariantType& _variant) { variant_ = _variant; + return *this; } /// Assigns the underlying object. - inline void operator=( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _field) { - variant_ = _field.get(); + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + VariantType&& _variant) { + variant_ = std::move(_variant); + return *this; } /// Assigns the underlying object. - inline void operator=( - TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) noexcept { - variant_ = std::move(_other.variant_); + template , + bool>::type = true> + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + T&& _variant) { + variant_ = std::forward(_variant); + return *this; + } + + /// Assigns the underlying object. + template , + bool>::type = true> + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + const T& _variant) { + variant_ = _variant; + return *this; + } + + /// Assigns the underlying object. + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _other) { + if (this != &_other) { + variant_ = _other.get(); + } + return *this; + } + + /// Assigns the underlying object. + inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) { + if (this != &_other) { + variant_ = std::move(_other.variant_); + } + return *this; } /// Returns the underlying variant.