From a3f693c5e8a52e75fabbbbd0527a6ccdedcde373 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 2 Oct 2023 22:15:31 +0200 Subject: [PATCH] Beautification and optimization --- include/rfl/Enum.hpp | 5 ++--- include/rfl/Field.hpp | 16 ++++++++-------- include/rfl/NamedTuple.hpp | 2 +- include/rfl/Result.hpp | 4 ++-- include/rfl/TaggedUnion.hpp | 28 +++++++++++++++++++--------- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/rfl/Enum.hpp b/include/rfl/Enum.hpp index c1b1626f..bdf365d1 100644 --- a/include/rfl/Enum.hpp +++ b/include/rfl/Enum.hpp @@ -22,8 +22,7 @@ struct Enum { "Duplicate field names are not allowed"); } - Enum(VariantType&& _variant) - : variant_(std::forward(_variant)) { + Enum(VariantType&& _variant) : variant_(std::move(_variant)) { static_assert(internal::no_duplicate_field_names(), "Duplicate field names are not allowed"); } @@ -41,7 +40,7 @@ struct Enum { inline void operator=(VariantType&& _variant) { static_assert(internal::no_duplicate_field_names(), "Duplicate field names are not allowed"); - variant_ = std::forward(_variant); + variant_ = std::move(_variant); } /// Returns the underlying variant. diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 42db5dd0..5411bd68 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -17,19 +17,19 @@ namespace rfl { template struct Field { /// The underlying type. - using Type = std::decay_t<_Type>; + using Type = _Type; /// The name of the field. using Name = rfl::Literal<_name>; Field(const Type& _value) : value_(_value) {} - Field(Type&& _value) : value_(std::forward(_value)) {} + Field(Type&& _value) noexcept : value_(std::move(_value)) {} Field(const Field<_name, Type>& _field) : value_(_field.get()) {} - Field(Field<_name, Type>&& _field) - : value_(std::forward(_field.value_)) {} + Field(Field<_name, Type>&& _field) noexcept + : value_(std::move(_field.value_)) {} template Field(const Field<_name, T>& _field) : value_(_field.get()) {} @@ -43,7 +43,7 @@ struct Field { template , bool>::type = true> - Field(T&& _value) : value_(_value) {} + Field(T&& _value) noexcept : value_(std::forward(_value)) {} template , bool>::type = true> @@ -73,8 +73,8 @@ struct Field { inline void operator=(const Type& _value) { value_ = _value; } /// Assigns the underlying object. - inline void operator=(Type&& _value) { - value_ = std::forward(_value); + inline void operator=(Type&& _value) noexcept { + value_ = std::move(_value); } /// Assigns the underlying object. @@ -118,7 +118,7 @@ struct Field { inline void set(const Type& _value) { value_ = _value; } /// Assigns the underlying object. - inline void set(Type&& _value) { value_ = std::forward(_value); } + inline void set(Type&& _value) { value_ = std::move(_value); } /// Returns the underlying object. inline Type& value() { return value_; } diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 49ed61fd..9dbb0bab 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -100,7 +100,7 @@ class NamedTuple { template auto add(const std::tuple& _tuple, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple), + return add_tuple(_tuple, std::make_index_sequence{}) .replace(_tail...); } else { diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index e07193b2..a7b507ad 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -66,11 +66,11 @@ class Result { Result(const T& _val) : t_or_err_(_val) {} - Result(T&& _val) : t_or_err_(std::forward(_val)) {} + Result(T&& _val) noexcept : t_or_err_(std::forward(_val)) {} Result(const Error& _err) : t_or_err_(_err) {} - Result(Error&& _err) : t_or_err_(std::forward(_err)) {} + Result(Error&& _err) noexcept : t_or_err_(std::forward(_err)) {} template , bool>::type = true> diff --git a/include/rfl/TaggedUnion.hpp b/include/rfl/TaggedUnion.hpp index 8b5d9b0d..67837bda 100644 --- a/include/rfl/TaggedUnion.hpp +++ b/include/rfl/TaggedUnion.hpp @@ -17,14 +17,15 @@ struct TaggedUnion { TaggedUnion(const VariantType& _variant) : variant_(_variant) {} - TaggedUnion(VariantType&& _variant) - : variant_(std::forward(_variant)) {} + TaggedUnion(VariantType&& _variant) noexcept + : variant_(std::move(_variant)) {} TaggedUnion(const TaggedUnion<_discriminator, VariantType>& _tagged_union) : variant_(_tagged_union.variant_) {} - TaggedUnion(TaggedUnion<_discriminator, VariantType>&& _tagged_union) - : variant_(std::forward(_tagged_union.variant_)) {} + TaggedUnion( + TaggedUnion<_discriminator, VariantType>&& _tagged_union) noexcept + : variant_(std::move(_tagged_union.variant_)) {} template , @@ -34,7 +35,7 @@ struct TaggedUnion { template , bool>::type = true> - TaggedUnion(T&& _t) : variant_(std::forward(_t)) {} + TaggedUnion(T&& _t) noexcept : variant_(std::forward(_t)) {} ~TaggedUnion() = default; @@ -42,8 +43,16 @@ struct TaggedUnion { inline void operator=(const VariantType& _variant) { variant_ = _variant; } /// Assigns the underlying object. - inline void operator=(VariantType&& _variant) { - variant_ = std::forward(_variant); + 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. @@ -61,8 +70,9 @@ struct TaggedUnion { } /// Assigns the underlying object. - inline void operator=(TaggedUnion<_discriminator, VariantType>&& _other) { - variant_ = std::forward(_other.variant_); + inline void operator=( + TaggedUnion<_discriminator, VariantType>&& _other) noexcept { + variant_ = std::move(_other.variant_); } /// Returns the underlying variant.