Changeset 9635
- Timestamp:
- Aug 11, 2013, 2:38:03 PM (11 years ago)
- Location:
- code/branches/core6
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core6/src/libraries/core/CoreIncludes.h
r9567 r9635 113 113 114 114 /** 115 @brief Creates the Factory.115 @brief Creates and registers the Factory. 116 116 @param ClassName The name of the class 117 117 */ 118 118 #define CreateFactory(ClassName) \ 119 Factory* _##ClassName##Factory = new orxonox::ClassFactory<ClassName>(#ClassName, true)119 RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, true)) 120 120 121 121 /** 122 @brief Creates the Factory for classes which should not be loaded through XML.122 @brief Creates and registers the Factory for classes which should not be loaded through XML. 123 123 @param ClassName The name of the class 124 124 */ 125 125 #define CreateUnloadableFactory(ClassName) \ 126 Factory* _##ClassName##Factory = new orxonox::ClassFactory<ClassName>(#ClassName, false) 126 RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, false)) 127 128 /** 129 @brief Registers a given Factory. 130 @param ClassName The name of the class 131 */ 132 #define RegisterFactory(ClassName, FactoryInstance) \ 133 Factory* _##ClassName##Factory = FactoryInstance 127 134 128 135 /** -
code/branches/core6/src/libraries/core/CorePrereqs.h
r9597 r9635 126 126 template <class T> 127 127 class ClassFactory; 128 template <class T> 129 class ClassFactoryWithContext; 128 130 template <class T> 129 131 class ClassIdentifier; -
code/branches/core6/src/libraries/core/object/ClassFactory.h
r9633 r9635 50 50 // ### Factory ### 51 51 // ########################### 52 /// Base-class of ClassFactory.52 /// Base-class of all factories. 53 53 class _CoreExport Factory 54 54 { … … 61 61 // ### ClassFactory ### 62 62 // ############################### 63 /// The ClassFactory is able to create new objects of a specific class .63 /// The ClassFactory is able to create new objects of a specific class that require no constructor arguments. 64 64 template <class T> 65 65 class ClassFactory : public Factory … … 84 84 inline Identifiable* fabricate(Context* context) 85 85 { 86 return static_cast<Identifiable*>(new T()); 87 } 88 }; 89 90 // ############################### 91 // ### ClassFactoryWithContext ### 92 // ############################### 93 /// The ClassFactoryWithContext is able to create new objects of a specific class that require a context as constructor argument 94 template <class T> 95 class ClassFactoryWithContext : public Factory 96 { 97 public: 98 /** 99 @brief Constructor: Adds the ClassFactory to the Identifier of the same type. 100 @param name The name of the class 101 @param bLoadable True if the class can be loaded through XML 102 */ 103 ClassFactoryWithContext(const std::string& name, bool bLoadable = true) 104 { 105 orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl; 106 ClassIdentifier<T>::getIdentifier(name)->setFactory(this); 107 ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable); 108 } 109 110 /** 111 @brief Creates and returns a new object of class T. 112 @return The new object 113 */ 114 inline Identifiable* fabricate(Context* context) 115 { 86 116 return static_cast<Identifiable*>(new T(context)); 87 117 } -
code/branches/core6/test/core/object/ClassFactoryTest.cc
r9632 r9635 7 7 TEST(ClassFactoryTest, CanFabricateObject) 8 8 { 9 Factory* factory = new ClassFactory <BaseObject>("BaseObject");9 Factory* factory = new ClassFactoryWithContext<BaseObject>("BaseObject"); 10 10 Identifiable* object = factory->fabricate(NULL); 11 11 ASSERT_TRUE(object != NULL);
Note: See TracChangeset
for help on using the changeset viewer.