GMP 0.3.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.
 
AbstractProductcreate (const std::string &key, const ConstructorArgs &... args)
 Create a concrete product with raw new semantics.
 
std::shared_ptr< AbstractProductcreate_shared (const std::string &key, const ConstructorArgs &... args)
 Create a concrete product and return it as std::shared_ptr.
 
std::unique_ptr< AbstractProductcreate_unique (const std::string &key, const ConstructorArgs &... args)
 Create a concrete product and return it as std::unique_ptr.
 

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 57 of file object_factory.hpp.

Member Function Documentation

◆ create()

template<typename AbstractProduct , typename... ConstructorArgs>
AbstractProduct * gmp::object_factory< AbstractProduct, ConstructorArgs >::create ( const std::string &  key,
const ConstructorArgs &...  args 
)
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 115 of file object_factory.hpp.

References gmp::enum_values().

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 
)
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 130 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 
)
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 144 of file object_factory.hpp.

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

◆ 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 95 of file object_factory.hpp.

References gmp::enum_values().


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