SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
spark::patterns::Factory< Key, BaseType, Args > Class Template Reference

A factory class that creates objects of type BaseType. More...

#include <Factory.h>

Public Types

using BasePtr = std::unique_ptr<BaseType>
 
using CreatorPtr = std::unique_ptr<details::BaseCreator<BaseType, Args...>>
 

Public Member Functions

 Factory (const Factory &other)=delete
 
 Factory (Factory &&other) noexcept=default
 
Factoryoperator= (const Factory &other)=delete
 
Factoryoperator= (Factory &&other) noexcept=default
 
template<typename TypeToRegister >
void registerType (const Key &key)
 Registers a type the factory can create.
 
BasePtr create (const Key &key, Args &&... args) const
 Creates an object of type BaseType. Throws an exception if the type is not registered.
 
BasePtr createOrFail (const Key &key, Args &&... args) const noexcept
 Creates an object of type BaseType.
 
std::vector< Key > registeredTypes () const noexcept
 Gets a vector of all registered types in the factory.
 

Detailed Description

template<typename Key, typename BaseType, typename... Args>
class spark::patterns::Factory< Key, BaseType, Args >

A factory class that creates objects of type BaseType.

Template Parameters
KeyThe type of the key used to reference objects.
BaseTypeThe type of the base class.
ArgsThe types of the arguments passed to the constructor of the derived class.

Member Function Documentation

◆ create()

template<typename Key , typename BaseType , typename... Args>
Factory< Key, BaseType, Args... >::BasePtr spark::patterns::Factory< Key, BaseType, Args >::create ( const Key & key,
Args &&... args ) const
nodiscard

Creates an object of type BaseType. Throws an exception if the type is not registered.

Parameters
keyThe key of type Key used to create the object.
argsThe arguments passed to the constructor of the derived class.
Returns
A std::unique_ptr to the created object.
Exceptions
spark::base::BadArgumentExceptionwhen requested type is not registered.

◆ createOrFail()

template<typename Key , typename BaseType , typename... Args>
Factory< Key, BaseType, Args... >::BasePtr spark::patterns::Factory< Key, BaseType, Args >::createOrFail ( const Key & key,
Args &&... args ) const
nodiscardnoexcept

Creates an object of type BaseType.

Parameters
keyThe key of type Key used to create the object.
argsThe arguments passed to the constructor of the derived class.
Returns
A std::unique_ptr to the created object if the type is registered, else a nullptr.

◆ registeredTypes()

template<typename Key , typename BaseType , typename... Args>
std::vector< Key > spark::patterns::Factory< Key, BaseType, Args >::registeredTypes ( ) const
nodiscardnoexcept

Gets a vector of all registered types in the factory.

Returns
The std::vector containing the keys of all registered types.

◆ registerType()

template<typename Key , typename BaseType , typename... Args>
template<typename TypeToRegister >
void spark::patterns::Factory< Key, BaseType, Args >::registerType ( const Key & key)

Registers a type the factory can create.

Template Parameters
TypeToRegisterThe type of the derived class to register.
Parameters
keyThe key used to reference the object.
Exceptions
spark::base::BadArgumentExceptionwhen type is already registered.