/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Christian Meyer co-programmer: Benjamin Grauer */ /*! \file factory.h \brief A loadable object handler */ #ifndef _FACTORY_H #define _FACTORY_H class BaseObject; #include "tinyxml.h" #include "load_param.h" #include "debug.h" /** Creates a factory to a Loadable Class. this should be used at the beginning of all the Classes that should be loadable (in the cc-file) */ #define CREATE_FACTORY(CLASS_NAME) tFactory* global_##CLASS_NAME##Factory = new tFactory(#CLASS_NAME) //! The Factory is a loadable object handler class Factory { public: Factory (const char* factoryName = NULL); ~Factory (); virtual BaseObject* fabricate(const TiXmlElement* root); void initialize(); void registerFactory( Factory* factory); void setFactoryName(const char* factoryName); /** \returns the name of the factory */ const char* getFactoryName() { return this->factoryName; }; /** \brief sets the Next factory in the list \param nextFactory the next factory */ inline void setNext( Factory* nextFactory) { this->next = nextFactory; }; /** \returns the next factory */ Factory* getNext(void) const { return this->next; }; protected: char* factoryName; //!< the name of the factory private: Factory* next; //!< pointer to the next factory. }; /** \brief a factory that is able to load any kind of Object (this is a Functor) */ template class tFactory : public Factory { public: tFactory(const char* factoryName); virtual ~tFactory(); private: BaseObject* fabricate(const TiXmlElement* root); }; /** \brief construnts a factory with \param factoryName the name of the factory */ template tFactory::tFactory(const char* factoryName) : Factory(factoryName) { PRINTF(5)("fileName: %s loadable\n", this->factoryName); } template tFactory::~tFactory() {} template BaseObject* tFactory::fabricate(const TiXmlElement* root) { if(!strcmp(root->Value(), getFactoryName())) return new T ( root); else if( getNext() != NULL) return getNext()->fabricate( root); else return NULL; } #endif /* _FACTORY_H */