O
orel
Please,
As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design pattern. I'm using gcc 3.4.2 on Mingw.
The factory is the same as the one in Modern C++ design (without error checking) ,
like this :
template
<
class AbstractProduct,
typename IdentifierType,
typename ProductCreator = AbstractProduct* (*)()class Factory
// : public FactoryErrorPolicy<IdentifierType, AbstractProduct>
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.insert(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, AbstractProduct> AssocMap;
AssocMap associations_;
};
and for the example i want to compile i have a simple class hierarchy like that:
class Shape
{
public:
virtual void Draw() const;
virtual void Rotate(double angle);
virtual void Zoom(double zoomFactor);
Shape* operator()() {return new Shape;} // i thought this was necessary but...
};
class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
So what i'm doing is :
const int LINE = 1;
const int TRIANGLE = 2;
typedef Factory<Shape*, int> ShapeFactory;
Line* CreateLine()
{
return new Line;
}
Triangle* CreateTriangle()
{
return new Triangle;
}
void TEST_FUNCTION ()
{
ShapeFactory myFactory;
myFactory.Register(LINE, &CreateLine);
myFactory.Register(TRIANGLE, &CreateTriangle);
}
Compiler listing is :
src\core\objectfactory.cpp:24: instantiated from here
src\include\objectfactory.h:125: error: dependent-name ` std::map<IdentifierType,AbstractProduct,std::less<IdentifierType>,std::allocator<std:
air<const IdentifierType, AbstractProduct> > >::value_type' is parsed as a non-type, but instantiation yields a type
src\include\objectfactory.h:125: note: say `typename std::map<IdentifierType,AbstractProduct,std::less<IdentifierType>,std::allocator<std:
air<const IdentifierType, AbstractProduct> > >::value_type' if a type is meant
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-
As i tried hundreds different implementation to make it work and actually didn't succeed, can someone here help me to understand and use the implementation of the Object Factory design pattern. I'm using gcc 3.4.2 on Mingw.
The factory is the same as the one in Modern C++ design (without error checking) ,
like this :
template
<
class AbstractProduct,
typename IdentifierType,
typename ProductCreator = AbstractProduct* (*)()class Factory
// : public FactoryErrorPolicy<IdentifierType, AbstractProduct>
{
public:
bool Register(const IdentifierType& id, ProductCreator creator)
{
return associations_.insert(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, AbstractProduct> AssocMap;
AssocMap associations_;
};
and for the example i want to compile i have a simple class hierarchy like that:
class Shape
{
public:
virtual void Draw() const;
virtual void Rotate(double angle);
virtual void Zoom(double zoomFactor);
Shape* operator()() {return new Shape;} // i thought this was necessary but...
};
class Line: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
class Triangle: public Shape
{
public:
void Draw () const {return;}
void Rotate (double angle) {return;}
void Zoom (double zoomFactor) {return;}
};
So what i'm doing is :
const int LINE = 1;
const int TRIANGLE = 2;
typedef Factory<Shape*, int> ShapeFactory;
Line* CreateLine()
{
return new Line;
}
Triangle* CreateTriangle()
{
return new Triangle;
}
void TEST_FUNCTION ()
{
ShapeFactory myFactory;
myFactory.Register(LINE, &CreateLine);
myFactory.Register(TRIANGLE, &CreateTriangle);
}
Compiler listing is :
src\core\objectfactory.cpp:24: instantiated from here
src\include\objectfactory.h:125: error: dependent-name ` std::map<IdentifierType,AbstractProduct,std::less<IdentifierType>,std::allocator<std:
src\include\objectfactory.h:125: note: say `typename std::map<IdentifierType,AbstractProduct,std::less<IdentifierType>,std::allocator<std:
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-