|
LDMX Software
|
Factory to dynamically create objects derived from a specific prototype class. More...
#include <Factory.h>
Public Types | |
| using | PrototypeMaker = PrototypePtr (*)(PrototypeConstructorArgs...) |
| the signature of a function that can be used by this factory to dynamically create a new object. | |
Public Member Functions | |
| template<typename DerivedType > | |
| uint64_t | declare (const std::string &derived_type_name) |
| register a new object to be constructible | |
| virtual std::optional< PrototypePtr > | make (const std::string &full_name, PrototypeConstructorArgs... maker_args) |
| make a new object by name | |
| Factory (Factory const &)=delete | |
| delete the copy constructor | |
| void | operator= (Factory const &)=delete |
| delete the assignment operator | |
| Factory ()=default | |
| default constructor that does nothing | |
| virtual | ~Factory ()=default |
| default destructor that is virtual for Warehouse override | |
Static Private Member Functions | |
| template<typename DerivedType > | |
| static PrototypePtr | maker (PrototypeConstructorArgs... args) |
| make a new DerivedType returning a PrototypePtr | |
Private Attributes | |
| std::unordered_map< std::string, PrototypeMaker > | library_ |
| library of possible objects to create | |
Factory to dynamically create objects derived from a specific prototype class.
This factory should be a singleton class so its copy constructor and assignment operator have been deleted.
| Prototype | the type of object that this factory creates. This should be the base class that all types in this factory derive from. |
| PrototypePtr | the type of pointer that the factory creates |
| PrototypeConstructorArgs | parameter pack of arguments to pass to the object constructor. |
The factory itself works in two steps.
Using the factory effectively can be done in situations where many classes all follow the same design structure, but have different implementations for specific steps. In order to reflect this "same design structure", we define an abstract base class for all of our derived classes from which to inherit. This abstract base class is our "prototype".
Below is a rudimentary example that shows you the basics of this class.
This LibraryEntry prototype class satisfies our requirements.
Here are a few example derived classes.
Since the FACTORY_REGISTRATION macro defines a function whose result produces a static variable, the function is called at library-load time, the registration of our various library entries is automatically done before the execution of main (or after if the libraries are dynamically loaded with dlopen). For simplicity, let's compile these sources files together with a main defined below.
Compiling these files together into the fave-things executable would then lead to the following behavior.
| using framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::PrototypeMaker = PrototypePtr (*)(PrototypeConstructorArgs...) |
|
inline |
register a new object to be constructible
We insert the new object into the library without checking if we are overwriting anything.
| DerivedType | object type to declare |
| [in] | derived_type_name | name of DerivedType to be used in the mapping |
Definition at line 211 of file Factory.h.
References framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::library_, and framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::maker().
|
inlinenodiscardvirtual |
make a new object by name
We look through the library to find the requested object. If found, we create one, and return it wrapped in a std::optional. If not found, we return std::nullopt.
The arguments to the maker are determined at compiletime using the template parameters of Factory.
| [in] | full_name | name of class to create same name as passed to declare |
| [in] | maker_args | parameter pack of arguments to pass on to maker |
Reimplemented in framework::FactoryWithWarehouse< Prototype, PrototypePtr, PrototypeConstructorArgs >.
Definition at line 233 of file Factory.h.
References framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::library_.
|
inlinestaticprivate |
make a new DerivedType returning a PrototypePtr
Basically a copy of what std::make_unique or std::make_shared do but with the following changes:
This is where we required that PrototypePtr has the same behavior as STL smart pointers. The PrototypePtr class must be able to be constructed from a pointer to a derived class and must take ownership of the new object.
| DerivedType | type of derived object we should create |
| [in] | args | constructor arguments for derived type construction |
Definition at line 275 of file Factory.h.
Referenced by framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::declare().
|
private |
library of possible objects to create
Definition at line 281 of file Factory.h.
Referenced by framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::declare(), and framework::Factory< Prototype, PrototypePtr, PrototypeConstructorArgs >::make().