LiteFX 0.3.1.2022
Computer Graphics Engine
LiteFX::Builder< TDerived, T, TParent, TPointer > Class Template Reference

Describes an generic builder type. More...

#include <containers.hpp>

Public Types

using derived_type = TDerived
 
using instance_type = T
 
using parent_type = TParent
 
using pointer_type = TPointer
 
using builder_type = Builder< derived_type, instance_type, parent_type, pointer_type >
 

Public Member Functions

const T * instance () const noexcept
 Returns a pointer to the current instance of the object that is built by the builder. More...
 
const TParent & parent () const noexcept
 Returns a reference of the parent builder. More...
 
 Builder (TParent &parent, TPointer &&instance) noexcept
 Initializes the builder instance. More...
 
 Builder (builder_type &&_other) noexcept
 Initializes the builder instance by taking over another instance. More...
 
 Builder (const builder_type &)=delete
 
virtual ~Builder () noexcept=default
 
template<typename TInstance >
void use (pointer_type &&)=delete
 Called by child builders to pass a constructed object back to the parent builder. More...
 
TParent & add ()
 First, calls build, then use on the parent builder using the current object instance and finally returns the parent builder. More...
 

Protected Member Functions

T * instance () noexcept
 Returns a pointer to the current instance of the object that is built by the builder. More...
 
virtual void build ()
 Can be overwritten to perform any pre-construction work before the builder returns the final object instance. More...
 

Detailed Description

template<typename TDerived, typename T, typename TParent, typename TPointer>
class LiteFX::Builder< TDerived, T, TParent, TPointer >

Describes an generic builder type.

Describes a child builder.

Builders are helper classes to create objects or object hierarchies. A builder is called root builder, if TParent is set to std::nullptr_t, otherwise it is called child builder and TParent is set to the type of the parent builder in the hierarchy. A builder can be a child builder to either a root builder or another child builder. However, at the top of the hierarchy there needs to be one root builder and each hierarchy can only have one root builder.

Building a child object using a child builder can be terminated by calling add on it. This will call use on the parent builder and pass the object instance to it. If any work needs to be done by the builder before passing it to the parent builder, it is possible to overwrite the build method. For root builders no additional call is required. Instead a root builder provides a move-assignment operator for T . Assigning the builder instance to an instance of TPointer will return the instance object. Similar to child builders, it is possible to overwrite the build method, to perform any additional pre-construction work.

The derived type TDerived marks the actual implementation of the builder, which itself is derived from this class in CRTP-fashion. It is typically used to define builder methods that return references to this.

Builders create the object instances they manage in form of smart pointers. The TPointer can either be set to any smart pointer type that wraps T for convenience.

Template Parameters
TDerivedThe concrete implementation of the builder itself.
TThe type of the object the builder builds.
TParentThe type of the parent builder or std::nullptr_t.
TPointerThe type of the pointer, used to access the instance of T this builder builds.
Template Parameters
TDerivedThe concrete implementation of the builder itself.
TThe type of the object the builder builds.
TPointerThe type of the pointer, used to access the instance of T this builder builds.

Member Typedef Documentation

◆ builder_type

template<typename TDerived , typename T , typename TParent , typename TPointer >
using LiteFX::Builder< TDerived, T, TParent, TPointer >::builder_type = Builder<derived_type, instance_type, parent_type, pointer_type>

◆ derived_type

template<typename TDerived , typename T , typename TParent , typename TPointer >
using LiteFX::Builder< TDerived, T, TParent, TPointer >::derived_type = TDerived

◆ instance_type

template<typename TDerived , typename T , typename TParent , typename TPointer >
using LiteFX::Builder< TDerived, T, TParent, TPointer >::instance_type = T

◆ parent_type

template<typename TDerived , typename T , typename TParent , typename TPointer >
using LiteFX::Builder< TDerived, T, TParent, TPointer >::parent_type = TParent

◆ pointer_type

template<typename TDerived , typename T , typename TParent , typename TPointer >
using LiteFX::Builder< TDerived, T, TParent, TPointer >::pointer_type = TPointer

Constructor & Destructor Documentation

◆ Builder() [1/3]

template<typename TDerived , typename T , typename TParent , typename TPointer >
LiteFX::Builder< TDerived, T, TParent, TPointer >::Builder ( TParent &  parent,
TPointer &&  instance 
)
inlineexplicitnoexcept

Initializes the builder instance.

Parameters
parentThe instance of the parent builder.
instanceThe instance of the object to build.

◆ Builder() [2/3]

template<typename TDerived , typename T , typename TParent , typename TPointer >
LiteFX::Builder< TDerived, T, TParent, TPointer >::Builder ( builder_type &&  _other)
inlinenoexcept

Initializes the builder instance by taking over another instance.

Parameters
_otherThe instance of another builder object to take over.

◆ Builder() [3/3]

template<typename TDerived , typename T , typename TParent , typename TPointer >
LiteFX::Builder< TDerived, T, TParent, TPointer >::Builder ( const builder_type )
delete

◆ ~Builder()

template<typename TDerived , typename T , typename TParent , typename TPointer >
virtual LiteFX::Builder< TDerived, T, TParent, TPointer >::~Builder ( )
virtualdefaultnoexcept

Member Function Documentation

◆ add()

template<typename TDerived , typename T , typename TParent , typename TPointer >
TParent & LiteFX::Builder< TDerived, T, TParent, TPointer >::add ( )
inline

First, calls build, then use on the parent builder using the current object instance and finally returns the parent builder.

◆ build()

template<typename TDerived , typename T , typename TParent , typename TPointer >
virtual void LiteFX::Builder< TDerived, T, TParent, TPointer >::build ( )
inlineprotectedvirtual

Can be overwritten to perform any pre-construction work before the builder returns the final object instance.

Reimplemented in LiteFX::AppBuilder.

◆ instance() [1/2]

template<typename TDerived , typename T , typename TParent , typename TPointer >
const T * LiteFX::Builder< TDerived, T, TParent, TPointer >::instance ( ) const
inlinenoexcept

Returns a pointer to the current instance of the object that is built by the builder.

Returns
A pointer to the current object instance.

◆ instance() [2/2]

template<typename TDerived , typename T , typename TParent , typename TPointer >
T * LiteFX::Builder< TDerived, T, TParent, TPointer >::instance ( )
inlineprotectednoexcept

Returns a pointer to the current instance of the object that is built by the builder.

Returns
A pointer to the current object instance.

◆ parent()

template<typename TDerived , typename T , typename TParent , typename TPointer >
const TParent & LiteFX::Builder< TDerived, T, TParent, TPointer >::parent ( ) const
inlinenoexcept

Returns a reference of the parent builder.

Returns
A reference of the parent builder.

◆ use()

template<typename TDerived , typename T , typename TParent , typename TPointer >
template<typename TInstance >
void LiteFX::Builder< TDerived, T, TParent, TPointer >::use ( pointer_type &&  )
delete

Called by child builders to pass a constructed object back to the parent builder.

This method must be implemented for each child builder, a builder can create. This introduces a hard dependency between child and parent builders. It is not possible to define a child builder without implementing the counter part in the parent builder.