Object factory for subclasses of type T. More...
#include <factory.hh>
Classes | |
struct | unknown_type |
Exception to indicate an unregistered subclass. More... | |
Static Public Member Functions | |
static T * | create (K k) |
Create an instance of a subclass of T. More... | |
template<typename A > | |
static T * | create (K k, A a) |
Create an instance of a subclass of T. More... | |
template<typename A , typename B > | |
static T * | create (K k, A a, B b) |
Create an instance of a subclass of T. More... | |
Object factory for subclasses of type T.
This class defines an object factory for subclasses of some base type T. Each subclass is expected to be identified uniquely by a value of type K (usually, an integral type). A function matching the signature specified by type F (usually a function pointer) should be registered with the factory and it should use the new operator to create an instance of the subclass identifed by K and upcast that to a T*.
The type M is expected to be an associative container that maps subclass identifier keys of type K to their factory functions of type F. By default, this type is factory_map<K,F>, which stores keys and factory functions and no client data. In minxlib, however, we usually use a factory_map<K,F,D>, where D is usually a function pointer for the pythonize functions.
The factory_map class defined above is good enough for most purposes and there should be little need to implement a whole new type M. However, if such madness becomes necessary, then, at the very least, from the factory class's perspective, the type M has to define a static get_factory() method that will return an F given a K.
|
static |
Create an instance of a subclass of T.
k | Identifier for the desired subclass. |
This function is for creating objects of class identified by k using the default constructor for that class. The object will be upcast to the base class T.
This function will retrieve the factory function for the desired class from the factory map (type M of this template) and use that to create the object. The subclass identified by k should have registered its factory function with the factory map. The factory function should create an instance of the subclass using the new operator and upcast the result to the base class type T.
If this function fails to retrieve a factory function for the given key k, it will throw an unknown_type exception. Clients should be prepared to handle this exception.
|
static |
Create an instance of a subclass of T.
k | Identifier for the desired subclass. |
a | Argument for the subclass's constructor. |
This function is for creating objects of class identified by k using the one-argument constructor for that class. The object will be upcast to the base class T.
This function will retrieve the factory function for the desired class from the factory map (type M of this template) and use that to create the object. The subclass identified by k should have registered its factory function with the factory map. The factory function should create an instance of the subclass using the new operator, passing the constructor the argument a, and upcast the result to the base class type T.
If this function fails to retrieve a factory function for the given key k, it will throw an unknown_type exception. Clients should be prepared to handle this exception.
|
static |
Create an instance of a subclass of T.
k | Identifier for the desired subclass. |
a | First argument for the subclass's constructor. |
b | Second argument for the subclass's constructor. |
This function is for creating objects of class identified by k using the two-argument constructor for that class. The object will be upcast to the base class T.
This function will retrieve the factory function for the desired class from the factory map (type M of this template) and use that to create the object. The subclass identified by k should have registered its factory function with the factory map. The factory function should create an instance of the subclass using the new operator, passing the constructor the arguments a and b, and upcast the result to the base class type T.
If this function fails to retrieve a factory function for the given key k, it will throw an unknown_type exception. Clients should be prepared to handle this exception.