| Revision 4 (by moose, 2006/06/25 00:12:54) |
patching Dynamo/Factory.h to use the new Factory Interface
|
#include <cstdio>
#include <vector>
#define log(a, b...) do{\
char __tmp__[2048]; \
snprintf(__tmp__, 2048, a,##b); \
fprintf(stderr, "%i:%s:%s: %s\n", __LINE__, __FUNCTION__, __FILE__, __tmp__); \
} while(0)
namespace Dynamo {
namespace Factory {
template<typename Object>
class CreateObjectArguments {
public:
};
template<typename Object>
class Interface {
protected:
typedef std::vector< Interface< Object > * > FactoryList;
static FactoryList _factoryList;
public:
bool Registered;
Interface() { log(""); }
virtual ~Interface() { log("~"); }
typedef FactoryList List;
virtual const char *name() = 0;
virtual const char *desc() = 0;
// somtimes you will want to know if its possible to create the object...
virtual bool canCreateObject() = 0;
// fac->createObject<int>(123);
virtual Object createObject() = 0;
static FactoryList & factoryList()
{
static FactoryList _factoryList;
return _factoryList;
}
static bool Register( Interface *fac )
{
log("Register: %s : %s", fac->name(), fac->desc());
factoryList().push_back(fac);
fac->Registered = true;
return true;
}
static void printList()
{
log("printList");
FactoryList &facList = factoryList();
for(int i=0; i < facList.size(); ++i) {
Interface *obj = facList.at(i);
log("[%i]: %s : %s", i,obj->name(), obj->desc());
}
}
};
}
namespace Object {
class Interface {
public:
Interface() { log("new Object::Interface!"); }
virtual ~Interface() { log("~"); }
};
class Interface2 : public Interface {
public:
Interface2() { log("new Object::Interface2!"); }
virtual ~Interface2() { log("~"); }
};
//FactoryList< Interface * > objectFactoryList;
class Factory : public Dynamo::Factory::Interface< Dynamo::Object::Interface * > {
public:
virtual const char *name() { return "Object::Factory"; }
virtual const char *desc() { return "Factory for Object types"; }
virtual bool canCreateObject() { return true; }
virtual Interface *createObject() { return new Interface(); }
//protected:
Factory() { log("new Object::Factory"); }
virtual ~Factory() { log("~"); }
};
class Factory2 : public Factory {
public:
virtual const char *name() { return "Object2::Factory"; }
virtual const char *desc() { return "Factory for Object2 types"; }
virtual bool canCreateObject() { return true; }
virtual Interface2 *createObject() { return new Interface2(); }
Factory2() { log("new Object2::Factory"); }
virtual ~Factory2() { log("~"); }
};
bool FactoryRegistered = Factory::Register( new Factory );
bool Factory2Registered = Factory2::Register( new Factory2 );
}
}
int main(int argc, char **argv)
{
log("startup");
Dynamo::Object::Factory::printList();
Dynamo::Object::Factory::List &facList = Dynamo::Object::Factory::factoryList();
for(int i=0; i < facList.size(); ++i) {
Dynamo::Object::Factory *factory = (Dynamo::Object::Factory *)facList.at(i);
log("registered: %i, %p", factory->Registered, &(factory->Registered));
delete factory->createObject();
}
log("end");
return 0;
}