Changeset 5935 for code/branches/pickup/src/libraries/core/ClassFactory.h
- Timestamp:
- Oct 13, 2009, 5:05:17 PM (15 years ago)
- Location:
- code/branches/pickup
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pickup
- Property svn:mergeinfo changed
-
code/branches/pickup/src/libraries/core/ClassFactory.h
r5781 r5935 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, bool bLoadable = true); 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 ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable); 77 } 59 78 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); 79 /** 80 @brief Creates and returns a new object of class T. 81 @return The new object 82 */ 83 inline BaseObject* fabricate(BaseObject* creator) 84 { 85 return static_cast<BaseObject*>(new T(creator)); 86 } 66 87 }; 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, bool bLoadable)76 {77 COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl;78 ClassIdentifier<T>::getIdentifier(name)->addFactory(new ClassFactory<T>);79 ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable);80 Factory::add(name, ClassIdentifier<T>::getIdentifier());81 82 return true;83 }84 85 /**86 @brief Creates and returns a new object of class T.87 @return The new object88 */89 template <class T>90 inline BaseObject* ClassFactory<T>::fabricate(BaseObject* creator)91 {92 return ClassFactory<T>::createNewObject(creator);93 }94 95 /**96 @brief Creates and returns a new object of class T; this is a wrapper for the new operator.97 @return The new object98 */99 template <class T>100 inline T* ClassFactory<T>::createNewObject(BaseObject* creator)101 {102 return new T(creator);103 }104 88 } 105 89
Note: See TracChangeset
for help on using the changeset viewer.