I noticed that alot of people do not know about factories and how easy it is to create dynamic enviroments using them is. So instead of showing it or pointing the people towards the irrlicht which actually uses them as well. I created a generic factory which is able to create any type of object which out much hassle. The factory is pretty customizeable to either use standard stl types or the irrlicht types.
class CInterface
{
public:
CInterface(void){}
virtual ~CInterface(void){}
virtual void print(void)
{
printf("CInterface\n");
}
};
typedef CGenericFactory<CInterface*> InterfaceFactory;
#include <irrlicht.h>
typedef CGenericFactory<CInterface*, irr::core::stringc, irr::core::array<IGenericCreator<CInterface*, irr::core::stringc>* > > InterfaceFactoryIrr;
class hello : public CInterface
{
public:
virtual void print(void)
{
printf("CInterface:hello\n");
}
};
class moin : public CInterface
{
public:
virtual void print(void)
{
printf("CInterface:moin\n");
}
};
typedef CGenericFactory<int> IntegerFactory;
class intCreator : public IntegerFactory::Creator
{
public:
intCreator() : IntegerFactory::Creator("")
{
}
int create(void)
{
return atoi(Checked.c_str());
}
bool checkIdent(const std::string& ident)
{
Checked = ident;
return true;
}
std::string Checked;
};
int factoryFunc(const IntegerFactory* ifP)
{
return ifP->create("5");
}
int main(int argc, char* args[])
{
InterfaceFactory* factory = new InterfaceFactory;
factory->registerTypePointer<hello>("hello");
factory->registerTypePointer<moin>("moin");
CInterface* i = factory->create("hello");
i->print();
i = factory->create(1);
i->print();
delete factory;
IntegerFactory ifactory;
ifactory.registerType(new intCreator());
int integer = ifactory.create("847898");
printf("Created: %i\n", integer);
integer = 0;
integer = factoryFunc(&ifactory);
printf("Created: %i\n", integer);
InterfaceFactoryIrr* factoryIrr = new InterfaceFactoryIrr;
factoryIrr->registerTypePointer<hello>("hello");
factoryIrr->registerTypePointer<moin>("moin");
i = factoryIrr->create("hello");
i->print();
i = factoryIrr->create(1);
i->print();
delete factoryIrr;
return 0;
}
Last edited by sudi on Thu Feb 24, 2011 7:54 pm, edited 2 times in total.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
serengeor wrote:Nice snippet
This would have been useful for me about one month ago, but then I started reading "Modern c++ Design"
Is there a better method than factories?
EDIT: nevermind checked out the book...it talks about factories. I guess you meant you already know about them now....
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Well then perfect for you to try this one and learn how to write template code^^
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
nice snippets. shows how to save/load your game. . this way you have not to depends on "save points" but can save the game anytime you want. that's not the only use of factories, but one possible(and usefull)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me