UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TTypeContainer< Mode > Class Template Reference

#include <TypeContainer.h>

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)
 

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.

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.

◆ 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

◆ 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

◆ 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.

◆ 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

◆ 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.

◆ 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

◆ 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

◆ 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

◆ 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

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