/*! @file ClassFactory.h @brief Definition of the ClassFactory class The ClassFactory is able to create new objects of a specific class. */ #ifndef _ClassFactory_H__ #define _ClassFactory_H__ #include "Identifier.h" namespace orxonox { // ############################### // ### ClassFactory ### // ############################### //! The ClassFactory is able to create new objects of a specific class. template class ClassFactory : public BaseFactory { public: static bool create(); BaseObject* fabricate(); private: ClassFactory() {} // Don't create ClassFactory(const ClassFactory& factory) {} // Don't copy ~ClassFactory() {} // Don't delete static T* createNewObject(); }; /** @brief Adds the ClassFactory to the Identifier of the same type and creates a new object to retrieve the parents. @return True, because the compiler only allows assignments before main() */ template bool ClassFactory::create() { // Add the ClassFactory to the Classidentifier of type T ClassIdentifier::getIdentifier()->addFactory(new ClassFactory); // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. ClassIdentifier::getIdentifier()->startCreatingHierarchy(); #if HIERARCHY_VERBOSE std::cout << "*** Create Factory -> Create Class\n"; #endif BaseObject* temp = ClassIdentifier::getIdentifier()->fabricate(); delete temp; ClassIdentifier::getIdentifier()->stopCreatingHierarchy(); return true; } /** @brief Creates and returns a new object of class T. @return The new object */ template BaseObject* ClassFactory::fabricate() { return ClassFactory::createNewObject(); } /** @brief Creates and returns a new object of class T; this is a wrapper for the new operator. @return The new object */ template T* ClassFactory::createNewObject() { return new T; } } #endif