/* * Creator.hpp * * Created on: Jan 3, 2011 * Author: heber */ #ifndef CREATOR_HPP_ #define CREATOR_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif /** \section (Creator Howto) * * 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 (Creator), which has an * abstract creation interface (ICreator). name distinguishes distinct * ICreator interface classes, the second template argument T is the wrapped * class to create by 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

* * simply include "Creator.hpp" in your file and inherit it, e.g. * @code * template * class MyParticularType : * public CommonInterface, * public Creator< * CommonInterface, * MyParticularType > * {}; * @encode * i.e. here MyParticularType will wrap all desired types T, where * CommonInterface is an abstract base class that defines all stuff common * for all possible types T. * */ template struct ICreator { virtual ~ICreator() {} virtual name * create() const =0; }; template struct Creator : public ICreator { virtual name * create() const { return new T(); } }; #endif /* CREATOR_HPP_ */