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_.insert( typename AssocMap::value_type(id,
creator)).second;
}
bool Unregister(const IdentifierType& id)
{
return associations_.erase(id) == 1;
}
AbstractProduct* CreateObject(const IdentifierType& id)
{
typename AssocMap::const_iterator i = associations_.find(id);
if (i != associations_.end())
{
return (i->second)();
}
//handle error
}
private:
typedef std::map<IdentifierType, 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"<<endl;}
};
class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Line(){cout<<"Line constructor"<<endl;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
Triangle(){cout<<"Triangle constructor"<<endl;}
};
#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.Register(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_registered = myFactory.Register(TRIANGLE,
&CreateTriangle);
}
int main()
{
Line* line= myFactory.CreateObject(LINE);
Triangle* triangle= myFactory.CreateObject(TRIANGLE);
//system("PAUSE"); //only for gcc, to see console output...
return 0;
}