Compare commits

..

3 Commits

2 changed files with 52 additions and 52 deletions

View File

@ -151,7 +151,8 @@ public:
FORCEINLINE ValueType FetchFn(F&& Func, EMemoryOrder Order = EMemoryOrder::SequentiallyConsistent)
{
ValueType Temp(Load(EMemoryOrder::Relaxed));
while (!CompareExchange(Temp, InvokeResult<ValueType>(Forward<F>(Func), Temp), Order));
// We do a weak read here because we require a loop.
while (!CompareExchange(Temp, InvokeResult<ValueType>(Forward<F>(Func), Temp), Order, true));
return Temp;
}
@ -160,7 +161,8 @@ public:
FORCEINLINE ValueType FetchFn(F&& Func, EMemoryOrder Order = EMemoryOrder::SequentiallyConsistent) volatile
{
ValueType Temp(Load(EMemoryOrder::Relaxed));
while (!CompareExchange(Temp, InvokeResult<ValueType>(Forward<F>(Func), Temp), Order));
// We do a weak read here because we require a loop.
while (!CompareExchange(Temp, InvokeResult<ValueType>(Forward<F>(Func), Temp), Order, true));
return Temp;
}

View File

@ -1,7 +1,9 @@
#pragma once
#include "CoreTypes.h"
#include "Templates/Invoke.h"
#include "Templates/Utility.h"
#include "Templates/Noncopyable.h"
#include "TypeTraits/PrimaryType.h"
#include "TypeTraits/Miscellaneous.h"
#include "TypeTraits/TypeProperties.h"
@ -14,24 +16,24 @@ NAMESPACE_MODULE_BEGIN(Utility)
NAMESPACE_PRIVATE_BEGIN
template <typename T, typename E, bool = CEmpty<E> && !CFinal<E>>
class TUniquePtrStorage;
class TUniqueStorage;
template <typename T, typename E>
class TUniquePtrStorage<T, E, true> : private E
class TUniqueStorage<T, E, true> : private E
{
public:
FORCEINLINE constexpr TUniquePtrStorage() = delete;
FORCEINLINE constexpr TUniqueStorage() = delete;
FORCEINLINE constexpr TUniquePtrStorage(T* InPtr) : E(), Pointer(InPtr) { }
FORCEINLINE constexpr TUniqueStorage(T* InPtr) : E(), Pointer(InPtr) { }
template<typename U>
FORCEINLINE constexpr TUniquePtrStorage(T* InPtr, U&& InDeleter) : E(Forward<U>(InDeleter)), Pointer(InPtr) { }
FORCEINLINE constexpr TUniqueStorage(T* InPtr, U&& InDeleter) : E(Forward<U>(InDeleter)), Pointer(InPtr) { }
FORCEINLINE constexpr TUniquePtrStorage(const TUniquePtrStorage&) = delete;
FORCEINLINE constexpr TUniquePtrStorage(TUniquePtrStorage&& InValue) = default;
FORCEINLINE constexpr TUniquePtrStorage& operator=(const TUniquePtrStorage&) = delete;
FORCEINLINE constexpr TUniquePtrStorage& operator=(TUniquePtrStorage&&) = default;
FORCEINLINE constexpr TUniqueStorage(const TUniqueStorage&) = delete;
FORCEINLINE constexpr TUniqueStorage(TUniqueStorage&& InValue) = default;
FORCEINLINE constexpr TUniqueStorage& operator=(const TUniqueStorage&) = delete;
FORCEINLINE constexpr TUniqueStorage& operator=(TUniqueStorage&&) = default;
FORCEINLINE constexpr T*& GetPointer() { return Pointer; }
FORCEINLINE constexpr T* GetPointer() const { return Pointer; }
@ -48,21 +50,21 @@ private:
};
template <typename T, typename E>
class TUniquePtrStorage<T, E, false>
class TUniqueStorage<T, E, false>
{
public:
FORCEINLINE constexpr TUniquePtrStorage() = delete;
FORCEINLINE constexpr TUniqueStorage() = delete;
FORCEINLINE constexpr TUniquePtrStorage(T* InPtr) : E(), Pointer(InPtr) { }
FORCEINLINE constexpr TUniqueStorage(T* InPtr) : E(), Pointer(InPtr) { }
template<typename U>
FORCEINLINE constexpr TUniquePtrStorage(T* InPtr, U&& InDeleter) : Pointer(InPtr), Deleter(Forward<U>(InDeleter)) { }
FORCEINLINE constexpr TUniqueStorage(T* InPtr, U&& InDeleter) : Pointer(InPtr), Deleter(Forward<U>(InDeleter)) { }
FORCEINLINE constexpr TUniquePtrStorage(const TUniquePtrStorage&) = delete;
FORCEINLINE constexpr TUniquePtrStorage(TUniquePtrStorage&& InValue) = default;
FORCEINLINE constexpr TUniquePtrStorage& operator=(const TUniquePtrStorage&) = delete;
FORCEINLINE constexpr TUniquePtrStorage& operator=(TUniquePtrStorage&&) = default;
FORCEINLINE constexpr TUniqueStorage(const TUniqueStorage&) = delete;
FORCEINLINE constexpr TUniqueStorage(TUniqueStorage&& InValue) = default;
FORCEINLINE constexpr TUniqueStorage& operator=(const TUniqueStorage&) = delete;
FORCEINLINE constexpr TUniqueStorage& operator=(TUniqueStorage&&) = default;
FORCEINLINE constexpr T*& GetPointer() { return Pointer; }
FORCEINLINE constexpr T* GetPointer() const { return Pointer; }
@ -122,8 +124,7 @@ struct TDefaultDelete<T[]>
};
/** This is essentially a reference version of TUniquePtr. */
template <typename T, typename E = TDefaultDelete<T>> requires (CObject<T> && !CBoundedArray<T> && !CRValueReference<E>
&& ((!CArray<T> && CInvocable<E, T*>) || (CArray<T> && CInvocable<E, TRemoveExtent<T>*>)))
template <typename T, CInvocable<TRemoveExtent<T>*> E = TDefaultDelete<T>> requires (CObject<T> && !CBoundedArray<T> && (CDestructible<E> || CLValueReference<E>))
class TUniqueRef final : private FSingleton
{
public:
@ -151,7 +152,7 @@ public:
}
/** Destroy the owned object. */
FORCEINLINE constexpr ~TUniqueRef() { GetDeleter()(Get()); }
FORCEINLINE constexpr ~TUniqueRef() { Invoke(GetDeleter(), Get()); }
/** Compares the pointer values of two TUniqueRef. */
NODISCARD friend FORCEINLINE constexpr bool operator==(const TUniqueRef& LHS, const TUniqueRef& RHS) { return LHS.Get() == RHS.Get(); }
@ -172,7 +173,7 @@ public:
FORCEINLINE constexpr void Reset(T* InPtr)
{
checkf(InPtr != nullptr, TEXT("TUniqueRef cannot be initialized by nullptr. Please use TUniquePtr."));
GetDeleter()(Get());
Invoke(GetDeleter(), Get());
Storage.GetPointer() = InPtr;
}
@ -233,7 +234,7 @@ public:
private:
NAMESPACE_PRIVATE::TUniquePtrStorage<T, E> Storage;
NAMESPACE_PRIVATE::TUniqueStorage<T, E> Storage;
};
@ -271,7 +272,7 @@ public:
}
/** Destroy the owned array. */
FORCEINLINE constexpr ~TUniqueRef() { GetDeleter()(Get()); }
FORCEINLINE constexpr ~TUniqueRef() { Invoke(GetDeleter(), Get()); }
/** Compares the pointer values of two TUniqueRef. */
NODISCARD friend FORCEINLINE constexpr bool operator==(const TUniqueRef& LHS, const TUniqueRef& RHS) { return LHS.Get() == RHS.Get(); }
@ -295,7 +296,7 @@ public:
FORCEINLINE constexpr void Reset(U InPtr)
{
checkf(InPtr != nullptr, TEXT("TUniqueRef cannot be initialized by nullptr. Please use TUniquePtr."));
GetDeleter()(Get());
Invoke(GetDeleter(), Get());
Storage.GetPointer() = InPtr;
}
@ -358,13 +359,12 @@ public:
private:
NAMESPACE_PRIVATE::TUniquePtrStorage<T, E> Storage;
NAMESPACE_PRIVATE::TUniqueStorage<T, E> Storage;
};
/** Single-ownership smart pointer. Use this when you need an object's lifetime to be strictly bound to the lifetime of a single smart pointer. */
template <typename T, typename E = TDefaultDelete<T>> requires (CObject<T> && !CBoundedArray<T> && !CRValueReference<E>
&& ((!CArray<T> && CInvocable<E, T*>) || (CArray<T> && CInvocable<E, TRemoveExtent<T>*>)))
template <typename T, CInvocable<TRemoveExtent<T>*> E = TDefaultDelete<T>> requires (CObject<T> && !CBoundedArray<T> && (CDestructible<E> || CLValueReference<E>))
class TUniquePtr final : private FNoncopyable
{
public:
@ -394,10 +394,10 @@ public:
FORCEINLINE constexpr TUniquePtr(TUniquePtr<U, InE>&& InValue) : Storage(InValue.Release(), Forward<InE>(InValue.GetDeleter())) { }
/** If !IsValid() there are no effects. Otherwise, the owned object is destroyed. */
FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) GetDeleter()(Get()); }
FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) Invoke(GetDeleter(), Get()); }
/** Move assignment operator. Transfers ownership from 'InValue' to *this. */
FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue)
FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) requires (!CReference<E> && CAssignableFrom<E&, E&&>)
{
Reset(InValue.Release());
GetDeleter() = Forward<E>(InValue.GetDeleter());
@ -435,7 +435,7 @@ public:
/** Replaces the managed object. */
FORCEINLINE constexpr void Reset(T* InPtr = nullptr)
{
if (IsValid()) GetDeleter()(Get());
if (IsValid()) Invoke(GetDeleter(), Get());
Storage.GetPointer() = InPtr;
}
@ -488,10 +488,9 @@ public:
private:
NAMESPACE_PRIVATE::TUniquePtrStorage<T, E> Storage;
NAMESPACE_PRIVATE::TUniqueStorage<T, E> Storage;
template <typename OtherT, typename OtherE> requires (CObject<OtherT> && !CBoundedArray<OtherT> && !CRValueReference<OtherE>
&& ((!CArray<OtherT> && CInvocable<OtherE, OtherT*>) || (CArray<OtherT> && CInvocable<OtherE, TRemoveExtent<OtherT>*>)))
template <typename OtherT, CInvocable<TRemoveExtent<OtherT>*> OtherE> requires (CObject<OtherT> && !CBoundedArray<OtherT> && (CDestructible<OtherE> || CLValueReference<OtherE>))
friend class TUniquePtr;
};
@ -527,15 +526,15 @@ public:
FORCEINLINE constexpr TUniquePtr(TUniquePtr&& InValue) : Storage(InValue.Release(), Forward<E>(InValue.GetDeleter())) { }
/** Constructs a TUniquePtr by transferring ownership from 'InValue' to *this and stores the nullptr in 'InValue'. */
template <typename U = T*, typename InE> requires (CConvertibleTo<U(*)[], T(*)[]> && CArray<U>
template <typename U, typename InE> requires (CConvertibleTo<U(*)[], T(*)[]> && CArray<U>
&& ((CReference<E> && CSameAs<InE, E>) || (!CReference<E> && CConvertibleTo<InE, E>)))
FORCEINLINE constexpr TUniquePtr(TUniquePtr<U, InE>&& InValue) : Storage(InValue.Release(), Forward<InE>(InValue.GetDeleter())) { }
/** If !IsValid() there are no effects. Otherwise, the owned array is destroyed. */
FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) GetDeleter()(Get()); }
FORCEINLINE constexpr ~TUniquePtr() { if (IsValid()) Invoke(GetDeleter(), Get()); }
/** Move assignment operator. Transfers ownership from 'InValue' to *this. */
FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue)
FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr&& InValue) requires (!CReference<E> && CAssignableFrom<E&, E&&>)
{
Reset(InValue.Release());
GetDeleter() = Forward<E>(InValue.GetDeleter());
@ -543,7 +542,7 @@ public:
}
/** Move assignment operator. Transfers ownership from 'InValue' to *this. */
template <typename U = T*, typename InE> requires (CConvertibleTo<U(*)[], T(*)[]>
template <typename U, typename InE> requires (CConvertibleTo<U(*)[], T(*)[]>
&& CArray<U> && !CReference<E> && CAssignableFrom<E&, InE&&>)
FORCEINLINE constexpr TUniquePtr& operator=(TUniquePtr<U, E>&& InValue)
{
@ -576,7 +575,7 @@ public:
template <typename U = T*> requires (CNullPointer<U> || (CPointer<U> && CConvertibleTo<TRemovePointer<U>(*)[], T(*)[]>))
FORCEINLINE constexpr void Reset(U InPtr = nullptr)
{
if (IsValid()) GetDeleter()(Get());
if (IsValid()) Invoke(GetDeleter(), Get());
Storage.GetPointer() = InPtr;
}
@ -631,10 +630,9 @@ public:
private:
NAMESPACE_PRIVATE::TUniquePtrStorage<T, E> Storage;
NAMESPACE_PRIVATE::TUniqueStorage<T, E> Storage;
template <typename OtherT, typename OtherE> requires (CObject<OtherT> && !CBoundedArray<OtherT> && !CRValueReference<OtherE>
&& ((!CArray<OtherT> && CInvocable<OtherE, OtherT*>) || (CArray<OtherT> && CInvocable<OtherE, TRemoveExtent<OtherT>*>)))
template <typename OtherT, CInvocable<TRemoveExtent<OtherT>*> OtherE> requires (CObject<OtherT> && !CBoundedArray<OtherT> && (CDestructible<OtherE> || CLValueReference<OtherE>))
friend class TUniquePtr;
};