Public Types | Public Member Functions | Protected Attributes

Factory< T > Class Template Reference

Inheritance diagram for Factory< T >:
FactoryBase

List of all members.

Public Types

typedef T *(* Creator )()

Public Member Functions

void addCreator (Creator, const char *nm, const char *username=0)
T * create (const char *nm) const
 Name may be not be null.

Protected Attributes

TypeSet< Creatorcreators_

Detailed Description

template<class T>
class Factory< T >

Generalized static factory that can deliver instances of T, when no variable is needed in the creation.

Usage. Each implementation of the base class T must add themselves to the factory when application starts up, e.g. in an initClass() function:

class A
{
public:
    virtual int         myFunc()        = 0;
};

class B : public A
{
public:
    static A*           createFunc() { return new B; }
    static void         initClass()
                        { thefactory.addCreator(createFunc,"MyKeyword",
                                                "My Name"); }
                            
    int                 myFunc();
};

Two macros are available to make a static accessfuncion for the factory:

mDefineFactory( ClassName, FunctionName );

that will create a static function that returns an instance to Factory<ClassName>. If the function is a static member of a class, it has to be defined with the mDefineFactoryInClass macro.

The static function must be implemented in a src-file with the macro

mImplFactory( ClassName, FunctionName );

Member Typedef Documentation

template<class T >
typedef T*(* Factory< T >::Creator)()

Member Function Documentation

template<class T >
void Factory< T >::addCreator ( Creator  cr,
const char *  nm,
const char *  username = 0 
) [inline]

Name may be not be null If nm is found, old creator is replaced. nm can be a SeparString, separated by cSeparator(), allowing multiple names, where the first name will be the main name that is returned in getNames.

template<class T >
T * Factory< T >::create ( const char *  nm  )  const [inline]

Name may be not be null.


Member Data Documentation

template<class T >
TypeSet<Creator> Factory< T >::creators_ [protected]