refactor(memory): remove FNoInit versions of MakeUnique() and MakeShared()

This commit is contained in:
_Redstone_c_ 2023-02-13 20:58:08 +08:00
parent 05d59ed323
commit 0c54bbe2ac
4 changed files with 29 additions and 68 deletions

View File

@ -379,12 +379,6 @@ void TestUniquePointer()
always_check( FCounter::Num == 0);
always_check(FArrayDeleter::Num == 4);
{
TUniquePtr<int32> Temp = MakeUnique<int32>(NoInit);
*Temp = 15;
always_check(*Temp.Get() = 15);
}
{
TUniquePtr<int32> Temp = MakeUnique<int32>();
*Temp = 15;
@ -450,12 +444,6 @@ void TestUniquePointer()
always_check(FCounter::Num == 0);
always_check(FDeleter::Num == 4);
{
TUniquePtr<int32[]> Temp = MakeUnique<int32[]>(4, NoInit);
Temp[0] = 15;
always_check(Temp.Get()[0] = 15);
}
{
TUniquePtr<int32[]> Temp = MakeUnique<int32[]>(4);
Temp[0] = 15;
@ -581,12 +569,6 @@ void TestSharedPointer()
always_check(FCounter::Num == 0);
always_check(FDeleter::Num == 4);
{
TSharedRef<int32[]> Temp = MakeShared<int32[]>(4, NoInit);
Temp[0] = 15;
always_check(Temp.Get()[0] = 15);
}
{
TSharedRef<int32[]> Temp = MakeShared<int32[]>(4);
Temp[0] = 15;
@ -684,12 +666,6 @@ void TestSharedPointer()
always_check(FCounter::Num == 0);
always_check(FDeleter::Num == 5);
{
TSharedPtr<int32[]> Temp = MakeShared<int32[]>(4, NoInit);
Temp[0] = 15;
always_check(Temp.Get()[0] = 15);
}
{
TSharedPtr<int32[]> Temp = MakeShared<int32[]>(4);
Temp[0] = 15;

View File

@ -241,10 +241,18 @@ class TSharedControllerWithObject final : public FSharedController
{
public:
FORCEINLINE explicit TSharedControllerWithObject(FNoInit) requires (!CConstructibleFrom<T, FNoInit>) { new (&Storage) T; }
template <typename... Ts> requires (CConstructibleFrom<T, Ts...>)
FORCEINLINE explicit TSharedControllerWithObject(Ts&&... Args) { new (&Storage) T(Forward<Ts>(Args)...); }
template <typename... Ts>
FORCEINLINE explicit TSharedControllerWithObject(Ts&&... Args)
{
if constexpr (sizeof...(Ts) == 0)
{
new (&Storage) T;
}
else
{
new (&Storage) T(Forward<Ts>(Args)...);
}
}
virtual ~TSharedControllerWithObject() = default;
@ -263,20 +271,11 @@ class TSharedControllerWithArray final : public FSharedController
{
public:
static TSharedControllerWithArray* New(size_t N, FNoInit)
{
void* Buffer = Memory::Malloc(sizeof(TSharedControllerWithArray) + sizeof(T) * (N - 1), alignof(TSharedControllerWithArray));
const auto Controller = new (Buffer) TSharedControllerWithArray(N);
const T* ElementPtr = new (Controller->GetPointer()) T[N];
check(ElementPtr == Controller->GetPointer());
return Controller;
}
static TSharedControllerWithArray* New(size_t N)
{
void* Buffer = Memory::Malloc(sizeof(TSharedControllerWithArray) + sizeof(T) * (N - 1), alignof(TSharedControllerWithArray));
const auto Controller = new (Buffer) TSharedControllerWithArray(N);
const T* ElementPtr = new (Controller->GetPointer()) T[N]();
const T* ElementPtr = new (Controller->GetPointer()) T[N];
check(ElementPtr == Controller->GetPointer());
return Controller;
}
@ -674,7 +673,7 @@ public:
return ControllerWithDeleter != nullptr ? &ControllerWithDeleter->GetDeleter() : nullptr;
}
/** @return The a reference or pointer to the object owned by *this, i.e. Get(). */
/** @return The reference or pointer to the object owned by *this, i.e. Get(). */
NODISCARD FORCEINLINE constexpr T& operator*() const { return *Get(); }
NODISCARD FORCEINLINE constexpr T* operator->() const { return Get(); }
@ -1143,7 +1142,7 @@ public:
NODISCARD FORCEINLINE constexpr bool IsValid() const { return Get() != nullptr; }
NODISCARD FORCEINLINE constexpr explicit operator bool() const { return Get() != nullptr; }
/** @return The a reference or pointer to the object owned by *this, i.e. Get(). */
/** @return The reference or pointer to the object owned by *this, i.e. Get(). */
NODISCARD FORCEINLINE constexpr T& operator*() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return *Get(); }
NODISCARD FORCEINLINE constexpr T* operator->() const { checkf(IsValid(), TEXT("Read access violation. Please check IsValid().")); return Get(); }
@ -1748,14 +1747,6 @@ private:
};
/** Constructs an object of type T and wraps it in a TSharedRef or TSharedPtr. Without initialization. */
template <typename T> requires (CObject<T> && !CArray<T> && !CConstructibleFrom<T, FNoInit> && CDestructible<T>)
NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy<T> MakeShared(FNoInit)
{
const auto Controller = new NAMESPACE_PRIVATE::TSharedControllerWithObject<T>(NoInit);
return NAMESPACE_PRIVATE::TSharedProxy<T>(Controller->GetPointer(), Controller);
}
/** Constructs an object of type T and wraps it in a TSharedRef or TSharedPtr. */
template <typename T, typename... Ts> requires (CObject<T> && !CArray<T> && CConstructibleFrom<T, Ts...> && CDestructible<T>)
NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy<T> MakeShared(Ts&&... Args)
@ -1764,14 +1755,6 @@ NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy<T> MakeShared(Ts&&... Args
return NAMESPACE_PRIVATE::TSharedProxy<T>(Controller->GetPointer(), Controller);
}
/** Constructs an array of type T and wraps it in a TSharedRef or TSharedPtr. Without initialization. */
template <typename T> requires (CUnboundedArray<T> && CDefaultConstructible<TRemoveExtent<T>> && CDestructible<TRemoveExtent<T>>)
NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy<T> MakeShared(size_t N, FNoInit)
{
const auto Controller = NAMESPACE_PRIVATE::TSharedControllerWithArray<TRemoveExtent<T>>::New(N, NoInit);
return NAMESPACE_PRIVATE::TSharedProxy<T>(Controller->GetPointer(), Controller);
}
/** Constructs an array of type T and wraps it in a TSharedRef or TSharedPtr. */
template <typename T> requires (CUnboundedArray<T> && CDefaultConstructible<TRemoveExtent<T>> && CDestructible<TRemoveExtent<T>>)
NODISCARD FORCEINLINE NAMESPACE_PRIVATE::TSharedProxy<T> MakeShared(size_t N)

View File

@ -659,21 +659,23 @@ private:
};
/** Constructs an object of type T and wraps it in a TUniquePtr. Without initialization. */
template <typename T> requires (CObject<T> && !CArray<T> && !CConstructibleFrom<T, FNoInit> && CDestructible<T>)
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(FNoInit) { return TUniquePtr<T>(new T); }
/** Constructs an object of type T and wraps it in a TUniquePtr. */
template <typename T, typename... Ts> requires (CObject<T> && !CArray<T> && CConstructibleFrom<T, Ts...> && CDestructible<T>)
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(Ts&&... Args) { return TUniquePtr<T>(new T(Forward<Ts>(Args)...)); }
/** Constructs an array of type T and wraps it in a TUniquePtr. Without initialization. */
template <typename T> requires (CUnboundedArray<T> && CDefaultConstructible<TRemoveExtent<T>> && CDestructible<TRemoveExtent<T>>)
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(size_t N, FNoInit) { return TUniquePtr<T>(new TRemoveExtent<T>[N]); }
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(Ts&&... Args)
{
if constexpr (sizeof...(Ts) == 0)
{
return TUniquePtr<T>(new T);
}
else
{
return TUniquePtr<T>(new T(Forward<Ts>(Args)...));
}
}
/** Constructs an array of type T and wraps it in a TUniquePtr. */
template <typename T> requires (CUnboundedArray<T> && CDefaultConstructible<TRemoveExtent<T>> && CDestructible<TRemoveExtent<T>>)
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(size_t N) { return TUniquePtr<T>(new TRemoveExtent<T>[N]()); }
NODISCARD FORCEINLINE constexpr TUniquePtr<T> MakeUnique(size_t N) { return TUniquePtr<T>(new TRemoveExtent<T>[N]); }
/** Construction of arrays of known bound is disallowed. */
template <typename T, typename... Ts> requires (CBoundedArray<T>)

View File

@ -245,14 +245,14 @@ public:
}
/** Check if the optional value is equivalent to 'InValue'. */
template <typename T = OptionalType> requires (!CTOptional<T>&& CWeaklyEqualityComparable<OptionalType, T>)
template <typename T = OptionalType> requires (!CTOptional<T> && CWeaklyEqualityComparable<OptionalType, T>)
NODISCARD FORCEINLINE constexpr bool operator==(const T& InValue) const&
{
return IsValid() ? GetValue() == InValue : false;
}
/** Check that the optional value is in ordered relationship with 'InValue'. */
template <typename T = OptionalType> requires (!CTOptional<T>&& CSynthThreeWayComparable<OptionalType, T>)
template <typename T = OptionalType> requires (!CTOptional<T> && CSynthThreeWayComparable<OptionalType, T>)
NODISCARD FORCEINLINE constexpr partial_ordering operator<=>(const T& InValue) const&
{
return IsValid() ? SynthThreeWayCompare(GetValue(), InValue) : partial_ordering::unordered;