Changeset 9869 in orxonox.OLD for trunk/src/lib/util/loading/factory.h
- Timestamp:
- Oct 3, 2006, 12:19:30 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/loading/factory.h
r8148 r9869 20 20 21 21 22 #ifndef _ FACTORY_H23 #define _ FACTORY_H22 #ifndef __FACTORY_H 23 #define __FACTORY_H 24 24 25 25 class BaseObject; … … 27 27 #include "parser/tinyxml/tinyxml.h" 28 28 #include "base_object.h" 29 #include <vector> 30 #include <list> 29 #include <map> 31 30 32 31 /** … … 34 33 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) 35 34 */ 36 #define CREATE_FACTORY(CLASS_NAME , CLASS_ID) \37 tFactory<CLASS_NAME> * global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)35 #define CREATE_FACTORY(CLASS_NAME) \ 36 tFactory<CLASS_NAME> global_##CLASS_NAME##_Factory = tFactory<CLASS_NAME>(CLASS_NAME::staticClassID()) 38 37 39 38 //! The Factory is a loadable object handler 40 class Factory : public BaseObject { 41 42 public: 39 class Factory : public BaseObject 40 { 41 //! Declare the ObjectList at the BaseObject List 42 ObjectListDeclaration(Factory); 43 public: 43 44 virtual ~Factory (); 44 45 45 static void deleteFactories(); 46 static BaseObject* fabricate(const std::string& className); 47 static BaseObject* fabricate(const ClassID& classID); 48 static BaseObject* fabricate(const TiXmlElement* root); 46 49 47 static BaseObject* fabricate(const std::string& className); 48 static BaseObject* fabricate(ClassID classID); 49 static BaseObject* fabricate(const TiXmlElement* root = NULL); 50 bool operator==(int classID) const; 51 bool operator==(const std::string& className) const; 52 /** @param classID the ID to compare @returns true if the ID's match */ 53 bool operator==(const ClassID& classID) const { return _classID == classID; }; 50 54 51 55 52 bool operator==(ClassID classID) const; 53 bool operator==(const char* className) const; 54 bool operator==(const std::string& className) const; 56 void debug() const; 57 static void debugAll(); 55 58 56 protected: 57 Factory (const std::string& factoryName, ClassID classID); 58 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0; 59 protected: 60 Factory (const ClassID& id); 61 /** 62 * @brief The core function of the Factory. Creates objects of the Factories Type. 63 * @param root The TiXmlElement of the Factory. 64 * @returns the created Object in BaseType* format. 65 */ 66 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0; 59 67 60 protected: 61 const ClassID classID; //!< The Class-Identifyer of the Factory. 62 const std::string className; //!< The name of the Class. 63 static std::list<Factory*>* factoryList; //!< List of Registered Factories 68 private: 69 //! Copy Constructor is hidden. 70 Factory (const Factory&) {}; 71 72 protected: 73 /** The Type of the FactoryMap that is sorted by ID */ 74 typedef std::map<const ClassID, Factory*> FactoryIDMap; 75 /** The Type of the FactoryMap that is sorted by Name */ 76 typedef std::map<std::string, Factory*> FactoryStringMap; 77 78 const ClassID _classID; //!< The Class-Identifyer of the Factory. 79 static FactoryIDMap _factoryIDMap; //!< List of Registered Factories 80 static FactoryStringMap _factoryStringMap; //!< List of Registered Factories 64 81 }; 65 82 … … 70 87 template<class T> class tFactory : public Factory 71 88 { 72 public: 73 /** 74 * @brief creates a new type Factory to enable the loading of T 75 * @param factoryName the Name of the Factory to load. 76 * @param classID the ID of the Class to be created. 77 */ 78 tFactory (const char* factoryName, ClassID classID) 79 : Factory(factoryName, classID) 89 public: 90 /** 91 * @brief creates a new type Factory to enable the loading of T 92 * @param classID the ID of the Class to be created. 93 */ 94 tFactory (const ClassID& classID) 95 : Factory(classID) 80 96 { } 97 /** 98 * @brief copy constructor 99 * @param factory the Factory to copy 100 */ 101 tFactory (const tFactory& factory) : Factory(factory._classID) {}; 81 102 82 private: 83 /** 84 * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*) 85 * @param root the TiXmlElement T should load parameters from. 86 * @return the newly fabricated T. 87 */ 88 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const 89 { 90 return new T(root); 91 } 103 /** 104 * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*) 105 * @param root the TiXmlElement T should load parameters from. 106 * @return the newly fabricated T. 107 */ 108 virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const 109 { 110 return new T(root); 111 } 92 112 }; 93 113
Note: See TracChangeset
for help on using the changeset viewer.