GMP 0.4.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
gmp::object_factory< AbstractProduct, ConstructorArgs > Class Template Reference

A singleton-backed runtime factory that creates products by string key. More...

#include <object_factory.hpp>

Classes

struct  register_type
 Register a concrete product type under a string key. More...
 

Public Member Functions

void unregister_type (const std::string &key)
 Remove a previously registered product key from the factory.
 
bool contains (const std::string &key) const
 Test whether a product key is registered.
 
std::vector< std::string > registered_keys () const
 Return all registered product keys.
 
AbstractProductcreate (const std::string &key, const ConstructorArgs &... args) const
 Create a concrete product with raw new semantics.
 
std::shared_ptr< AbstractProductcreate_shared (const std::string &key, const ConstructorArgs &... args) const
 Create a concrete product and return it as std::shared_ptr.
 
std::unique_ptr< AbstractProductcreate_unique (const std::string &key, const ConstructorArgs &... args) const
 Create a concrete product and return it as std::unique_ptr.
 
std::unique_ptr< AbstractProducttry_create_unique (const std::string &key, const ConstructorArgs &... args) const
 Try to create a product, returning an empty pointer for an unknown key.
 
std::shared_ptr< AbstractProducttry_create_shared (const std::string &key, const ConstructorArgs &... args) const
 Try to create a shared product, returning an empty pointer for an unknown key.
 

Detailed Description

template<typename AbstractProduct, typename... ConstructorArgs>
class gmp::object_factory< AbstractProduct, ConstructorArgs >

object_factory stores a mapping from textual identifiers to constructor callbacks for concrete types derived from AbstractProduct. Product types are typically registered through register_type or the GMP_FACTORY_REGISTER(...) helper macro, and can later be instantiated by calling create, create_shared, or create_unique.

The factory itself is a singleton so registrations and lookups operate on a single process-wide registry for a given <AbstractProduct, ConstructorArgs...> specialization.

Template Parameters
AbstractProductThe abstract base type returned by the factory.
ConstructorArgsThe constructor argument types forwarded to concrete products.
Since
version 1.0.0

Definition at line 58 of file object_factory.hpp.

Member Function Documentation

◆ contains()

template<typename AbstractProduct , typename... ConstructorArgs>
bool gmp::object_factory< AbstractProduct, ConstructorArgs >::contains ( const std::string &  key) const
inline

Definition at line 103 of file object_factory.hpp.

◆ create()

template<typename AbstractProduct , typename... ConstructorArgs>
AbstractProduct * gmp::object_factory< AbstractProduct, ConstructorArgs >::create ( const std::string &  key,
const ConstructorArgs &...  args 
) const
inline

This function looks up key, invokes the registered constructor callback, and returns the resulting object as a raw pointer.

Parameters
keyThe registered lookup key of the desired concrete type.
argsThe constructor arguments forwarded to the concrete product.
Returns
A raw pointer to the newly created product.
Exceptions
std::invalid_argumentIf key is not registered.
Warning
The caller owns the returned pointer and must delete it to avoid memory leaks. Prefer create_shared() or create_unique() when ownership should be explicit.

Definition at line 135 of file object_factory.hpp.

References gmp::enum_values(), and gmp::object_factory< AbstractProduct, ConstructorArgs >::try_create_unique().

Referenced by gmp::object_factory< AbstractProduct, ConstructorArgs >::create_shared(), and gmp::object_factory< AbstractProduct, ConstructorArgs >::create_unique().

◆ create_shared()

template<typename AbstractProduct , typename... ConstructorArgs>
std::shared_ptr< AbstractProduct > gmp::object_factory< AbstractProduct, ConstructorArgs >::create_shared ( const std::string &  key,
const ConstructorArgs &...  args 
) const
inline
Parameters
keyThe registered lookup key of the desired concrete type.
argsThe constructor arguments forwarded to the concrete product.
Returns
A shared pointer owning the newly created product.
Exceptions
std::invalid_argumentIf key is not registered.

Definition at line 152 of file object_factory.hpp.

References gmp::object_factory< AbstractProduct, ConstructorArgs >::create(), and gmp::enum_values().

◆ create_unique()

template<typename AbstractProduct , typename... ConstructorArgs>
std::unique_ptr< AbstractProduct > gmp::object_factory< AbstractProduct, ConstructorArgs >::create_unique ( const std::string &  key,
const ConstructorArgs &...  args 
) const
inline
Parameters
keyThe registered lookup key of the desired concrete type.
argsThe constructor arguments forwarded to the concrete product.
Returns
A unique pointer owning the newly created product.
Exceptions
std::invalid_argumentIf key is not registered.

Definition at line 166 of file object_factory.hpp.

References gmp::object_factory< AbstractProduct, ConstructorArgs >::create(), and gmp::enum_values().

◆ registered_keys()

template<typename AbstractProduct , typename... ConstructorArgs>
std::vector< std::string > gmp::object_factory< AbstractProduct, ConstructorArgs >::registered_keys ( ) const
inline

Definition at line 110 of file object_factory.hpp.

References gmp::enum_values().

◆ try_create_shared()

template<typename AbstractProduct , typename... ConstructorArgs>
std::shared_ptr< AbstractProduct > gmp::object_factory< AbstractProduct, ConstructorArgs >::try_create_shared ( const std::string &  key,
const ConstructorArgs &...  args 
) const
inline

◆ try_create_unique()

template<typename AbstractProduct , typename... ConstructorArgs>
std::unique_ptr< AbstractProduct > gmp::object_factory< AbstractProduct, ConstructorArgs >::try_create_unique ( const std::string &  key,
const ConstructorArgs &...  args 
) const
inline

◆ unregister_type()

template<typename AbstractProduct , typename... ConstructorArgs>
void gmp::object_factory< AbstractProduct, ConstructorArgs >::unregister_type ( const std::string &  key)
inline

Removing a key only affects future lookups. It does not modify or destroy any objects that were already created.

Parameters
keyThe registered lookup key to erase.

Definition at line 96 of file object_factory.hpp.


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