MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Mcro::SharedObjects Namespace Reference

Concepts

concept  CSharedInitializeable
 

Typedefs

template<CSharedOrWeak T>
using TSharedPtrFrom = TSharedPtr<typename T::ElementType, T::Mode>
 
template<CSharedOrWeak T>
using TSharedRefFrom = TSharedRef<typename T::ElementType, T::Mode>
 
template<CSharedOrWeak T>
using TWeakPtrFrom = TWeakPtr<typename T::ElementType, T::Mode>
 

Functions

template<typename T , ESPMode Mode = ESPMode::ThreadSafe, typename... Args>
requires CSharedInitializeable<T, Args...>
TSharedRef< T, Mode > MakeShareableInit (T *newObject, Args &&... args)
 
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)
 
template<CRefCounted T, ESPMode Mode = ESPMode::ThreadSafe>
TSharedRef< T, Mode > ShareRefCounted (T *object)
 
template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T const>().AsShared())::Mode>
auto WeakSelf (const T *self) -> TWeakPtr< T const, Mode >
 
template<CSharedFromThis T, ESPMode Mode = decltype(DeclVal<T>().AsShared())::Mode>
auto WeakSelf (T *self) -> TWeakPtr< T, Mode >
 

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.

◆ 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>>);
TSharedRef< Result, Mode > ConstructShared(Initializer &&init, Args &&... args)
Parameters
initA lambda function with a single l-value reference parameter of the object type to initialize.
args
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 99 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 63 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 114 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 130 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 144 of file SharedObjects.h.