MCRO
C++23 utilities for Unreal Engine.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
Mcro::SharedObjects Namespace Reference

Utilities for TSharedPtr/Ref and related. More...

Data Structures

struct  TSharedStorage
 A simple mutable shared storage of any value. More...
 

Concepts

concept  CSharedInitializeable
 Concept describing an object which provides a deferred initializer for shared objects.
 

Typedefs

template<CSharedOrWeak T>
using TSharedPtrFrom = TSharedPtr<typename T::ElementType, T::Mode>
 Copy thread-safety from other shared object type.
 
template<CSharedOrWeak T>
using TSharedRefFrom = TSharedRef<typename T::ElementType, T::Mode>
 Copy thread-safety from other shared object type.
 
template<CSharedOrWeak T>
using TWeakPtrFrom = TWeakPtr<typename T::ElementType, T::Mode>
 Copy thread-safety from other shared object type.
 
template<CDefaultInitializable T>
using TSharedStoragePtr = TSharedPtr<TSharedStorage<T>>
 
template<CDefaultInitializable T>
using TSharedStorageRef = TSharedRef<TSharedStorage<T>>
 
template<CDefaultInitializable T>
using TSharedStorageWeakPtr = TWeakPtr<TSharedStorage<T>>
 

Functions

template<typename T , ESPMode Mode = ESPMode::ThreadSafe, typename... Args>
requires CSharedInitializeable<T, Args...>
TSharedRef< T, Mode > MakeShareableInit (T *newObject, Args &&... args)
 A wrapper around MakeShareable that automatically calls an initializer method Initialize on the instantiated object.
 
template<CFunctorObject Initializer, typename... Args, ESPMode Mode = ESPMode::ThreadSafe, typename ResultArg = TFunction_Arg<Initializer, 0>, typename Result = std::decay_t<ResultArg>>
requires std::is_lvalue_reference_v<ResultArg>
TSharedRef< Result, Mode > ConstructShared (Initializer &&init, Args &&... args)
 A combination of MakeShareable and Mcro::Construct::ConstructNew.
 
template<CRefCounted T, ESPMode Mode = ESPMode::ThreadSafe>
TSharedRef< T, Mode > ShareRefCounted (T *object)
 Create a shared pointer which takes in an object with in-place refcounting.
 
template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T const>().AsShared())::Mode>
auto WeakSelf (const T *self) -> TWeakPtr< T const, Mode >
 Same as SharedThis(this) in TSharedFromThis but returning a weak pointer instead.
 
template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T>().AsShared())::Mode>
auto WeakSelf (T *self) -> TWeakPtr< T, Mode >
 Same as SharedThis(this) in TSharedFromThis but returning a weak pointer instead.
 
FORCEINLINE TSharedStorageRef< FDelegateHandle > MakeSharedDelegateHandle ()
 A simple convenience wrapper around making FDelegateHandles shared.
 

Detailed Description

Utilities for TSharedPtr/Ref and related.

Typedef Documentation

◆ TSharedPtrFrom

template<CSharedOrWeak T>
using Mcro::SharedObjects::TSharedPtrFrom = TSharedPtr<typename T::ElementType, T::Mode>

Copy thread-safety from other shared object type.

Definition at line 26 of file SharedObjects.h.

◆ TSharedRefFrom

template<CSharedOrWeak T>
using Mcro::SharedObjects::TSharedRefFrom = TSharedRef<typename T::ElementType, T::Mode>

Copy thread-safety from other shared object type.

Definition at line 30 of file SharedObjects.h.

◆ TSharedStoragePtr

template<CDefaultInitializable T>
using Mcro::SharedObjects::TSharedStoragePtr = TSharedPtr<TSharedStorage<T>>

Definition at line 175 of file SharedObjects.h.

◆ TSharedStorageRef

template<CDefaultInitializable T>
using Mcro::SharedObjects::TSharedStorageRef = TSharedRef<TSharedStorage<T>>

Definition at line 178 of file SharedObjects.h.

◆ TSharedStorageWeakPtr

template<CDefaultInitializable T>
using Mcro::SharedObjects::TSharedStorageWeakPtr = TWeakPtr<TSharedStorage<T>>

Definition at line 181 of file SharedObjects.h.

◆ TWeakPtrFrom

template<CSharedOrWeak T>
using Mcro::SharedObjects::TWeakPtrFrom = TWeakPtr<typename T::ElementType, T::Mode>

Copy thread-safety from other shared object type.

Definition at line 34 of file SharedObjects.h.

Function Documentation

◆ ConstructShared()

template<CFunctorObject Initializer, typename... Args, ESPMode Mode = ESPMode::ThreadSafe, typename ResultArg = TFunction_Arg<Initializer, 0>, typename Result = std::decay_t<ResultArg>>
requires std::is_lvalue_reference_v<ResultArg>
TSharedRef< Result, Mode > Mcro::SharedObjects::ConstructShared ( Initializer && init,
Args &&... args )

A combination of MakeShareable and Mcro::Construct::ConstructNew.

Usage:

using namespace Mcro::SharedObjects;
auto myObject = ConstructShared([](MyObject& _)
{
_.Foo = 42;
_.Initialize();
// etc...
});
static_assert(std::is_same_v<decltype(myObject), TSharedRef<MyObject>>);
Utilities for TSharedPtr/Ref and related.
TSharedRef< Result, Mode > ConstructShared(Initializer &&init, Args &&... args)
A combination of MakeShareable and Mcro::Construct::ConstructNew.
Parameters
initA lambda function with a single l-value reference parameter of the object type to initialize.
argsArguments of the object constructor
Returns
A pointer to the object instance on heap.
Remarks
The C++ 20 designated initializers with named arguments has annoying limitations, therefore this exists

Definition at line 100 of file SharedObjects.h.

◆ MakeShareableInit()

template<typename T , ESPMode Mode = ESPMode::ThreadSafe, typename... Args>
requires CSharedInitializeable<T, Args...>
TSharedRef< T, Mode > Mcro::SharedObjects::MakeShareableInit ( T * newObject,
Args &&... args )

A wrapper around MakeShareable that automatically calls an initializer method Initialize on the instantiated object.

This works around the annoyance of TSharedFromThis objects cannot use their shared pointers in their constructor, braking RAII in some cases. Of course this is only achievable if the object cooperates and implements an Initialize method. It's important that Initialize should not be virtual so derived classes can hide them with their own overload.

Definition at line 64 of file SharedObjects.h.

◆ MakeSharedDelegateHandle()

FORCEINLINE TSharedStorageRef< FDelegateHandle > Mcro::SharedObjects::MakeSharedDelegateHandle ( )

A simple convenience wrapper around making FDelegateHandles shared.

Definition at line 184 of file SharedObjects.h.

◆ ShareRefCounted()

template<CRefCounted T, ESPMode Mode = ESPMode::ThreadSafe>
TSharedRef< T, Mode > Mcro::SharedObjects::ShareRefCounted ( T * object)

Create a shared pointer which takes in an object with in-place refcounting.

Refcounted TSharedPtr/Ref doesn't take ownership of the object and when the last reference goes out of scope it simply decreases the refcount instead of deleting the object.

Definition at line 115 of file SharedObjects.h.

◆ WeakSelf() [1/2]

template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T const>().AsShared())::Mode>
auto Mcro::SharedObjects::WeakSelf ( const T * self) -> TWeakPtr<T const, Mode>

Same as SharedThis(this) in TSharedFromThis but returning a weak pointer instead.

Indeed, TSharedFromThis already has AsWeak() but that can only return the type which was originally set by TSharedFromThis making its usage slightly less convenient in derived classes.

Definition at line 133 of file SharedObjects.h.

◆ WeakSelf() [2/2]

template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T>().AsShared())::Mode>
auto Mcro::SharedObjects::WeakSelf ( T * self) -> TWeakPtr<T, Mode>

Same as SharedThis(this) in TSharedFromThis but returning a weak pointer instead.

Indeed, TSharedFromThis already has AsWeak() but that can only return the type which was originally set by TSharedFromThis making its usage slightly less convenient in derived classes.

Definition at line 149 of file SharedObjects.h.