Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
TTypeContainer< Mode > Class Template Reference

#include <TypeContainer.h>

+ Collaboration diagram for TTypeContainer< Mode >:

Classes

struct  FThreadInstanceProvider
 
class  IInstanceProvider
 
struct  TFunctionInstanceProvider
 
struct  TSharedInstanceProvider
 
struct  TThreadInstanceProvider
 

Public Member Functions

template<class R >
TSharedRef< R, Mode > GetInstance () const
 
template<class R >
TSharedRef< R, Mode > GetInstanceRef () const
 
template<class R >
bool IsRegistered () const
 
template<class R , class T , typename... P>
void RegisterClass (ETypeContainerScope Scope=ETypeContainerScope::Process)
 
template<class R , class D , typename... P>
void RegisterDelegate (D Delegate)
 
template<class R >
void RegisterFactory (const TFunction< TSharedRef< R, Mode >()> &CreateFunc)
 
template<class R , typename P0 , typename... P>
void RegisterFactory (const TFunction< TSharedRef< R, Mode >(TSharedRef< P0, Mode >, TSharedRef< P, Mode >...)> &CreateFunc)
 
template<class R , class T >
void RegisterInstance (const TSharedRef< T, Mode > &Instance)
 
template<class R >
void Unregister ()
 

Protected Member Functions

void AddProvider (const TCHAR *Name, const TSharedPtr< IInstanceProvider > &Provider)
 

Private Attributes

FCriticalSection CriticalSection
 
TMap< FString, TSharedPtr< IInstanceProvider > > Providers
 

Detailed Description

template<ESPMode Mode = ESPMode::ThreadSafe>
class TTypeContainer< Mode >

Template for type containers.

Type containers allow for configuring objects and their dependencies without actually creating these objects. They fulfill a role similar to class factories, but without being locked to a particular type of class. When passed into class constructors or methods, type containers can facilitate the Inversion of Control (IoC) pattern.

Since UE neither uses run-time type information nor pre-processes plain old C++ classes, type names need to be exposed using the Expose_TNameOf macro in order to make them registrable with type containers, i.e. Expose_TNameOf(FMyClass).

Once a type is registered with a container, instances of that type can be retrieved from it. There are currently three life time scopes available for instance creation:

  1. Unique instance per process (aka. singleton), using RegisterClass(ETypeContainerScope::Process) or RegisterInstance()
  2. Unique instance per thread (aka. thread singleton), using RegisterClass(ETypeContainerScope::Thread)
  3. Unique instance per call (aka. class factory), using RegisterClass(ETypeContainerScope::Instance) or RegisterFactory()

See the file TypeContainerTest.cpp for detailed examples on how to use each of these type registration methods.

In the interest of fast performance, the object pointers returned by type containers are not thread-safe by default. If you intend to use the same type container and share its objects from different threads, use TTypeContainer<ESPMode::ThreadSafe> instead, which will then manage and return thread-safe versions of all object pointers.

Note: Type containers depend on variadic templates and are therefore not available on XboxOne at this time, which means they should only be used for desktop applications.

Definition at line 66 of file TypeContainer.h.

Member Function Documentation

◆ AddProvider()

template<ESPMode Mode = ESPMode::ThreadSafe>
void TTypeContainer< Mode >::AddProvider ( const TCHAR * Name,
const TSharedPtr< IInstanceProvider > & Provider )
inlineprotected

Add an instance provider to the container.

Parameters
NameThe name of the type to add the provider for.
ProviderThe instance provider.

Definition at line 407 of file TypeContainer.h.

◆ GetInstance()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R >
TSharedRef< R, Mode > TTypeContainer< Mode >::GetInstance ( ) const
inline

Gets a shared pointer to an instance of the specified class.

Parameters
RThe type of class to get an instance for.
Ashared reference to the instance.
See also
RegisterClass, RegisterDelegate, RegisterFactory, RegisterInstance

Definition at line 181 of file TypeContainer.h.

◆ GetInstanceRef()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R >
TSharedRef< R, Mode > TTypeContainer< Mode >::GetInstanceRef ( ) const
inline

