MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn > Class Template Reference

Auto Modular Features are a workflow with Modular Features where the developer doesn't have to rely on string identifiers. TAutoModularFeature template take care of naming the feature and introduces some common functionality, like getter functions and runtime validations. More...

#include <AutoModularFeature.h>

Inheritance diagram for Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >:
Mcro::AutoModularFeature::IAutoModularFeature

Public Types

using Feature = FeatureIn
 
using AutoModularFeature = TAutoModularFeature
 

Public Member Functions

template<typename Implementation >
requires CDerivedFrom<Implementation, Feature>
void Register (this Implementation &&self)
 Call this function in implementation constructors.
 
virtual ~TAutoModularFeature ()
 

Static Public Member Functions

static auto OnRegistered () -> TBelatedEventDelegate< void(Feature *)> &
 This event is triggered when an implementation of this feature is created.
 
static FORCEINLINE FName FeatureName ()
 Get the name of the feature.
 
static FORCEINLINE int32 ImplementationCount ()
 
static FORCEINLINE FeatureGet ()
 Get the first existing implementation of this feature. If there are no implementations a check will fail.
 
static FORCEINLINE FeatureTryGet (const int32 index)
 Get the first existing implementation of this feature. Return nullptr If there are no implementations.
 
static FORCEINLINE TArray< Feature * > GetAll ()
 
static FORCEINLINE TFuture< Feature * > GetBelated ()
 Get the first implementation once it is registered, or return the first implementation immediately if there's already one registered.
 

Detailed Description

template<typename FeatureIn>
class Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >

Auto Modular Features are a workflow with Modular Features where the developer doesn't have to rely on string identifiers. TAutoModularFeature template take care of naming the feature and introduces some common functionality, like getter functions and runtime validations.

First a feature is defined with its interface class like so:

class IMyModularFeature : public TAutoModularFeature<IMyModularFeature>
{
// ...
}
Auto Modular Features are a workflow with Modular Features where the developer doesn't have to rely o...

Then each implementation of this feature are defined like so:

class FMyFeatureImplementation : public IMyModularFeature, public IFeatureImplementation
{
FMyFeatureImplementation()
{
// See the inline docs for why this needs to be done
Register()
}
}
Tagging an implementation of a feature.

Then instantiate the feature implementation when needed:

class FMyModule
{
TPimplPtr<FMyFeatureImplementation> MyImplementation;
}
if (...)
{
MyImplementation = MakePimpl<FMyFeatureImplementation>();
}

To access the feature implementations then just use

if (IMyModularFeature::ImplementationCount() > 0)
{
IMyModularFeature::Get().MyStuff();
}

Internally the feature name will be identical to the class name. In this case IMyModularFeature will register as "IMyModularFeature". Technically one can get it via

IModularFeatures::Get().GetModularFeature<IMyModularFeature>(TEXT_"IMyModularFeature")
#define TEXT_
A convenience alternative to Unreal's own TEXT macro but this one doesn't require parenthesis around ...
Definition TextMacros.h:53

but it is strongly discouraged for type safety and keeping code simple.

For globally available features you may use just simply a global variable, or if it's important to have the owning module fully initialized, TModuleBoundObject is recommended.

TModuleBoundObject<FFoobarModule, FMyFeatureImplementation> GMyFeatureImplementation;
Remarks
IMyModularFeature::FeatureName() and TTypeFName<FMyFeatureImplementation>() can be used for runtime comparison / validation.
Template Parameters
FeatureInCuriously Recurring Template argument of the feature

Definition at line 96 of file AutoModularFeature.h.

Member Typedef Documentation

◆ AutoModularFeature

template<typename FeatureIn >
using Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::AutoModularFeature = TAutoModularFeature

Definition at line 100 of file AutoModularFeature.h.

◆ Feature

template<typename FeatureIn >
using Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::Feature = FeatureIn

Definition at line 99 of file AutoModularFeature.h.

Constructor & Destructor Documentation

◆ ~TAutoModularFeature()

template<typename FeatureIn >
virtual Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::~TAutoModularFeature ( )
inlinevirtual

Definition at line 166 of file AutoModularFeature.h.

Member Function Documentation

◆ FeatureName()

template<typename FeatureIn >
static FORCEINLINE FName Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::FeatureName ( )
inlinestatic

Get the name of the feature.

Definition at line 110 of file AutoModularFeature.h.

◆ Get()

template<typename FeatureIn >
static FORCEINLINE Feature & Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::Get ( )
inlinestatic

Get the first existing implementation of this feature. If there are no implementations a check will fail.

Definition at line 125 of file AutoModularFeature.h.

◆ GetAll()

template<typename FeatureIn >
static FORCEINLINE TArray< Feature * > Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::GetAll ( )
inlinestatic
Returns
An array of all implementations of this feature

Definition at line 137 of file AutoModularFeature.h.

◆ GetBelated()

template<typename FeatureIn >
static FORCEINLINE TFuture< Feature * > Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::GetBelated ( )
inlinestatic

Get the first implementation once it is registered, or return the first implementation immediately if there's already one registered.

Returns
A future completed when the first implementation becomes available, or there's already one
Warning
If no implementations were created throughout the entire execution of the program, it will crash on exit with an unfulfilled promise error. When using this function either handle this scenario early, or use OnRegistered() if your particular feature may never be created.

Definition at line 183 of file AutoModularFeature.h.

◆ ImplementationCount()

template<typename FeatureIn >
static FORCEINLINE int32 Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::ImplementationCount ( )
inlinestatic
Returns
The number of implementations created for this feature

Definition at line 116 of file AutoModularFeature.h.

◆ OnRegistered()

template<typename FeatureIn >
static auto Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::OnRegistered ( ) -> TBelatedEventDelegate<void(Feature*)>&
inlinestatic

This event is triggered when an implementation of this feature is created.

Definition at line 103 of file AutoModularFeature.h.

◆ Register()

template<typename FeatureIn >
template<typename Implementation >
requires CDerivedFrom<Implementation, Feature>
void Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::Register ( this Implementation && self)
inline

Call this function in implementation constructors.

This is a necessary boilerplate to maintain polymorphism of implementations. Otherwise, if the native registration function would be called directly in TAutoModularFeature default constructor, virtual function overrides are not yet known, and "deducing this" is not meant for constructors.

Template Parameters
ImplementationDerived type of the implementation
Parameters
selfPointer to implementation registering itself

Definition at line 154 of file AutoModularFeature.h.

◆ TryGet()

template<typename FeatureIn >
static FORCEINLINE Feature * Mcro::AutoModularFeature::TAutoModularFeature< FeatureIn >::TryGet ( const int32 index)
inlinestatic

Get the first existing implementation of this feature. Return nullptr If there are no implementations.

Definition at line 131 of file AutoModularFeature.h.


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