[Orxonox-commit 4968] r9637 - in code/branches/core6: src/libraries/core src/libraries/core/object test/core/class test/core/object
landauf at orxonox.net
landauf at orxonox.net
Sun Aug 11 16:23:41 CEST 2013
Author: landauf
Date: 2013-08-11 16:23:41 +0200 (Sun, 11 Aug 2013)
New Revision: 9637
Modified:
code/branches/core6/src/libraries/core/CoreIncludes.h
code/branches/core6/src/libraries/core/object/ClassFactory.h
code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc
code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc
code/branches/core6/test/core/object/ClassFactoryTest.cc
Log:
removed constructor arguments from ClassFactories. use the helper-function registerClass() to add the factory to the corresponding identifier
Modified: code/branches/core6/src/libraries/core/CoreIncludes.h
===================================================================
--- code/branches/core6/src/libraries/core/CoreIncludes.h 2013-08-11 12:41:37 UTC (rev 9636)
+++ code/branches/core6/src/libraries/core/CoreIncludes.h 2013-08-11 14:23:41 UTC (rev 9637)
@@ -116,21 +116,21 @@
@param ClassName The name of the class
*/
#define CreateFactory(ClassName) \
- RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, true))
+ RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true)
/**
@brief Creates and registers the Factory for classes which should not be loaded through XML.
@param ClassName The name of the class
*/
#define CreateUnloadableFactory(ClassName) \
- RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, false))
+ RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false)
/**
@brief Registers a given Factory.
@param ClassName The name of the class
*/
-#define RegisterFactory(ClassName, FactoryInstance) \
- Factory* _##ClassName##Factory = FactoryInstance
+#define RegisterFactory(ClassName, FactoryInstance, bLoadable) \
+ Identifier* _##ClassName##Identifier = orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable)
/**
@brief Returns the Identifier of the given class.
@@ -143,6 +143,31 @@
namespace orxonox
{
/**
+ * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory.
+ */
+ template <class T>
+ inline Identifier* registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true)
+ {
+ return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable);
+ }
+
+ /**
+ * @brief Registers a class in the framework.
+ * @param name The name of the class
+ * @param factory The factory which is able to create new instances of this class
+ * @param bLoadable Whether the class is allowed to be loaded through XML
+ */
+ template <class T>
+ inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
+ {
+ orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
+ Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
+ identifier->setFactory(factory);
+ identifier->setLoadable(bLoadable);
+ return identifier;
+ }
+
+ /**
@brief Returns the Identifier with a given name.
@param name The name of the class
*/
Modified: code/branches/core6/src/libraries/core/object/ClassFactory.h
===================================================================
--- code/branches/core6/src/libraries/core/object/ClassFactory.h 2013-08-11 12:41:37 UTC (rev 9636)
+++ code/branches/core6/src/libraries/core/object/ClassFactory.h 2013-08-11 14:23:41 UTC (rev 9637)
@@ -42,7 +42,6 @@
#include <string>
#include "util/Output.h"
-#include "core/class/Identifier.h"
namespace orxonox
{
@@ -53,31 +52,28 @@
class _CoreExport Factory
{
public:
- virtual ~Factory() {};
+ virtual ~Factory() {}
virtual Identifiable* fabricate(Context* context) = 0;
};
// ###############################
// ### ClassFactory ###
// ###############################
- /// The ClassFactory is able to create new objects of a specific class that require no constructor arguments.
+ /// The ClassFactory is the base-class of all class-spezific factories
template <class T>
class ClassFactory : public Factory
{
+ };
+
+ // ###############################
+ // ### ClassFactoryNoArgs ###
+ // ###############################
+ /// The ClassFactoryNoArgs is able to create new objects of a specific class that require no constructor arguments.
+ template <class T>
+ class ClassFactoryNoArgs : public ClassFactory<T>
+ {
public:
/**
- @brief Constructor: Adds the ClassFactory to the Identifier of the same type.
- @param name The name of the class
- @param bLoadable True if the class can be loaded through XML
- */
- ClassFactory(const std::string& name, bool bLoadable = true)
- {
- orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
- ClassIdentifier<T>::getIdentifier(name)->setFactory(this);
- ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable);
- }
-
- /**
@brief Creates and returns a new object of class T.
@return The new object
*/
@@ -92,22 +88,10 @@
// ###############################
/// The ClassFactoryWithContext is able to create new objects of a specific class that require a context as constructor argument
template <class T>
- class ClassFactoryWithContext : public Factory
+ class ClassFactoryWithContext : public ClassFactory<T>
{
public:
/**
- @brief Constructor: Adds the ClassFactory to the Identifier of the same type.
- @param name The name of the class
- @param bLoadable True if the class can be loaded through XML
- */
- ClassFactoryWithContext(const std::string& name, bool bLoadable = true)
- {
- orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
- ClassIdentifier<T>::getIdentifier(name)->setFactory(this);
- ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable);
- }
-
- /**
@brief Creates and returns a new object of class T.
@return The new object
*/
Modified: code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc
===================================================================
--- code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc 2013-08-11 12:41:37 UTC (rev 9636)
+++ code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc 2013-08-11 14:23:41 UTC (rev 9637)
@@ -1,79 +1,79 @@
-#include <gtest/gtest.h>
-#include "core/CoreIncludes.h"
-#include "core/class/Identifiable.h"
-
-namespace orxonox
-{
- namespace
- {
- class Interface : virtual public Identifiable
- { public: Interface() { RegisterRootObject(Interface); } };
-
- class BaseClass : virtual public Identifiable
- { public: BaseClass() { RegisterRootObject(BaseClass); } };
-
- class RealClass : public BaseClass, public Interface
- { public: RealClass() { RegisterObject(RealClass); } };
-
- // Fixture
- class IdentifierExternalClassHierarchyTest : public ::testing::Test
- {
- public:
- virtual void SetUp()
- {
- new ClassFactory<Interface>("Interface");
- new ClassFactory<BaseClass>("BaseClass");
- new ClassFactory<RealClass>("RealClass");
- IdentifierManager::createClassHierarchy();
- }
-
- virtual void TearDown()
- {
- IdentifierManager::destroyAllIdentifiers();
- }
- };
- }
-
- TEST(IdentifierExternalClassHierarchyTest_NoFixture, NoInitialization)
- {
- {
- Identifier* identifier = Class(Interface);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- {
- Identifier* identifier = Class(BaseClass);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- {
- Identifier* identifier = Class(RealClass);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- }
-
- TEST_F(IdentifierExternalClassHierarchyTest, TestInterface)
- {
- Identifier* identifier = Class(Interface);
-
- EXPECT_EQ(1u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
-
- TEST_F(IdentifierExternalClassHierarchyTest, BaseClass)
- {
- Identifier* identifier = Class(BaseClass);
-
- EXPECT_EQ(1u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
-
- TEST_F(IdentifierExternalClassHierarchyTest, RealClass)
- {
- Identifier* identifier = Class(RealClass);
-
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(2u, identifier->getParents().size());
- }
-}
+#include <gtest/gtest.h>
+#include "core/CoreIncludes.h"
+#include "core/class/Identifiable.h"
+
+namespace orxonox
+{
+ namespace
+ {
+ class Interface : virtual public Identifiable
+ { public: Interface() { RegisterRootObject(Interface); } };
+
+ class BaseClass : virtual public Identifiable
+ { public: BaseClass() { RegisterRootObject(BaseClass); } };
+
+ class RealClass : public BaseClass, public Interface
+ { public: RealClass() { RegisterObject(RealClass); } };
+
+ // Fixture
+ class IdentifierExternalClassHierarchyTest : public ::testing::Test
+ {
+ public:
+ virtual void SetUp()
+ {
+ registerClass("Interface", new ClassFactoryNoArgs<Interface>());
+ registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>());
+ registerClass("RealClass", new ClassFactoryNoArgs<RealClass>());
+ IdentifierManager::createClassHierarchy();
+ }
+
+ virtual void TearDown()
+ {
+ IdentifierManager::destroyAllIdentifiers();
+ }
+ };
+ }
+
+ TEST(IdentifierExternalClassHierarchyTest_NoFixture, NoInitialization)
+ {
+ {
+ Identifier* identifier = Class(Interface);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ {
+ Identifier* identifier = Class(BaseClass);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ {
+ Identifier* identifier = Class(RealClass);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ }
+
+ TEST_F(IdentifierExternalClassHierarchyTest, TestInterface)
+ {
+ Identifier* identifier = Class(Interface);
+
+ EXPECT_EQ(1u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+
+ TEST_F(IdentifierExternalClassHierarchyTest, BaseClass)
+ {
+ Identifier* identifier = Class(BaseClass);
+
+ EXPECT_EQ(1u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+
+ TEST_F(IdentifierExternalClassHierarchyTest, RealClass)
+ {
+ Identifier* identifier = Class(RealClass);
+
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(2u, identifier->getParents().size());
+ }
+}
Property changes on: code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc
===================================================================
--- code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc 2013-08-11 12:41:37 UTC (rev 9636)
+++ code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc 2013-08-11 14:23:41 UTC (rev 9637)
@@ -1,81 +1,81 @@
-#include <gtest/gtest.h>
-#include "core/CoreIncludes.h"
-#include "core/class/Identifiable.h"
-#include "core/class/OrxonoxClass.h"
-#include "core/class/OrxonoxInterface.h"
-
-namespace orxonox
-{
- namespace
- {
- class Interface : public OrxonoxInterface
- { public: Interface() { RegisterRootObject(Interface); } };
-
- class BaseClass : public OrxonoxClass
- { public: BaseClass() { RegisterRootObject(BaseClass); } };
-
- class RealClass : public BaseClass, public Interface
- { public: RealClass() { RegisterObject(RealClass); } };
-
- // Fixture
- class IdentifierSimpleClassHierarchyTest : public ::testing::Test
- {
- public:
- virtual void SetUp()
- {
- new ClassFactory<Interface>("Interface");
- new ClassFactory<BaseClass>("BaseClass");
- new ClassFactory<RealClass>("RealClass");
- IdentifierManager::createClassHierarchy();
- }
-
- virtual void TearDown()
- {
- IdentifierManager::destroyAllIdentifiers();
- }
- };
- }
-
- TEST(IdentifierSimpleClassHierarchyTest_NoFixture, NoInitialization)
- {
- {
- Identifier* identifier = Class(Interface);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- {
- Identifier* identifier = Class(BaseClass);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- {
- Identifier* identifier = Class(RealClass);
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
- }
-
- TEST_F(IdentifierSimpleClassHierarchyTest, TestInterface)
- {
- Identifier* identifier = Class(Interface);
-
- EXPECT_EQ(1u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
-
- TEST_F(IdentifierSimpleClassHierarchyTest, BaseClass)
- {
- Identifier* identifier = Class(BaseClass);
-
- EXPECT_EQ(1u, identifier->getChildren().size());
- EXPECT_EQ(0u, identifier->getParents().size());
- }
-
- TEST_F(IdentifierSimpleClassHierarchyTest, RealClass)
- {
- Identifier* identifier = Class(RealClass);
-
- EXPECT_EQ(0u, identifier->getChildren().size());
- EXPECT_EQ(2u, identifier->getParents().size());
- }
-}
+#include <gtest/gtest.h>
+#include "core/CoreIncludes.h"
+#include "core/class/Identifiable.h"
+#include "core/class/OrxonoxClass.h"
+#include "core/class/OrxonoxInterface.h"
+
+namespace orxonox
+{
+ namespace
+ {
+ class Interface : public OrxonoxInterface
+ { public: Interface() { RegisterRootObject(Interface); } };
+
+ class BaseClass : public OrxonoxClass
+ { public: BaseClass() { RegisterRootObject(BaseClass); } };
+
+ class RealClass : public BaseClass, public Interface
+ { public: RealClass() { RegisterObject(RealClass); } };
+
+ // Fixture
+ class IdentifierSimpleClassHierarchyTest : public ::testing::Test
+ {
+ public:
+ virtual void SetUp()
+ {
+ registerClass("Interface", new ClassFactoryNoArgs<Interface>());
+ registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>());
+ registerClass("RealClass", new ClassFactoryNoArgs<RealClass>());
+ IdentifierManager::createClassHierarchy();
+ }
+
+ virtual void TearDown()
+ {
+ IdentifierManager::destroyAllIdentifiers();
+ }
+ };
+ }
+
+ TEST(IdentifierSimpleClassHierarchyTest_NoFixture, NoInitialization)
+ {
+ {
+ Identifier* identifier = Class(Interface);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ {
+ Identifier* identifier = Class(BaseClass);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ {
+ Identifier* identifier = Class(RealClass);
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+ }
+
+ TEST_F(IdentifierSimpleClassHierarchyTest, TestInterface)
+ {
+ Identifier* identifier = Class(Interface);
+
+ EXPECT_EQ(1u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+
+ TEST_F(IdentifierSimpleClassHierarchyTest, BaseClass)
+ {
+ Identifier* identifier = Class(BaseClass);
+
+ EXPECT_EQ(1u, identifier->getChildren().size());
+ EXPECT_EQ(0u, identifier->getParents().size());
+ }
+
+ TEST_F(IdentifierSimpleClassHierarchyTest, RealClass)
+ {
+ Identifier* identifier = Class(RealClass);
+
+ EXPECT_EQ(0u, identifier->getChildren().size());
+ EXPECT_EQ(2u, identifier->getParents().size());
+ }
+}
Property changes on: code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: code/branches/core6/test/core/object/ClassFactoryTest.cc
===================================================================
--- code/branches/core6/test/core/object/ClassFactoryTest.cc 2013-08-11 12:41:37 UTC (rev 9636)
+++ code/branches/core6/test/core/object/ClassFactoryTest.cc 2013-08-11 14:23:41 UTC (rev 9637)
@@ -6,7 +6,7 @@
{
TEST(ClassFactoryTest, CanFabricateObject)
{
- Factory* factory = new ClassFactoryWithContext<BaseObject>("BaseObject");
+ Factory* factory = new ClassFactoryWithContext<BaseObject>();
Identifiable* object = factory->fabricate(NULL);
ASSERT_TRUE(object != NULL);
BaseObject* baseObject = dynamic_cast<BaseObject*>(object);
More information about the Orxonox-commit
mailing list