TTypeContainer

Template for type containers.

Windows
MacOS
Linux

Inheritance Hierarchy

TTypeContainer

FTypeContainer

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Misc/TypeContainer.h

Include

#include "Misc/TypeContainer.h"

Syntax

template<ESPMode Mode>
class TTypeContainer

Remarks

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 UE4 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:

Unique instance per process (aka. singleton), using RegisterClass(ETypeContainerScope::Process) or RegisterInstance()

Unique instance per thread (aka. thread singleton), using RegisterClass(ETypeContainerScope::Thread)

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.

Functions

Name Description

Protected function

void

 

AddProvider

(
    const TCHAR* Name,
    const TSharedPtr< IInstanceProvider...
)

Add an instance provider to the container.

Public function Const

TSharedRef< ...

 

GetInstance()

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

Public function Const

TSharedRef< ...

 

GetInstanceRef()

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

Public function Const

bool

 

IsRegistered()

Check whether the specified class has been registered.

Public function

void

 

RegisterClass

(
    ETypeContainerScope Scope
)

Registers a class for instances of the specified class.

Public function

void

 

RegisterDelegate

(
    D Delegate
)

Register a factory delegate for the specified class.

Public function

void

 

RegisterFactory

(
    const TFunction< TSharedRef< R, Mod...
)

Register a factory function for the specified class.

Public function

void

 

RegisterFactory

(
    const TFunction< TSharedRef< R, Mod...
)

Register a factory function for the specified class.

Public function

void

 

RegisterInstance

(
    const TSharedRef< T, Mode >& Insta...
)

Registers an existing instance for the specified class.

Public function

void

 

Unregister()

Unregisters the instance or class for a previously registered class.

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss