/* * Creator_impl.hpp * * Created on: Jan 3, 2011 * Author: heber */ #ifndef CREATOR_IMPL_HPP_ #define CREATOR_IMPL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif /** This class wraps another one and implements a create() function for it. * * This pattern is sometimes called 'type erasure': we want to present * the user with a abstract interface only (name) but in * order to create a copy (such that handed out instances are independent from * one another) we have to know about the type of underlying (complex) class. * * Hence, we create this templated creator class (name##_Creator), which has an * abstract creation interface (I##name##_Creator). * * This pattern is e.g. very useful when multiple (proto)types have to be * stored in the table of a factory. Factories can serve only a general * (abstract) type and not the specific one due to a necessary general function * signature. * *

Howto

* * Wrap the classes to instantiate by the factory in this creator * pattern. The factory in its tables only stores the abstract creation * interface (I##name##_Creator). Let the factory call create() when it has to * produce an instance, which is passed through to the templated creation class * (name##_Creator), dealing out the copy. * *

Best Practice

* * NOTE: It is best-practice to place the call to CONSTRUCT_CREATOR in its * own header file and include this file only. Otherwise, one might get * redefinition errors. */ #define CONSTRUCT_CREATOR(name) \ class name; \ struct I##name##_Creator \ { \ virtual ~I##name##_Creator() {}; \ virtual name * create() const =0; \ }; \ template \ struct name##_Creator : public I##name##_Creator \ { \ virtual name * create() const { \ return new T(); \ }; \ }; #endif /* CREATOR_IMPL_HPP_ */