Presumably your map wants to have ProductCreator as the data type instead.
Thank you very much to make me open my eyes.... I realized it when i
read you... for a beginer like me, templates are a little bit scary and
i usually don't take the time to decompose each non-trivials portion of
code into various shorter and simpler ones.
Now it's working like i wanted.
For others like me wanting to use a template ObjectFactory, here is the
code of the solution:
/*************** *************** *************** *************** *************** ****/
/*
/*************** *************** *************** *************** *************** ****/
file : objfactory.h
*/
#ifndef __OBJFACTORY_H
#define __OBJFACTORY_H
#include <map>
using namespace std;
template
<
class AbstractProduct ,
class IdentifierType,
class ProductCreator = AbstractProduct * (*)()
>
class Factory
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.i nsert( typename AssocMap::value _type(id,
creator)).secon d;
}
bool Unregister(cons t IdentifierType& id)
{
return associations_.e rase(id) == 1;
}
AbstractProduct * CreateObject(co nst IdentifierType& id)
{
typename AssocMap::const _iterator i = associations_.f ind(id);
if (i != associations_.e nd())
{
return (i->second)();
}
//handle error
}
private:
typedef std::map<Identi fierType, ProductCreator AssocMap;
AssocMap associations_;
};
#endif
/*************** *************** *************** *************** *************** ****/
and now a little working example. Say I want my factory to a Shape
factory, where i can instance Line, Triangle and others Shape-derived
classes, so
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
class Shape
{
public:
virtual void Draw() const {}
virtual void Rotate(double angle){}
virtual void Zoom(double zoomFactor){}
Shape(){cout<<" Shape constructor"<<e ndl;}
};
class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Line(){cout<<"L ine constructor"<<e ndl;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Triangle(){cout <<"Triangle constructor"<<e ndl;}
};
#endif // SHAPE_H
/*************** *************** *************** *************** *************** ****/
Now how i create Shapes with the factory, so simple...
//main.cpp
// i declare a Shape factory where Shape-and-derived types are known as
integers.
Factory<Shape, int>myFactory;
// here i register the Line class
namespace
{
Shape* CreateLine()
{
return new Line;
}
// The ID of class Line
const int LINE = 1;
const bool line_registered = myFactory.Regis ter(LINE, &CreateLine) ;
}
// here i register the triangle class
namespace
{
Shape* CreateTriangle( )
{
return new Triangle;
}
// The ID of class Triangle
const int TRIANGLE = 2;
const bool triangle_regist ered = myFactory.Regis ter(TRIANGLE,
&CreateTriangle );
}
int main()
{
Line* line= myFactory.Creat eObject(LINE);
Triangle* triangle= myFactory.Creat eObject(TRIANGL E);
//system("PAUSE") ; //only for gcc, to see console output...
return 0;
}