GenericFactory

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

GenericFactory

Post by sudi »

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.

Have fun!!

EDIT: totally forgot the factory itself
CGenericFactory.h
IGenericCreator.h

Example:

Code: Select all

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
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Nice snippet :)
This would have been useful for me about one month ago, but then I started reading "Modern c++ Design" 8)
Working on game: Marrbles (Currently stopped).
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

serengeor wrote:Nice snippet :)
This would have been useful for me about one month ago, but then I started reading "Modern c++ Design" 8)
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.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Sudi wrote:
serengeor wrote:Nice snippet :)
This would have been useful for me about one month ago, but then I started reading "Modern c++ Design" 8)
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....
Yeah, tough my implementation of factory is a little different, doesn't use templates as I still can't make myself to write templated code :D
Working on game: Marrbles (Currently stopped).
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

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.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Sudi wrote:Well then perfect for you to try this one and learn how to write template code^^
I can write template code, but I'm more comfortable with non template alternatives 8)
Working on game: Marrbles (Currently stopped).
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

i see.
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.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

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
Post Reply