Factory< T(ARGS...)> Class Template Reference

#include <factory.h>

Inheritance diagram for Factory< T(ARGS...)>:

Detailed Description

template<typename T, typename... ARGS>
class maxon::Factory< T(ARGS...)>

Reference class for factories which return an object of type T based on the arguments ARGS. Typically you declare a factory in a header file as in

namespace DataFormatBaseReader
{
MAXON_DECLARATION(Factory<DataFormatBaseReaderRef(const Url&)>, FromUrl, "net.maxon.dataformatbasereader.fromurl");
}
MAXON_DECLARATION(Class< AssetType >, AssetTypeBaseComponent, "net.maxon.component.assettypebase")
This base component can be used by implementations of AssetType.

The factory can then be used to create new DataFormatBaseReaderRef objects given an Url:

DataFormatBaseReaderRef stream = DataFormatBaseReader::FromUrl().Create(url) iferr_return;
PyObject * stream
Definition: codecs.h:186
#define iferr_return
Definition: resultbase.h:1465

The definition of a factory can be done with the help of one of the static Create*Factory functions of Factory.

Template Parameters
TThe type of objects created by the factory.
ARGSThe arguments which the factory expects for object creation.

Classes

class  NonConst
 

Public Member Functions

 MAXON_DEFAULT_REFERENCE_CONSTRUCTORS (Factory, FactoryBase< Factory< T(ARGS...)>>)
 

Static Public Member Functions

static Result< NonConst > CreateFactory (DelegateType &&delegate, const DataDictionary &params=maxon::NullValue< const DataDictionary & >())
 
static Result< NonConst > CreateCachedFactory (DelegateType &&delegate, Int cacheSize, const DataDictionary &params=maxon::NullValue< const DataDictionary & >())
 
template<typename COMP , typename = typename COMP::_Wrapper>
static Result< NonConst > CreateObjectFactory (Result< void >(COMP::*initFunc)(FactoryInterface::ConstPtr, ARGS...), const DataDictionary &params=maxon::NullValue< const DataDictionary & >())
 
template<typename COMP , typename = typename COMP::_Wrapper>
static Result< NonConst > CreateCachedObjectFactory (Result< void >(COMP::*initFunc)(FactoryInterface::ConstPtr, ARGS...), Int cacheSize, const DataDictionary &params=maxon::NullValue< const DataDictionary & >())
 

Private Types

using ReferenceClass = Factory
 
using NewInstanceType = T
 
using DelegateType = typename Super::DelegateType
 

Static Private Member Functions

static MAXON_ATTRIBUTE_FORCE_INLINE const Factory & NullValue ()
 

Member Typedef Documentation

◆ ReferenceClass

using ReferenceClass = Factory
private

◆ NewInstanceType

using NewInstanceType = T
private

◆ DelegateType

using DelegateType = typename Super::DelegateType
private

Member Function Documentation

◆ MAXON_DEFAULT_REFERENCE_CONSTRUCTORS()

MAXON_DEFAULT_REFERENCE_CONSTRUCTORS ( Factory< T(ARGS...)>  ,
FactoryBase< Factory< T(ARGS...)>>   
)

◆ NullValue()

static MAXON_ATTRIBUTE_FORCE_INLINE const Factory& NullValue ( )
staticprivate

◆ CreateFactory()

static Result<NonConst> CreateFactory ( DelegateType &&  delegate,
const DataDictionary &  params = maxon::NullValue<const DataDictionary&>() 
)
static

Creates a factory which creates objects through a Delegate<Result<T>(FactoryInterface::ConstPtr, ARGS...)>. Note the first extra parameter for the delegate, it receives a pointer to the called factory.

The returned factory doesn't use caching, so each call will return a new object. If you want to cache created objects, use CreateCachedObjectFactory.

Parameters
[in]delegateDelegate to which the created factory delegates the object creation.
[in]paramsParameters to store at the created factory itself. They can be accessed through the base interface DataDictionaryObject of the factory.
Returns
Factory which uses #delegate when called.

◆ CreateCachedFactory()

