template<class AbstractProductT, typename ParameterT, typename InfoT>
class PolymorphicConstructionImpl< AbstractProductT, ParameterT, InfoT >
Template to simplify the polymorphic creation of a number of concrete classes that share the common base class AbstractProductT. Use this to create flexible class hierarchies.
The template assumes a number of things from the user classes:
- AbstractProductT must implement
static void RegisterPlugins()
which will register all available derived classes by calling RegisterPlugin<DerivedClass>()
for each implemented sub-class.
- Each derived class of AbstractProductT must implement
static bool WillHandle(const ParameterT ¶m)
that figures out if the concrete class can cope with the given parameter
- Each derived class must have at least the following constructor:
DerivedClass(const ParameterT ¶m)
which is used to instantiate the concrete class in case it returned true in WillHandle()
- (OPTIONAL) Both AbstractProductT and ConcreteProductTs can override the virtual method
bool Initialize()
which will be called directly after creation of a ConcreteProductT. If it returns false, the constructed in- stance is deleted and the list of plugins is traversed further.
- (OPTIONAL) The ConcreteProductTs can implement a
static InfoT GetInfo()
that can be used for run-time introspection of registered plugins using PolymorphicConstruction<AbstractProductT, ParameterT, InfoT>::Introspect()
A possible class hierarchy could look like this:
PolymorphicConstruction<AbstractNumberCruncher, Parameter> | +–> AbstractNumberCruncher | +–> ConcreteMulticoreNumberCruncher | +–> ConcreteGpuNumberCruncher | +–> ConcreteClusterNumberCruncher
In this example AbstractNumberCruncher::RegisterPlugins() will register all three concrete number cruncher classes. Using the whole thing would look like so:
Parameter param = Parameter(typicalGpuProblem); AbstractNumberCruncher *polymorphicCruncher = AbstractNumberCruncher::Construct(param); polymorphicCruncher->Crunch();
polymorphicCruncher
now points to an instance of ConcreteGpuNumberCruncher and can be used as any other polymorphic class with the interface defined in AbstractNumberCruncher.
Note: PolymorphicCreation goes through the list of registered plugins in the order they have been registered and instantiates the first class that claims responsibility for the given parameter.
- Parameters
-
AbstractProductT | the common base class of all classes that should be polymorphically created. In most cases this will be the class that directly inherits from Polymorphic- Construction. |
ParameterT | the type of the parameter that is used to poly- morphically instantiate one of the subclasses of AbstractProductT |
InfoT | (optional) wrapper type for introspection data of registered plugins. InfoT AbstractProductT::GetInfo() needs to be implemented for each plugin |
Definition at line 180 of file plugin.h.
template<class AbstractProductT, typename ParameterT, typename InfoT>
template<class ConcreteProductT >
Registers a plugin that is polymorphically constructable afterwards. Warning: Multiple registrations of the same ConcreteProductT might lead to undefined behaviour!
- Parameters
-
ConcreteProductT | the concrete implementation of AbstractProductT that should be registered as constructable. |
Note: You shall not need to use this method anywhere in your code except in AbstractProductT::RegisterPlugins().
Definition at line 248 of file plugin.h.