refactor(templates): remove TSharedPtr<T>::ToSharedRef(T*) function overload

This commit is contained in:
_Redstone_c_ 2023-01-15 18:41:38 +08:00
parent 5c2c1b4a48
commit c38714220e

View File

@ -465,6 +465,10 @@ private:
}; };
struct FSharedPtrConstructor { explicit FSharedPtrConstructor() = default; };
inline constexpr FSharedPtrConstructor SharedPtrConstructor{ };
NAMESPACE_PRIVATE_END NAMESPACE_PRIVATE_END
/** /**
@ -714,20 +718,16 @@ private:
T* Pointer; T* Pointer;
NAMESPACE_PRIVATE::FSharedController* Controller; NAMESPACE_PRIVATE::FSharedController* Controller;
template <typename U> FORCEINLINE TSharedRef(NAMESPACE_PRIVATE::FSharedPtrConstructor, const TSharedPtr<T>& InValue)
FORCEINLINE TSharedRef(const TSharedPtr<U>& InValue, T* InPtr) : Pointer(InValue.Pointer), Controller(InValue.Controller)
: Pointer(InPtr), Controller(InValue.Controller)
{ {
Controller->AddSharedReference(); Controller->AddSharedReference();
} }
template <typename U> FORCEINLINE TSharedRef(NAMESPACE_PRIVATE::FSharedPtrConstructor, TSharedPtr<T>&& InValue)
FORCEINLINE TSharedRef(TSharedPtr<U>&& InValue, T* InPtr) : Pointer(Exchange(InValue.Pointer, nullptr))
: Pointer(InPtr), Controller(InValue.Controller) , Controller(Exchange(InValue.Controller, nullptr))
{ { }
InValue.Pointer = nullptr;
InValue.Controller = nullptr;
}
FORCEINLINE TSharedRef(T* InPtr, NAMESPACE_PRIVATE::FSharedController* InController) FORCEINLINE TSharedRef(T* InPtr, NAMESPACE_PRIVATE::FSharedController* InController)
: Pointer(InPtr), Controller(InController) : Pointer(InPtr), Controller(InController)
@ -936,20 +936,16 @@ private:
T* Pointer; T* Pointer;
NAMESPACE_PRIVATE::FSharedController* Controller; NAMESPACE_PRIVATE::FSharedController* Controller;
template <typename U> FORCEINLINE TSharedRef(NAMESPACE_PRIVATE::FSharedPtrConstructor, const TSharedPtr<T[]>& InValue)
FORCEINLINE TSharedRef(const TSharedPtr<U>& InValue, T* InPtr) : Pointer(InValue.Pointer), Controller(InValue.Controller)
: Pointer(InPtr), Controller(InValue.Controller)
{ {
Controller->AddSharedReference(); Controller->AddSharedReference();
} }
template <typename U> FORCEINLINE TSharedRef(NAMESPACE_PRIVATE::FSharedPtrConstructor, TSharedPtr<T[]>&& InValue)
FORCEINLINE TSharedRef(TSharedPtr<U>&& InValue, T* InPtr) : Pointer(Exchange(InValue.Pointer, nullptr))
: Pointer(InPtr), Controller(InValue.Controller) , Controller(Exchange(InValue.Controller, nullptr))
{ { }
InValue.Pointer = nullptr;
InValue.Controller = nullptr;
}
FORCEINLINE TSharedRef(T* InPtr, NAMESPACE_PRIVATE::FSharedController* InController) FORCEINLINE TSharedRef(T* InPtr, NAMESPACE_PRIVATE::FSharedController* InController)
: Pointer(InPtr), Controller(InController) : Pointer(InPtr), Controller(InController)
@ -1111,25 +1107,19 @@ public:
NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(T* InPtr) const& { return Get() <=> InPtr; } NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(T* InPtr) const& { return Get() <=> InPtr; }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */ /** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T> ToSharedRef(T* InPtr) const& FORCEINLINE TSharedRef<T> ToSharedRef() const&
{ {
checkf(IsValid() && InPtr != nullptr, TEXT("TSharedRef cannot be initialized by nullptr.")); checkf(IsValid(), TEXT("TSharedRef cannot be initialized by nullptr."));
return TSharedRef<T>(*this, InPtr); return TSharedRef<T>(NAMESPACE_PRIVATE::SharedPtrConstructor, *this);
} }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */ /** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T> ToSharedRef(T* InPtr) && FORCEINLINE TSharedRef<T> ToSharedRef() &&
{ {
checkf(IsValid() && InPtr != nullptr, TEXT("TSharedRef cannot be initialized by nullptr.")); checkf(IsValid(), TEXT("TSharedRef cannot be initialized by nullptr."));
return TSharedRef<T>(MoveTemp(*this), InPtr); return TSharedRef<T>(NAMESPACE_PRIVATE::SharedPtrConstructor, *this);
} }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T> ToSharedRef() const& { return ToSharedRef(Get()); }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T> ToSharedRef() && { return ToSharedRef(Get()); }
/** Replaces the managed object. */ /** Replaces the managed object. */
FORCEINLINE void Reset(T* InPtr = nullptr) { *this = MoveTemp(TSharedPtr(InPtr)); } FORCEINLINE void Reset(T* InPtr = nullptr) { *this = MoveTemp(TSharedPtr(InPtr)); }
@ -1385,27 +1375,19 @@ public:
NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(U InPtr) const& { return Get() <=> InPtr; } NODISCARD FORCEINLINE constexpr strong_ordering operator<=>(U InPtr) const& { return Get() <=> InPtr; }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */ /** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
template <typename U = T*> requires (CPointer<U> && CConvertibleTo<TRemovePointer<U>(*)[], T(*)[]>) FORCEINLINE TSharedRef<T[]> ToSharedRef() const&
FORCEINLINE TSharedRef<T[]> ToSharedRef(U InPtr) const&
{ {
checkf(IsValid() && InPtr != nullptr, TEXT("TSharedRef cannot be initialized by nullptr.")); checkf(IsValid(), TEXT("TSharedRef cannot be initialized by nullptr."));
return TSharedRef<T[]>(*this, InPtr); return TSharedRef<T[]>(NAMESPACE_PRIVATE::SharedPtrConstructor, *this);
} }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */ /** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
template <typename U = T*> requires (CPointer<U>&& CConvertibleTo<TRemovePointer<U>(*)[], T(*)[]>) FORCEINLINE TSharedRef<T[]> ToSharedRef() &&
FORCEINLINE TSharedRef<T[]> ToSharedRef(U InPtr)&&
{ {
checkf(IsValid() && InPtr != nullptr, TEXT("TSharedRef cannot be initialized by nullptr.")); checkf(IsValid(), TEXT("TSharedRef cannot be initialized by nullptr."));
return TSharedRef<T[]>(MoveTemp(*this), InPtr); return TSharedRef<T[]>(NAMESPACE_PRIVATE::SharedPtrConstructor, *this);
} }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T[]> ToSharedRef() const& { return ToSharedRef(Get()); }
/** Converts a shared pointer to a shared reference. The pointer MUST be valid or an assertion will trigger. */
FORCEINLINE TSharedRef<T[]> ToSharedRef() && { return ToSharedRef(Get()); }
/** Replaces the managed array. */ /** Replaces the managed array. */
template <typename U = T*> requires (CNullPointer<U> || (CPointer<U> && CConvertibleTo<TRemovePointer<U>(*)[], T(*)[]>)) template <typename U = T*> requires (CNullPointer<U> || (CPointer<U> && CConvertibleTo<TRemovePointer<U>(*)[], T(*)[]>))
FORCEINLINE void Reset(U InPtr = nullptr) { *this = MoveTemp(TSharedPtr(InPtr)); } FORCEINLINE void Reset(U InPtr = nullptr) { *this = MoveTemp(TSharedPtr(InPtr)); }