Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 5, 2009, 9:22:22 PM (14 years ago)
Author:
rgrieder
Message:

Synchronised sandbox with current code trunk. There should be a few bug fixes.

Location:
sandbox
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sandbox

  • sandbox/src/libraries/core/ClassFactory.h

    r5782 r6038  
    4242
    4343#include "util/Debug.h"
    44 #include "Factory.h"
    4544#include "Identifier.h"
    4645
    4746namespace orxonox
    4847{
     48    // ###########################
     49    // ###       Factory       ###
     50    // ###########################
     51    //! Base-class of ClassFactory.
     52    class _CoreExport Factory
     53    {
     54        public:
     55            virtual ~Factory() {};
     56            virtual BaseObject* fabricate(BaseObject* creator) = 0;
     57    };
     58
    4959    // ###############################
    5060    // ###      ClassFactory       ###
     
    5262    //! The ClassFactory is able to create new objects of a specific class.
    5363    template <class T>
    54     class ClassFactory : public BaseFactory
     64    class ClassFactory : public Factory
    5565    {
    5666        public:
    57             static bool create(const std::string& name);
    58             BaseObject* fabricate(BaseObject* creator);
     67            /**
     68                @brief Constructor: Adds the ClassFactory to the Identifier of the same type.
     69                @param name The name of the class
     70                @param bLoadable True if the class can be loaded through XML
     71            */
     72            ClassFactory(const std::string& name, bool bLoadable = true)
     73            {
     74                COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl;
     75                ClassIdentifier<T>::getIdentifier(name)->addFactory(this);
     76            }
    5977
    60         private:
    61             ClassFactory() {}                               // Don't create
    62             ClassFactory(const ClassFactory& factory) {}    // Don't copy
    63             virtual ~ClassFactory() {}                      // Don't delete
    64 
    65             static T* createNewObject(BaseObject* creator);
     78            /**
     79                @brief Creates and returns a new object of class T.
     80                @return The new object
     81            */
     82            inline BaseObject* fabricate(BaseObject* creator)
     83            {
     84                return static_cast<BaseObject*>(new T(creator));
     85            }
    6686    };
    67 
    68     /**
    69         @brief Adds the ClassFactory to the Identifier of the same type and the Identifier to the Factory.
    70         @param name The name of the class
    71         @param bLoadable True if the class can be loaded through XML
    72         @return Always true (this is needed because the compiler only allows assignments before main())
    73     */
    74     template <class T>
    75     bool ClassFactory<T>::create(const std::string& name)
    76     {
    77         COUT(4) << "*** ClassFactory: Create entry for " << name << " in Factory." << std::endl;
    78         ClassIdentifier<T>::getIdentifier(name)->addFactory(new ClassFactory<T>);
    79         Factory::add(name, ClassIdentifier<T>::getIdentifier());
    80 
    81         return true;
    82     }
    83 
    84     /**
    85         @brief Creates and returns a new object of class T.
    86         @return The new object
    87     */
    88     template <class T>
    89     inline BaseObject* ClassFactory<T>::fabricate(BaseObject* creator)
    90     {
    91         return ClassFactory<T>::createNewObject(creator);
    92     }
    93 
    94     /**
    95         @brief Creates and returns a new object of class T; this is a wrapper for the new operator.
    96         @return The new object
    97     */
    98     template <class T>
    99     inline T* ClassFactory<T>::createNewObject(BaseObject* creator)
    100     {
    101         return new T(creator);
    102     }
    10387}
    10488
Note: See TracChangeset for help on using the changeset viewer.