Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5982 in orxonox.OLD for trunk/src/util/loading/factory.h


Ignore:
Timestamp:
Dec 8, 2005, 12:22:53 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: Factory-Redesign on the wish of manuel

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/util/loading/factory.h

    r5944 r5982  
    2828#include "base_object.h"
    2929#include "debug.h"
     30#include <vector>
    3031
    3132/**
     
    4041
    4142 public:
    42   Factory (const char* factoryName = NULL, ClassID classID = CL_NULL);
     43  Factory (const char* factoryName, ClassID classID);
    4344  virtual ~Factory ();
    4445
    45   void fabricate(const char* className, const char* entityName);
    46   virtual BaseObject* fabricate(ClassID classID) = NULL;
    47   virtual BaseObject* fabricate(const TiXmlElement* root) = NULL;
    48   virtual BaseObject* fabricateDirect() = NULL;
     46  static void deleteFactories();
    4947
    50   static void registerFactory( Factory* factory);
    51   /** @returns the first factory */
    52   static Factory* getFirst() { return Factory::first; };
     48  static  BaseObject* fabricate(const char* className);
     49  static  BaseObject* fabricate(ClassID classID);
     50  static  BaseObject* fabricate(const TiXmlElement* root = NULL);
     51
     52  bool operator==(ClassID classID) const { return (this->classID == classID); };
     53  bool operator==(const char* className) const;
    5354
    5455  protected:
    55     /** sets the Next factory in the list @param nextFactory the next factory */
    56     inline void setNext( Factory* nextFactory) { this->next = nextFactory; };
    57     /** @returns the next factory */
    58     Factory* getNext() const { return this->next; };
    59 
     56    virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
    6057
    6158  protected:
    62     ClassID           classID;              //!< The CLass-Identifyer of the Factory.
    63 
    64   private:
    65     Factory*          next;                 //!< pointer to the next factory.
    66     static Factory*   first;                //!< A pointer to the first factory.
     59    ClassID                       classID;              //!< The Class-Identifyer of the Factory.
     60    const char*                   className;            //!< The name of the Class.
     61    static std::list<Factory*>*   factoryList;          //!< List of Registered Factories
    6762};
    6863
     
    7368template<class T> class tFactory : public Factory
    7469{
    75   public:
    76     tFactory(const char* factoryName, ClassID classID);
    77     virtual ~tFactory();
     70 public:
     71  tFactory (const char* factoryName, ClassID classID)
     72   : Factory(factoryName, classID)
     73  {
     74  }
    7875
    7976  private:
    80     virtual BaseObject* fabricate(ClassID classID);
    81     virtual BaseObject* fabricate(const TiXmlElement* root);
    82     virtual BaseObject* fabricateDirect();
     77   /**
     78    * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
     79    * @param root the TiXmlElement T should load parameters from.
     80    * @return the newly fabricated T.
     81    */
     82    virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const
     83    {
     84      return new T(root);
     85    }
    8386};
    84 
    85 /**
    86  *  construnts a factory with
    87  * @param factoryName the name of the factory
    88 */
    89 template<class T>
    90     tFactory<T>::tFactory(const char* factoryName, ClassID classID) : Factory(factoryName, classID)
    91 {
    92   PRINTF(4)("Class: %s loadable\n", this->getName());
    93 }
    94 
    95 /**
    96  * destructs the type-Factory
    97  */
    98 template<class T>
    99     tFactory<T>::~tFactory()
    100 {}
    101 
    102 /**
    103  * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
    104  * @param root the TiXmlElement T should load parameters from.
    105  * @return the newly fabricated T, NULL otherwise.
    106  */
    107 template<class T>
    108     BaseObject* tFactory<T>::fabricate(const TiXmlElement* root)
    109 {
    110   if (root == NULL)
    111     return NULL;
    112 
    113   if(!strcmp(root->Value(), this->getName()))
    114     return new T ( root);
    115   else if( getNext() != NULL)
    116     return getNext()->fabricate( root);
    117   else
    118     return NULL;
    119 }
    120 
    121 
    122 /**
    123  * fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
    124  * @param classID the ClassID of T that should be created.
    125  * @return the newly fabricated T if fabricated NULL otherwise.
    126  */
    127 template<class T>
    128     BaseObject* tFactory<T>::fabricate(ClassID classID)
    129 {
    130   if(classID == this->classID)
    131     return this->fabricateDirect();
    132   else if( getNext() != NULL)
    133     return getNext()->fabricate( classID);
    134   else
    135     return NULL;
    136 }
    137 
    138 /**
    139  * directly fabricate an Entity of this factory.
    140  */
    141 template<class T>
    142     BaseObject* tFactory<T>::fabricateDirect()
    143 {
    144   return new T((const TiXmlElement*)NULL);
    145 }
    14687
    14788#endif /* _FACTORY_H */
Note: See TracChangeset for help on using the changeset viewer.