[4597] | 1 | /* |
---|
[4250] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christian Meyer |
---|
| 13 | co-programmer: Benjamin Grauer |
---|
| 14 | */ |
---|
| 15 | |
---|
[4597] | 16 | /*! |
---|
[4885] | 17 | * @file factory.h |
---|
| 18 | * @brief A loadable object handler |
---|
[3940] | 19 | */ |
---|
| 20 | |
---|
[4250] | 21 | |
---|
[9869] | 22 | #ifndef __FACTORY_H |
---|
| 23 | #define __FACTORY_H |
---|
[3940] | 24 | |
---|
| 25 | class BaseObject; |
---|
| 26 | |
---|
[5944] | 27 | #include "parser/tinyxml/tinyxml.h" |
---|
[4597] | 28 | #include "base_object.h" |
---|
[9869] | 29 | #include <map> |
---|
[3940] | 30 | |
---|
[4597] | 31 | /** |
---|
[4885] | 32 | * Creates a factory to a Loadable Class. |
---|
| 33 | * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) |
---|
[7221] | 34 | */ |
---|
[9869] | 35 | #define CREATE_FACTORY(CLASS_NAME) \ |
---|
| 36 | tFactory<CLASS_NAME> global_##CLASS_NAME##_Factory = tFactory<CLASS_NAME>(CLASS_NAME::staticClassID()) |
---|
[4487] | 37 | |
---|
| 38 | //! The Factory is a loadable object handler |
---|
[9869] | 39 | class Factory : public BaseObject |
---|
| 40 | { |
---|
| 41 | //! Declare the ObjectList at the BaseObject List |
---|
| 42 | ObjectListDeclaration(Factory); |
---|
| 43 | public: |
---|
[5156] | 44 | virtual ~Factory (); |
---|
[3940] | 45 | |
---|
[7221] | 46 | static BaseObject* fabricate(const std::string& className); |
---|
[9869] | 47 | static BaseObject* fabricate(const ClassID& classID); |
---|
| 48 | static BaseObject* fabricate(const TiXmlElement* root); |
---|
[4597] | 49 | |
---|
[9869] | 50 | bool operator==(int classID) const; |
---|
[7221] | 51 | bool operator==(const std::string& className) const; |
---|
[9869] | 52 | /** @param classID the ID to compare @returns true if the ID's match */ |
---|
| 53 | bool operator==(const ClassID& classID) const { return _classID == classID; }; |
---|
[5982] | 54 | |
---|
[4932] | 55 | |
---|
[9869] | 56 | void debug() const; |
---|
| 57 | static void debugAll(); |
---|
| 58 | |
---|
| 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; |
---|
| 67 | |
---|
| 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 |
---|
[3940] | 81 | }; |
---|
| 82 | |
---|
[4487] | 83 | /** |
---|
[7221] | 84 | * @brief a factory that is able to load any kind of Object |
---|
[5750] | 85 | * (this is a Functor) |
---|
| 86 | */ |
---|
[4004] | 87 | template<class T> class tFactory : public Factory |
---|
| 88 | { |
---|
[9869] | 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) |
---|
[5984] | 96 | { } |
---|
[9869] | 97 | /** |
---|
| 98 | * @brief copy constructor |
---|
| 99 | * @param factory the Factory to copy |
---|
| 100 | */ |
---|
| 101 | tFactory (const tFactory& factory) : Factory(factory._classID) {}; |
---|
[4597] | 102 | |
---|
[9869] | 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 | } |
---|
[4004] | 112 | }; |
---|
| 113 | |
---|
[3940] | 114 | #endif /* _FACTORY_H */ |
---|
| 115 | |
---|