static Result<NonConst> CreateCachedFactory ( DelegateType &&  delegate,
Int  cacheSize,
const DataDictionary &  params = maxon::NullValue<const DataDictionary&>() 
)
static

Creates a factory which creates cached objects through a Delegate<Result<T>(FactoryInterface::ConstPtr, ARGS...)>. This function is used in the same way as CreateFactory, but the returned factory caches the created objects based on the arguments. So whenever the factory is called multiple times with equal arguments, it will return the same object.

Parameters
[in]delegateDelegate to which the created factory delegates the object creation.
[in]cacheSizeThe cache size of the factory. This should be large enough because the performance of the cache degrades when there are more than cacheSize entries.
[in]paramsParameters to store at the created factory itself. They can be accessed through the base interface DataDictionaryObject of the factory.
Returns
Factory which uses #initFunc when called.

◆ CreateObjectFactory()

static Result<NonConst> CreateObjectFactory ( Result< void >(COMP::*)(FactoryInterface::ConstPtr, ARGS...)  initFunc,
const DataDictionary &  params = maxon::NullValue<const DataDictionary&>() 
)
static

Creates a factory which creates virtual objects from a component implementation COMP. If the factory's return type T is an interface reference class as in

MAXON_DECLARATION(Factory<DataFormatBaseReaderRef(const Url&)>, FromUrl, "net.maxon.dataformatbasereader.fromurl");

you can implement such a factory by a component which implements the interface and has an additional member function #initFunc which expects the factory as first parameter and then ARGS:

class DataFormatReaderUniCharImpl : public Component<DataFormatReaderUniCharImpl, DataFormatBaseReaderInterface>
{
MAXON_COMPONENT(NORMAL, DataDictionaryObjectClass);
public:
Result<void> InitFromUrl(FactoryInterface::ConstPtr factory, const Url& url);
...
};
ComponentWithBase< C, ComponentRoot, INTERFACES... > Component
Definition: objectbase.h:2794
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2212

Then CreateObjectFactory takes #initFunc and creates a corresponding factory:

MAXON_DECLARATION_REGISTER(DataFormatBaseReader::FromUrl)
{
return decltype(DataFormatBaseReader::FromUrl)::Type::CreateObjectFactory(&DataFormatReaderUniCharImpl::InitFromUrl);
}
static Result< NonConst > CreateObjectFactory(Result< void >(COMP::*initFunc)(FactoryInterface::ConstPtr, ARGS...), const DataDictionary &params=maxon::NullValue< const DataDictionary & >())
Definition: factory.h:230
#define MAXON_DECLARATION_REGISTER(...)
Definition: module.h:923

The returned factory doesn't use caching, so each call will return a new object. If you want to cache created objects, use CreateCachedObjectFactory.

Template Parameters
COMPComponent class.
Parameters
[in]initFuncMember function of COMP which initializes the object with the arguments given to the factory.
[in]paramsParameters to store at the created factory itself. They can be accessed through the base interface DataDictionaryObject of the factory.
Returns
Factory which uses #initFunc when called.

◆ CreateCachedObjectFactory()

static Result<NonConst> CreateCachedObjectFactory ( Result< void >(COMP::*)(FactoryInterface::ConstPtr, ARGS...)  initFunc,
Int  cacheSize,
const DataDictionary &  params = maxon::NullValue<const DataDictionary&>() 
)
static

Creates a factory which creates cached virtual objects from a component implementation COMP. This function is used in the same way as CreateObjectFactory, but the returned factory caches the created objects based on the arguments. So whenever the factory is called multiple times with equal arguments, it will return the same object.

Template Parameters
COMPComponent class.
Parameters
[in]initFuncMember function of COMP which initializes the object with the arguments given to the factory.
[in]cacheSizeThe cache size of the factory. This should be large enough because the performance of the cache degrades when there are more than cacheSize entries.
[in]paramsParameters to store at the created factory itself. They can be accessed through the base interface DataDictionaryObject of the factory.
Returns
Factory which uses #initFunc when called.