Gets a shared reference to an instance of the specified class.

Unlike GetInstance(), this function will assert if no instance was registered for the requested type of class using either RegisterClass() or RegisterInstance().

Parameters
RThe type of class that an instance is being requested for.
Ashared pointer to the instance.
See also
GetInstance, RegisterClass, RegisterInstance

Definition at line 203 of file TypeContainer.h.

◆ IsRegistered()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R >
bool TTypeContainer< Mode >::IsRegistered ( ) const
inline

Check whether the specified class has been registered.

Parameters
RThe type of registered class to check.
Returns
true if the class was registered, false otherwise.

Definition at line 215 of file TypeContainer.h.

◆ RegisterClass()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R , class T , typename... P>
void TTypeContainer< Mode >::RegisterClass ( ETypeContainerScope Scope = ETypeContainerScope::Process)
inline

Registers a class for instances of the specified class.

Parameters
RThe type of class to register an instance class for.
TTye type of class to register (must be the same as or derived from R).
PThe constructor parameters that the class requires.
ScopeThe scope at which instances must be unique (default = Process).
See also
RegisterDelegate, RegisterInstance, Unregister

Definition at line 233 of file TypeContainer.h.

◆ RegisterDelegate()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R , class D , typename... P>
void TTypeContainer< Mode >::RegisterDelegate ( D Delegate)
inline

Register a factory delegate for the specified class.

Parameters
RThe type of class to register an instance class for.
DTye type of factory delegate to register.
PThe parameters that the delegate requires.
DelegateThe delegate to register.

Definition at line 282 of file TypeContainer.h.

◆ RegisterFactory() [1/2]

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R >
void TTypeContainer< Mode >::RegisterFactory ( const TFunction< TSharedRef< R, Mode >()> & CreateFunc)
inline

Register a factory function for the specified class.

Parameters
RThe type of class to register the instance for.
PAdditional parameters that the factory function requires.
CreateFuncThe factory function that will create instances.
See also
RegisterClass, RegisterInstance, Unregister

Definition at line 306 of file TypeContainer.h.

◆ RegisterFactory() [2/2]

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R , typename P0 , typename... P>
void TTypeContainer< Mode >::RegisterFactory ( const TFunction< TSharedRef< R, Mode >(TSharedRef< P0, Mode >, TSharedRef< P, Mode >...)> & CreateFunc)
inline

Register a factory function for the specified class.

Parameters
RThe type of class to register the instance for.
P0The first parameter that the factory function requires.
PAdditional parameters that the factory function requires.
CreateFuncThe factory function that will create instances.
See also
RegisterClass, RegisterInstance, Unregister

Definition at line 329 of file TypeContainer.h.

◆ RegisterInstance()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R , class T >
void TTypeContainer< Mode >::RegisterInstance ( const TSharedRef< T, Mode > & Instance)
inline

Registers an existing instance for the specified class.

Parameters
RThe type of class to register the instance for.
TThe type of the instance to register (must be the same as or derived from R).
InstanceThe instance of the class to register.
See also
RegisterClass, RegisterDelegate, Unregister

Definition at line 377 of file TypeContainer.h.

◆ Unregister()

template<ESPMode Mode = ESPMode::ThreadSafe>
template<class R >
void TTypeContainer< Mode >::Unregister ( )
inline

Unregisters the instance or class for a previously registered class.

Parameters
RThe type of class to unregister.
See also
RegisterClass, RegisterDelegate, RegisterInstance

Definition at line 391 of file TypeContainer.h.

Member Data Documentation

◆ CriticalSection

template<ESPMode Mode = ESPMode::ThreadSafe>
FCriticalSection TTypeContainer< Mode >::CriticalSection
mutableprivate

Critical section for synchronizing access.

Definition at line 418 of file TypeContainer.h.

◆ Providers

template<ESPMode Mode = ESPMode::ThreadSafe>
TMap<FString, TSharedPtr<IInstanceProvider> > TTypeContainer< Mode >::Providers
private

Maps class names to instance providers.

Definition at line 421 of file TypeContainer.h.


The documentation for this class was generated from the following file: