Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9635


Ignore:
Timestamp:
Aug 11, 2013, 2:38:03 PM (11 years ago)
Author:
landauf
Message:

added Factory for classes that don't require a Context as constructor argument

Location:
code/branches/core6
Files:
4 edited

Legend:

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

    r9567 r9635  
    113113
    114114/**
    115     @brief Creates the Factory.
     115    @brief Creates and registers the Factory.
    116116    @param ClassName The name of the class
    117117*/
    118118#define CreateFactory(ClassName) \
    119     Factory* _##ClassName##Factory = new orxonox::ClassFactory<ClassName>(#ClassName, true)
     119    RegisterFactory(ClassName, new orxonox::ClassFactoryWithContext<ClassName>(#ClassName, true))
    120120
    121121/**
    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.
    123123    @param ClassName The name of the class
    124124*/
    125125#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
    127134
    128135/**
  • code/branches/core6/src/libraries/core/CorePrereqs.h

    r9597 r9635  
    126126    template <class T>
    127127    class ClassFactory;
     128    template <class T>
     129    class ClassFactoryWithContext;
    128130    template <class T>
    129131    class ClassIdentifier;
  • code/branches/core6/src/libraries/core/object/ClassFactory.h

    r9633 r9635  
    5050    // ###       Factory       ###
    5151    // ###########################
    52     /// Base-class of ClassFactory.
     52    /// Base-class of all factories.
    5353    class _CoreExport Factory
    5454    {
     
    6161    // ###      ClassFactory       ###
    6262    // ###############################
    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.
    6464    template <class T>
    6565    class ClassFactory : public Factory
     
    8484            inline Identifiable* fabricate(Context* context)
    8585            {
     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            {
    86116                return static_cast<Identifiable*>(new T(context));
    87117            }
  • code/branches/core6/test/core/object/ClassFactoryTest.cc

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