Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9637 for code/branches/core6


Ignore:
Timestamp:
Aug 11, 2013, 4:23:41 PM (11 years ago)
Author:
landauf
Message:

removed constructor arguments from ClassFactories. use the helper-function registerClass() to add the factory to the corresponding identifier

Location:
code/branches/core6
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core6/src/libraries/core/CoreIncludes.h

    r9635 r9637  
    117117*/
    118118#define CreateFactory(ClassName) \
    119     RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, true))
     119    RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), true)
    120120
    121121/**
     
    124124*/
    125125#define CreateUnloadableFactory(ClassName) \
    126     RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, false))
     126    RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(), false)
    127127
    128128/**
     
    130130    @param ClassName The name of the class
    131131*/
    132 #define RegisterFactory(ClassName, FactoryInstance) \
    133     Factory* _##ClassName##Factory = FactoryInstance
     132#define RegisterFactory(ClassName, FactoryInstance, bLoadable) \
     133    Identifier* _##ClassName##Identifier = orxonox::registerClass<ClassName>(#ClassName, FactoryInstance, bLoadable)
    134134
    135135/**
     
    143143namespace orxonox
    144144{
     145    /**
     146     * @brief Overload of registerClass() which determines T implicitly by the template argument of the ClassFactory.
     147     */
     148    template <class T>
     149    inline Identifier* registerClass(const std::string& name, ClassFactory<T>* factory, bool bLoadable = true)
     150    {
     151        return registerClass<T>(name, static_cast<Factory*>(factory), bLoadable);
     152    }
     153
     154    /**
     155     * @brief Registers a class in the framework.
     156     * @param name The name of the class
     157     * @param factory The factory which is able to create new instances of this class
     158     * @param bLoadable Whether the class is allowed to be loaded through XML
     159     */
     160    template <class T>
     161    inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
     162    {
     163        orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
     164        Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
     165        identifier->setFactory(factory);
     166        identifier->setLoadable(bLoadable);
     167        return identifier;
     168    }
     169
    145170    /**
    146171        @brief Returns the Identifier with a given name.
  • code/branches/core6/src/libraries/core/object/ClassFactory.h

    r9635 r9637  
    4343
    4444#include "util/Output.h"
    45 #include "core/class/Identifier.h"
    4645
    4746namespace orxonox
     
    5453    {
    5554        public:
    56             virtual ~Factory() {};
     55            virtual ~Factory() {}
    5756            virtual Identifiable* fabricate(Context* context) = 0;
    5857    };
     
    6160    // ###      ClassFactory       ###
    6261    // ###############################
    63     /// The ClassFactory is able to create new objects of a specific class that require no constructor arguments.
     62    /// The ClassFactory is the base-class of all class-spezific factories
    6463    template <class T>
    6564    class ClassFactory : public Factory
    6665    {
     66    };
     67
     68    // ###############################
     69    // ###   ClassFactoryNoArgs    ###
     70    // ###############################
     71    /// The ClassFactoryNoArgs is able to create new objects of a specific class that require no constructor arguments.
     72    template <class T>
     73    class ClassFactoryNoArgs : public ClassFactory<T>
     74    {
    6775        public:
    68             /**
    69                 @brief Constructor: Adds the ClassFactory to the Identifier of the same type.
    70                 @param name The name of the class
    71                 @param bLoadable True if the class can be loaded through XML
    72             */
    73             ClassFactory(const std::string& name, bool bLoadable = true)
    74             {
    75                 orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
    76                 ClassIdentifier<T>::getIdentifier(name)->setFactory(this);
    77                 ClassIdentifier<T>::getIdentifier()->setLoadable(bLoadable);
    78             }
    79 
    8076            /**
    8177                @brief Creates and returns a new object of class T.
     
    9389    /// The ClassFactoryWithContext is able to create new objects of a specific class that require a context as constructor argument
    9490    template <class T>
    95     class ClassFactoryWithContext : public Factory
     91    class ClassFactoryWithContext : public ClassFactory<T>
    9692    {
    9793        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 
    11094            /**
    11195                @brief Creates and returns a new object of class T.
  • code/branches/core6/test/core/class/IdentifierExternalClassHierarchyTest.cc

    • Property svn:eol-style set to native
    r9636 r9637  
    2222                virtual void SetUp()
    2323                {
    24                     new ClassFactory<Interface>("Interface");
    25                     new ClassFactory<BaseClass>("BaseClass");
    26                     new ClassFactory<RealClass>("RealClass");
     24                    registerClass("Interface", new ClassFactoryNoArgs<Interface>());
     25                    registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>());
     26                    registerClass("RealClass", new ClassFactoryNoArgs<RealClass>());
    2727                    IdentifierManager::createClassHierarchy();
    2828                }
  • code/branches/core6/test/core/class/IdentifierSimpleClassHierarchyTest.cc

    • Property svn:eol-style set to native
    r9636 r9637  
    2424                virtual void SetUp()
    2525                {
    26                     new ClassFactory<Interface>("Interface");
    27                     new ClassFactory<BaseClass>("BaseClass");
    28                     new ClassFactory<RealClass>("RealClass");
     26                    registerClass("Interface", new ClassFactoryNoArgs<Interface>());
     27                    registerClass("BaseClass", new ClassFactoryNoArgs<BaseClass>());
     28                    registerClass("RealClass", new ClassFactoryNoArgs<RealClass>());
    2929                    IdentifierManager::createClassHierarchy();
    3030                }
  • code/branches/core6/test/core/object/ClassFactoryTest.cc

    r9635 r9637  
    77    TEST(ClassFactoryTest, CanFabricateObject)
    88    {
    9         Factory* factory = new ClassFactoryWithContext<BaseObject>("BaseObject");
     9        Factory* factory = new ClassFactoryWithContext<BaseObject>();
    1010        Identifiable* object = factory->fabricate(NULL);
    1111        ASSERT_TRUE(object != NULL);
Note: See TracChangeset for help on using the changeset viewer.