Changeset 6038 for sandbox/src/libraries/core/ClassFactory.h
- Timestamp:
- Nov 5, 2009, 9:22:22 PM (14 years ago)
- Location:
- sandbox
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox
- Property svn:mergeinfo changed
-
sandbox/src/libraries/core/ClassFactory.h
r5782 r6038 42 42 43 43 #include "util/Debug.h" 44 #include "Factory.h"45 44 #include "Identifier.h" 46 45 47 46 namespace orxonox 48 47 { 48 // ########################### 49 // ### Factory ### 50 // ########################### 51 //! Base-class of ClassFactory. 52 class _CoreExport Factory 53 { 54 public: 55 virtual ~Factory() {}; 56 virtual BaseObject* fabricate(BaseObject* creator) = 0; 57 }; 58 49 59 // ############################### 50 60 // ### ClassFactory ### … … 52 62 //! The ClassFactory is able to create new objects of a specific class. 53 63 template <class T> 54 class ClassFactory : public BaseFactory64 class ClassFactory : public Factory 55 65 { 56 66 public: 57 static bool create(const std::string& name); 58 BaseObject* fabricate(BaseObject* creator); 67 /** 68 @brief Constructor: Adds the ClassFactory to the Identifier of the same type. 69 @param name The name of the class 70 @param bLoadable True if the class can be loaded through XML 71 */ 72 ClassFactory(const std::string& name, bool bLoadable = true) 73 { 74 COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl; 75 ClassIdentifier<T>::getIdentifier(name)->addFactory(this); 76 } 59 77 60 private: 61 ClassFactory() {} // Don't create 62 ClassFactory(const ClassFactory& factory) {} // Don't copy 63 virtual ~ClassFactory() {} // Don't delete 64 65 static T* createNewObject(BaseObject* creator); 78 /** 79 @brief Creates and returns a new object of class T. 80 @return The new object 81 */ 82 inline BaseObject* fabricate(BaseObject* creator) 83 { 84 return static_cast<BaseObject*>(new T(creator)); 85 } 66 86 }; 67 68 /**69 @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory.70 @param name The name of the class71 @param bLoadable True if the class can be loaded through XML72 @return Always true (this is needed because the compiler only allows assignments before main())73 */74 template <class T>75 bool ClassFactory<T>::create(const std::string& name)76 {77 COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl;78 ClassIdentifier<T>::getIdentifier(name)->addFactory(new ClassFactory<T>);79 Factory::add(name, ClassIdentifier<T>::getIdentifier());80 81 return true;82 }83 84 /**85 @brief Creates and returns a new object of class T.86 @return The new object87 */88 template <class T>89 inline BaseObject* ClassFactory<T>::fabricate(BaseObject* creator)90 {91 return ClassFactory<T>::createNewObject(creator);92 }93 94 /**95 @brief Creates and returns a new object of class T; this is a wrapper for the new operator.96 @return The new object97 */98 template <class T>99 inline T* ClassFactory<T>::createNewObject(BaseObject* creator)100 {101 return new T(creator);102 }103 87 } 104 88
Note: See TracChangeset
for help on using the changeset viewer.