mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-07 03:06:18 +08:00
Make sure that we are consistently following the Rule of 5
This commit is contained in:
@@ -18,6 +18,8 @@ class Box {
|
||||
return Box<T>(std::make_unique<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
Box(const Box<T>& _other) = delete;
|
||||
|
||||
Box(Box<T>&& _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<T>& operator=(const Box<T>& _other) = delete;
|
||||
|
||||
/// Move assignment operator
|
||||
inline Box<T>& operator=(Box<T>&& _other) noexcept {
|
||||
if (&_other != this) {
|
||||
ptr_ = std::move(_other.ptr_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Returns the underlying object.
|
||||
inline T& operator*() const { return *ptr_; }
|
||||
|
||||
|
||||
+27
-14
@@ -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 <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
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 <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
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 <class T>
|
||||
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 <class T>
|
||||
inline void operator=(Field<_name, T>&& _field) {
|
||||
value_ = _field.get();
|
||||
inline auto& operator=(Field<_name, T>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
|
||||
+21
-10
@@ -19,9 +19,9 @@ struct Flatten {
|
||||
|
||||
Flatten(Type&& _value) noexcept : value_(std::forward<Type>(_value)) {}
|
||||
|
||||
Flatten(const Flatten<Type>& _f) : value_(_f.get()) {}
|
||||
Flatten(const Flatten<_Type>& _f) : value_(_f.get()) {}
|
||||
|
||||
Flatten(Flatten<Type>&& _f) noexcept
|
||||
Flatten(Flatten<_Type>&& _f) noexcept
|
||||
: value_(std::forward<Type>(_f.value_)) {}
|
||||
|
||||
template <class T>
|
||||
@@ -50,13 +50,13 @@ struct Flatten {
|
||||
inline const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline Flatten<Type> operator=(const Type& _value) {
|
||||
inline Flatten<_Type>& operator=(const _Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline Flatten<Type> operator=(Type&& _value) {
|
||||
inline Flatten<_Type>& operator=(_Type&& _value) {
|
||||
value_ = std::forward<Type>(_value);
|
||||
return *this;
|
||||
}
|
||||
@@ -64,28 +64,39 @@ struct Flatten {
|
||||
/// Assigns the underlying object.
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
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<Type>& _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<Type>&& _f) {
|
||||
value_ = std::forward<Type>(_f.value_);
|
||||
inline Flatten<_Type>& operator=(Flatten<_Type>&& _f) {
|
||||
if (&_f != this) {
|
||||
value_ = std::move(_f.value_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(const Flatten<T>& _f) {
|
||||
inline Flatten<_Type>& operator=(const Flatten<T>& _f) {
|
||||
value_ = _f.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
inline void operator=(Flatten<T>&& _f) {
|
||||
inline Flatten<_Type>& operator=(Flatten<T>&& _f) {
|
||||
value_ = std::forward<T>(_f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
|
||||
@@ -119,19 +119,23 @@ class Literal {
|
||||
}
|
||||
|
||||
/// Assigns from another literal.
|
||||
Literal<fields_...> operator=(const Literal<fields_...>& _other) {
|
||||
value_ = _other.value_;
|
||||
Literal<fields_...>& operator=(const Literal<fields_...>& _other) {
|
||||
if (this != &_other) {
|
||||
value_ = _other.value_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns from another literal.
|
||||
Literal<fields_...> operator=(Literal<fields_...>&& _other) noexcept {
|
||||
value_ = _other.value_;
|
||||
Literal<fields_...>& operator=(Literal<fields_...>&& _other) noexcept {
|
||||
if (this != &_other) {
|
||||
value_ = _other.value_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the literal from a string
|
||||
Literal<fields_...> operator=(const std::string& _str) {
|
||||
Literal<fields_...>& operator=(const std::string& _str) {
|
||||
value_ = find_value(_str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -98,6 +98,19 @@ class NamedTuple {
|
||||
|
||||
~NamedTuple() = default;
|
||||
|
||||
/// Returns a new named tuple with additional fields.
|
||||
template <class Head, class... Tail>
|
||||
auto add(Head&& _head, Tail&&... _tail) {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)))
|
||||
.add(std::forward<Tail>(_tail)...);
|
||||
} else {
|
||||
return NamedTuple<FieldTypes..., std::decay_t<Head>>(
|
||||
make_fields<1, Head>(std::forward<Head>(_head)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a new named tuple with additional fields.
|
||||
template <class Head, class... Tail>
|
||||
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 <class... TupContent, class... Tail>
|
||||
auto add(std::tuple<TupContent...>&& _tuple, Tail&&... _tail) {
|
||||
if constexpr (sizeof...(Tail) > 0) {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{})
|
||||
.replace(_tail...);
|
||||
} else {
|
||||
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
|
||||
std::make_index_sequence<sizeof...(TupContent)>{});
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for std::tuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
@@ -139,6 +166,13 @@ class NamedTuple {
|
||||
}
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
@@ -146,6 +180,13 @@ class NamedTuple {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
auto add(const NamedTuple<TupContent...>& _named_tuple, Tail&&... _tail) {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
|
||||
}
|
||||
|
||||
/// Template specialization for NamedTuple, so we can pass fields from other
|
||||
/// named tuples.
|
||||
template <class... TupContent, class... Tail>
|
||||
@@ -154,6 +195,9 @@ class NamedTuple {
|
||||
return add(_named_tuple.fields(), std::forward<Tail>(_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 <class Tuple, size_t... Is>
|
||||
constexpr auto add_tuple(Tuple&& _tuple, const std::index_sequence<Is...>) {
|
||||
return add(std::get<Is>(_tuple)...);
|
||||
}
|
||||
|
||||
/// Adds the elements of a tuple to a newly created named tuple,
|
||||
/// and other elements to a newly created named tuple.
|
||||
template <class Tuple, size_t... Is>
|
||||
@@ -308,6 +359,28 @@ class NamedTuple {
|
||||
return add(std::get<Is>(_tuple)...);
|
||||
}
|
||||
|
||||
/// Generates the fields.
|
||||
template <int num_additional_fields = 0, class... Args>
|
||||
auto make_fields(Args&&... _args) {
|
||||
constexpr auto size = sizeof...(Args) - num_additional_fields;
|
||||
constexpr auto num_fields = std::tuple_size<Fields>{};
|
||||
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>(_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<i, Fields>::type;
|
||||
using T = std::decay_t<typename FieldType::Type>;
|
||||
return make_fields<num_additional_fields>(
|
||||
FieldType(std::forward<T>(std::get<i>(values_))),
|
||||
std::forward<Args>(_args)...);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates the fields.
|
||||
template <int num_additional_fields = 0, class... Args>
|
||||
auto make_fields(Args&&... _args) const {
|
||||
|
||||
+19
-12
@@ -18,18 +18,9 @@ class Ref {
|
||||
return Ref<T>(std::make_shared<T>(std::forward<Args>(_args)...));
|
||||
}
|
||||
|
||||
/// Wrapper around a shared_ptr, leads to a runtime error,
|
||||
/// if the shared_ptr is not allocated.
|
||||
explicit Ref(const std::shared_ptr<T>& _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<T>& _other) : ptr_(_other.ptr_){};
|
||||
|
||||
Ref(Ref<T>&& _other) : ptr_(std::move(_other.ptr_)) {}
|
||||
|
||||
template <class Y>
|
||||
Ref(const Ref<Y>& _other) : ptr_(_other.ptr()) {}
|
||||
@@ -55,6 +46,22 @@ class Ref {
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Move assignment operator
|
||||
inline Ref<T>& operator=(Ref<T>&& _other) noexcept {
|
||||
if (&_other != this) {
|
||||
ptr_ = std::move(_other.ptr_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Copy assignment operator
|
||||
inline Ref<T>& operator=(const Ref<T>& _other) {
|
||||
if (&_other != this) {
|
||||
ptr_ = _other.ptr_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
/// Only make is allowed to use this constructor.
|
||||
explicit Ref(std::shared_ptr<T>&& _ptr) : ptr_(std::move(_ptr)) {}
|
||||
|
||||
+18
-1
@@ -182,12 +182,29 @@ class Result {
|
||||
/// undefined behavior, if the result contains an error.
|
||||
const T& operator*() const { return *std::get_if<T>(&t_or_err_); }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline Result<T>& operator=(const Result<T>& _other) {
|
||||
if (this != &_other) {
|
||||
t_or_err_ = _other.t_or_err_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
inline Result<T>& operator=(Result<T>&& _other) {
|
||||
if (this != &_other) {
|
||||
t_or_err_ = std::move(_other.t_or_err_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
inline void operator=(const Result<U>& _other) {
|
||||
inline auto& operator=(const Result<U>& _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<T> and returns
|
||||
|
||||
+42
-26
@@ -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 <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
inline void operator=(T&& _variant) {
|
||||
variant_ = std::forward<T>(_variant);
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
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 <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
bool>::type = true>
|
||||
inline TaggedUnion<_discriminator, NamedTupleTypes...>& operator=(
|
||||
T&& _variant) {
|
||||
variant_ = std::forward<T>(_variant);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T,
|
||||
typename std::enable_if<std::is_convertible_v<T, VariantType>,
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user