From 013708b77cb346070237ff33083fec8dae2b23dd Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 15 Oct 2023 17:58:37 +0200 Subject: [PATCH] Removed unnecessary boilerplate code --- include/rfl/Box.hpp | 19 +++++------ include/rfl/Field.hpp | 45 ++++++++++---------------- include/rfl/Flatten.hpp | 40 +++++++++-------------- include/rfl/Literal.hpp | 63 ++++++++++++++----------------------- include/rfl/NamedTuple.hpp | 37 +++++++--------------- include/rfl/Ref.hpp | 26 +++++---------- include/rfl/Result.hpp | 16 ++-------- include/rfl/TaggedUnion.hpp | 40 +++++++++-------------- 8 files changed, 97 insertions(+), 189 deletions(-) diff --git a/include/rfl/Box.hpp b/include/rfl/Box.hpp index bb628334..dd107edd 100644 --- a/include/rfl/Box.hpp +++ b/include/rfl/Box.hpp @@ -20,32 +20,27 @@ class Box { Box(const Box& _other) = delete; - Box(Box&& _other) : ptr_(std::move(_other.ptr_)) {} + Box(Box&& _other) = default; ~Box() = default; /// Returns a pointer to the underlying object - inline T* get() const { return ptr_.get(); } + T* get() const { return ptr_.get(); } /// Copy assignment operator - inline Box& operator=(const Box& _other) = delete; + 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; - } + Box& operator=(Box&& _other) noexcept = default; /// Returns the underlying object. - inline T& operator*() const { return *ptr_; } + T& operator*() const { return *ptr_; } /// Returns the underlying object. - inline T* operator->() const { return ptr_.get(); } + T* operator->() const { return ptr_.get(); } /// Returns the underlying unique_ptr - inline const std::unique_ptr& ptr() const { return ptr_; } + const std::unique_ptr& ptr() const { return ptr_; } private: /// Only make is allowed to use this constructor. diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 2a19a923..77249f76 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -26,10 +26,9 @@ struct Field { Field(Type&& _value) noexcept : value_(std::move(_value)) {} - Field(Field<_name, _Type>&& _field) noexcept - : value_(std::move(_field.value_)) {} + Field(Field<_name, _Type>&& _field) noexcept = default; - Field(const Field<_name, Type>& _field) : value_(_field.get()) {} + Field(const Field<_name, Type>& _field) = default; template Field(const Field<_name, T>& _field) : value_(_field.get()) {} @@ -61,22 +60,22 @@ struct Field { constexpr static const internal::StringLiteral name_ = _name; /// Returns the underlying object. - inline const Type& get() const { return value_; } + const Type& get() const { return value_; } /// Returns the underlying object. - inline Type& operator()() { return value_; } + Type& operator()() { return value_; } /// Returns the underlying object. - inline const Type& operator()() const { return value_; } + const Type& operator()() const { return value_; } /// Assigns the underlying object. - inline auto& operator=(const Type& _value) { + auto& operator=(const Type& _value) { value_ = _value; return *this; } /// Assigns the underlying object. - inline auto& operator=(Type&& _value) noexcept { + auto& operator=(Type&& _value) noexcept { value_ = std::move(_value); return *this; } @@ -84,7 +83,7 @@ struct Field { /// Assigns the underlying object. template , bool>::type = true> - inline auto& operator=(const T& _value) { + auto& operator=(const T& _value) { value_ = _value; return *this; } @@ -93,51 +92,41 @@ struct Field { template , bool>::type = true> - inline auto& operator=(const Default& _default) { + 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; - } + Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) = default; /// Assigns the underlying object. - inline Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) { - if (&_field != this) { - value_ = std::move(_field.value_); - } - return *this; - } + Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) = default; /// Assigns the underlying object. template - inline auto& operator=(const Field<_name, T>& _field) { + auto& operator=(const Field<_name, T>& _field) { value_ = _field.get(); return *this; } /// Assigns the underlying object. template - inline auto& operator=(Field<_name, T>&& _field) { + auto& operator=(Field<_name, T>&& _field) { value_ = std::forward(_field.value_); return *this; } /// Assigns the underlying object. - inline void set(const Type& _value) { value_ = _value; } + void set(const Type& _value) { value_ = _value; } /// Assigns the underlying object. - inline void set(Type&& _value) { value_ = std::move(_value); } + void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. - inline Type& value() { return value_; } + Type& value() { return value_; } /// Returns the underlying object. - inline const Type& value() const { return value_; } + const Type& value() const { return value_; } /// The underlying value. Type value_; diff --git a/include/rfl/Flatten.hpp b/include/rfl/Flatten.hpp index ca13d944..5d7028e8 100644 --- a/include/rfl/Flatten.hpp +++ b/include/rfl/Flatten.hpp @@ -19,10 +19,9 @@ struct Flatten { Flatten(Type&& _value) noexcept : value_(std::forward(_value)) {} - Flatten(const Flatten<_Type>& _f) : value_(_f.get()) {} + Flatten(const Flatten<_Type>& _f) = default; - Flatten(Flatten<_Type>&& _f) noexcept - : value_(std::forward(_f.value_)) {} + Flatten(Flatten<_Type>&& _f) noexcept = default; template Flatten(const Flatten& _f) : value_(_f.get()) {} @@ -41,22 +40,22 @@ struct Flatten { ~Flatten() = default; /// Returns the underlying object. - inline const Type& get() const { return value_; } + const Type& get() const { return value_; } /// Returns the underlying object. - inline Type& operator()() { return value_; } + Type& operator()() { return value_; } /// Returns the underlying object. - inline const Type& operator()() const { return value_; } + const Type& operator()() const { return value_; } /// Assigns the underlying object. - inline Flatten<_Type>& operator=(const _Type& _value) { + Flatten<_Type>& operator=(const _Type& _value) { value_ = _value; return *this; } /// Assigns the underlying object. - inline Flatten<_Type>& operator=(_Type&& _value) { + Flatten<_Type>& operator=(_Type&& _value) { value_ = std::forward(_value); return *this; } @@ -64,46 +63,35 @@ struct Flatten { /// Assigns the underlying object. template , bool>::type = true> - inline Flatten<_Type>& operator=(const T& _value) { + Flatten<_Type>& operator=(const T& _value) { value_ = _value; return *this; } /// Assigns the underlying object. - inline Flatten<_Type>& operator=(const Flatten<_Type>& _f) { - if (&_f != this) { - value_ = _f.get(); - } - return *this; - } + Flatten<_Type>& operator=(const Flatten<_Type>& _f) = default; /// Assigns the underlying object. - inline Flatten<_Type>& operator=(Flatten<_Type>&& _f) { - if (&_f != this) { - value_ = std::move(_f.value_); - } - return *this; - } - + Flatten<_Type>& operator=(Flatten<_Type>&& _f) = default; /// Assigns the underlying object. template - inline Flatten<_Type>& operator=(const Flatten& _f) { + Flatten<_Type>& operator=(const Flatten& _f) { value_ = _f.get(); return *this; } /// Assigns the underlying object. template - inline Flatten<_Type>& operator=(Flatten&& _f) { + Flatten<_Type>& operator=(Flatten&& _f) { value_ = std::forward(_f); return *this; } /// Assigns the underlying object. - inline void set(const Type& _value) { value_ = _value; } + void set(const Type& _value) { value_ = _value; } /// Assigns the underlying object. - inline void set(Type&& _value) { value_ = std::forward(_value); } + void set(Type&& _value) { value_ = std::forward(_value); } /// The underlying value. Type value_; diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index f1019fd5..9661f3f5 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -34,10 +34,10 @@ class Literal { static constexpr ValueType num_fields_ = sizeof...(fields_); /// Constructs a Literal from another literal. - Literal(const Literal& _other) : value_(_other.value_) {} + Literal(const Literal& _other) = default; /// Constructs a Literal from another literal. - Literal(Literal&& _other) noexcept : value_(_other.value_) {} + Literal(Literal&& _other) noexcept = default; /// A single-field literal is special because it /// can also have a default constructor. @@ -48,20 +48,18 @@ class Literal { ~Literal() = default; /// Determines whether the literal contains the string. - inline static bool contains(const std::string& _str) { - return has_value(_str); - } + static bool contains(const std::string& _str) { return has_value(_str); } /// Determines whether the literal contains the string at compile time. template - inline static constexpr bool contains() { + static constexpr bool contains() { return find_value_of<_name>() != -1; } /// Determines whether the literal contains any of the strings in the other /// literal at compile time. template - inline static constexpr bool contains_any() { + static constexpr bool contains_any() { if constexpr (_i == num_fields_) { return false; } else { @@ -74,7 +72,7 @@ class Literal { /// Determines whether the literal contains all of the strings in the other /// literal at compile time. template - inline static constexpr bool contains_all() { + static constexpr bool contains_all() { if constexpr (_i == num_fields_) { return _n_found == OtherLiteralType::num_fields_; } else { @@ -89,9 +87,7 @@ class Literal { /// Determines whether the literal has duplicate strings at compile time. /// These is useful for checking collections of strings in other contexts. - inline static constexpr bool has_duplicates() { - return has_duplicate_strings(); - } + static constexpr bool has_duplicates() { return has_duplicate_strings(); } /// Constructs a Literal from a string. Returns an error if the string /// cannot be found. @@ -113,26 +109,17 @@ class Literal { /// Helper function to retrieve a name at compile time. template - inline constexpr static auto name_of() { + constexpr static auto name_of() { constexpr auto name = find_name_within_own_fields<_value>(); return Literal(); } /// Assigns from another literal. - Literal& operator=(const Literal& _other) { - if (this != &_other) { - value_ = _other.value_; - } - return *this; - } + Literal& operator=(const Literal& _other) = default; /// Assigns from another literal. - Literal& operator=(Literal&& _other) noexcept { - if (this != &_other) { - value_ = _other.value_; - } - return *this; - } + Literal& operator=(Literal&& _other) noexcept = + default; /// Assigns the literal from a string Literal& operator=(const std::string& _str) { @@ -141,37 +128,33 @@ class Literal { } /// Equality operator other Literals. - inline bool operator==(const Literal& _other) const { + bool operator==(const Literal& _other) const { return value() == _other.value(); } /// Equality operator other Literals with different fields. template - inline bool operator==(const Literal& _other) const { + bool operator==(const Literal& _other) const { return name() == _other.name(); } /// Inequality operator for other Literals. template - inline bool operator!=(const Literal& _other) const { + bool operator!=(const Literal& _other) const { return !(*this == _other); } /// Equality operator for strings. - inline bool operator==(const std::string& _str) const { - return name() == _str; - } + bool operator==(const std::string& _str) const { return name() == _str; } /// Inequality operator for strings. - inline bool operator!=(const std::string& _str) const { - return name() != _str; - } + bool operator!=(const std::string& _str) const { return name() != _str; } /// Alias for .name(). std::string str() const { return name(); } /// Returns the value actually contained in the Literal. - inline ValueType value() const { return value_; } + ValueType value() const { return value_; } /// Returns the value of the string literal in the template. template @@ -187,7 +170,7 @@ class Literal { /// Returns all of the allowed fields. template - inline static std::string allowed_strings(const std::string& _values = "") { + static std::string allowed_strings(const std::string& _values = "") { using FieldType = typename std::tuple_element<_i, FieldsType>::type; const auto head = "'" + FieldType::field_.str() + "'"; const auto values = _values.size() == 0 ? head : _values + ", " + head; @@ -200,7 +183,7 @@ class Literal { /// Whether the Literal contains duplicate strings. template - inline constexpr static bool has_duplicate_strings() { + constexpr static bool has_duplicate_strings() { if constexpr (_i >= num_fields_) { return false; } else { @@ -209,7 +192,7 @@ class Literal { } template - inline constexpr static bool is_duplicate() { + constexpr static bool is_duplicate() { using FieldType1 = typename std::tuple_element<_i, FieldsType>::type; using FieldType2 = typename std::tuple_element<_j, FieldsType>::type; if constexpr (FieldType1::field_ == FieldType2::field_) { @@ -239,7 +222,7 @@ class Literal { /// Finds the correct index associated with /// the string at compile time within the Literal's own fields. template - inline constexpr static auto find_name_within_own_fields() { + constexpr static auto find_name_within_own_fields() { using FieldType = typename std::tuple_element<_i, FieldsType>::type; return FieldType::field_; } @@ -247,7 +230,7 @@ class Literal { /// Finds the correct value associated with /// the string at run time. template - inline static Result find_value(const std::string& _str) { + static Result find_value(const std::string& _str) { using FieldType = typename std::tuple_element<_i, FieldsType>::type; if (FieldType::field_.str() == _str) { return _i; @@ -276,7 +259,7 @@ class Literal { /// Whether the literal contains this string. template - inline static bool has_value(const std::string& _str) { + static bool has_value(const std::string& _str) { using FieldType = typename std::tuple_element<_i, FieldsType>::type; if (FieldType::field_.str() == _str) { return true; diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index d1dd877d..350e0f4e 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -70,16 +70,10 @@ class NamedTuple { } /// Copy constructor. - NamedTuple(const NamedTuple& _other) - : values_(_other.values_) { - // Duplicate field checks not required - has already been checked. - } + NamedTuple(const NamedTuple& _other) = default; /// Move constructor. - NamedTuple(NamedTuple&& _other) - : values_(std::move(_other.values_)) { - // Duplicate field checks not required - has already been checked. - } + NamedTuple(NamedTuple&& _other) = default; /// Copy constructor. template @@ -175,62 +169,53 @@ class NamedTuple { /// Gets a field by index. template - inline auto& get() { + auto& get() { return rfl::get<_index>(*this); } /// Gets a field by name. template - inline auto& get() { + auto& get() { return rfl::get<_field_name>(*this); } /// Gets a field by the field type. template - inline auto& get() { + auto& get() { return rfl::get(*this); } /// Gets a field by index. template - inline const auto& get() const { + const auto& get() const { return rfl::get<_index>(*this); } /// Gets a field by name. template - inline const auto& get() const { + const auto& get() const { return rfl::get<_field_name>(*this); } /// Gets a field by the field type. template - inline const auto& get() const { + const auto& get() const { return rfl::get(*this); } /// Returns the results wrapped in a field. template - inline auto get_field() const { + 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; - } + const NamedTuple& _other) = default; /// Move assignment operator. NamedTuple& operator=( - NamedTuple&& _other) noexcept { - if (this == &_other) { - return *this; - } - values_ = std::move(_other.values_); - return *this; - } + NamedTuple&& _other) noexcept = default; /// Replaces one or several fields, returning a new version /// with the non-replaced fields left unchanged. diff --git a/include/rfl/Ref.hpp b/include/rfl/Ref.hpp index 229ec0ce..866791ea 100644 --- a/include/rfl/Ref.hpp +++ b/include/rfl/Ref.hpp @@ -18,9 +18,9 @@ class Ref { return Ref(std::make_shared(std::forward(_args)...)); } - Ref(const Ref& _other) : ptr_(_other.ptr_){}; + Ref(const Ref& _other) = default; - Ref(Ref&& _other) : ptr_(std::move(_other.ptr_)) {} + Ref(Ref&& _other) = default; template Ref(const Ref& _other) : ptr_(_other.ptr()) {} @@ -28,16 +28,16 @@ class Ref { ~Ref() = default; /// Returns a pointer to the underlying object - inline T* get() const { return ptr_.get(); } + T* get() const { return ptr_.get(); } /// Returns the underlying object. - inline T& operator*() const { return *ptr_; } + T& operator*() const { return *ptr_; } /// Returns the underlying object. - inline T* operator->() const { return ptr_.get(); } + T* operator->() const { return ptr_.get(); } /// Returns the underlying shared_ptr - inline const std::shared_ptr& ptr() const { return ptr_; } + const std::shared_ptr& ptr() const { return ptr_; } /// Copy assignment operator. template @@ -47,20 +47,10 @@ class Ref { } /// Move assignment operator - inline Ref& operator=(Ref&& _other) noexcept { - if (&_other != this) { - ptr_ = std::move(_other.ptr_); - } - return *this; - } + Ref& operator=(Ref&& _other) noexcept = default; /// Copy assignment operator - inline Ref& operator=(const Ref& _other) { - if (&_other != this) { - ptr_ = _other.ptr_; - } - return *this; - } + Ref& operator=(const Ref& _other) = default; private: /// Only make is allowed to use this constructor. diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index 9e84d24f..5423fd1e 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -191,25 +191,15 @@ class Result { 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; - } + Result& operator=(const Result& _other) = default; /// Assigns the underlying object. - inline Result& operator=(Result&& _other) { - if (this != &_other) { - t_or_err_ = std::move(_other.t_or_err_); - } - return *this; - } + Result& operator=(Result&& _other) = default; /// Assigns the underlying object. template , bool>::type = true> - inline auto& operator=(const Result& _other) { + 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; diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index c832c456..7d3a9d8b 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -20,13 +20,11 @@ struct TaggedUnion { TaggedUnion(VariantType&& _variant) noexcept : variant_(std::move(_variant)) {} - TaggedUnion( - const TaggedUnion<_discriminator, NamedTupleTypes...>& _tagged_union) - : variant_(_tagged_union.variant_) {} + TaggedUnion(const TaggedUnion<_discriminator, NamedTupleTypes...>& + _tagged_union) = default; TaggedUnion(TaggedUnion<_discriminator, NamedTupleTypes...>&& - _tagged_union) noexcept - : variant_(std::move(_tagged_union.variant_)) {} + _tagged_union) noexcept = default; template , @@ -41,14 +39,14 @@ struct TaggedUnion { ~TaggedUnion() = default; /// Assigns the underlying object. - inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( const VariantType& _variant) { variant_ = _variant; return *this; } /// Assigns the underlying object. - inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( VariantType&& _variant) { variant_ = std::move(_variant); return *this; @@ -58,8 +56,7 @@ struct TaggedUnion { template , bool>::type = true> - inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - T&& _variant) { + TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(T&& _variant) { variant_ = std::forward(_variant); return *this; } @@ -68,35 +65,26 @@ struct TaggedUnion { template , bool>::type = true> - inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + 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; - } + TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + const TaggedUnion<_discriminator, NamedTupleTypes...>& _other) = + default; /// Assigns the underlying object. - inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( - TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) { - if (this != &_other) { - variant_ = std::move(_other.variant_); - } - return *this; - } + TaggedUnion<_discriminator, NamedTupleTypes...>& operator=( + TaggedUnion<_discriminator, NamedTupleTypes...>&& _other) = default; /// Returns the underlying variant. - inline VariantType& variant() { return variant_; } + VariantType& variant() { return variant_; } /// Returns the underlying variant. - inline const VariantType& variant() const { return variant_; } + const VariantType& variant() const { return variant_; } /// The underlying variant - a TaggedUnion is a thin wrapper /// around a variant that is mainly used for parsing.