Classes | Static Public Member Functions | List of all members
minxlib::factory< T, K, F, M > Class Template Reference

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...
 

Detailed Description

template<typename T, typename K, typename F, typename M = factory_map<K,F>>
class minxlib::factory< T, K, F, M >

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.

Note
There is an implicit assumption that the type M will be a singleton.

Member Function Documentation

template<typename T , typename K , typename F , typename M >
T * minxlib::factory< T, K, F, M >::create ( k)
static

Create an instance of a subclass of T.

Parameters
kIdentifier for the desired subclass.
Returns
An instance of the desired subclass or an exception.

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.

template<typename T , typename K , typename F , typename M = factory_map<K,F>>
template<typename A >
static T* minxlib::factory< T, K, F, M >::create ( k,
a 
)
static

Create an instance of a subclass of T.

Parameters
kIdentifier for the desired subclass.
aArgument for the subclass's constructor.
Returns
An instance of the desired subclass or an exception.

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.

template<typename T , typename K , typename F , typename M = factory_map<K,F>>
template<typename A , typename B >
static T* minxlib::factory< T, K, F, M >::create ( k,
a,
b 
)
static

Create an instance of a subclass of T.

Parameters
kIdentifier for the desired subclass.
aFirst argument for the subclass's constructor.
bSecond argument for the subclass's constructor.
Returns
An instance of the desired subclass or an exception.

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.