Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4932 in orxonox.OLD for orxonox/trunk/src


Ignore:
Timestamp:
Jul 22, 2005, 12:08:51 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: factory optimisations

Location:
orxonox/trunk/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/defs/class_id.h

    r4839 r4932  
    188188  CL_TRACK_ELEMENT              =    0x00000b0b,
    189189  CL_NUMBER                     =    0x00000b0c,
     190  CL_FAST_FACTORY               =    0x00000c01,
    190191
    191192
  • orxonox/trunk/src/defs/debug.h

    r4885 r4932  
    6666#define DEBUG_MODULE_TRACK_MANAGER      2
    6767#define DEBUG_MODULE_GARBAGE_COLLECTOR  0
    68 #define DEBUG_MODULE_OBJECT_MANAGER     0
     68#define DEBUG_MODULE_OBJECT_MANAGER     2
    6969#define DEBUG_MODULE_LIGHT              0
    7070#define DEBUG_MODULE_PLAYER             1
  • orxonox/trunk/src/util/loading/factory.h

    r4885 r4932  
    4848
    4949  static void registerFactory( Factory* factory);
    50   /** sets the Next factory in the list @param nextFactory the next factory */
    51   inline void setNext( Factory* nextFactory) { this->next = nextFactory; };
    5250  /** @returns the first factory */
    5351  static Factory* getFirst() { return Factory::first; };
    54   /** @returns the next factory */
    55   Factory* getNext() const { return this->next; };
     52
     53  protected:
     54    /** sets the Next factory in the list @param nextFactory the next factory */
     55    inline void setNext( Factory* nextFactory) { this->next = nextFactory; };
     56    /** @returns the next factory */
     57    Factory* getNext() const { return this->next; };
    5658
    5759  private:
  • orxonox/trunk/src/util/object_manager.cc

    r4931 r4932  
    2424
    2525
     26
     27
     28/**
     29 *  constructor, sets everything to zero and define factoryName
     30 */
     31FastFactory::FastFactory (const char* fastFactoryName, ClassID classID)
     32{
     33  this->setClassID(CL_FAST_FACTORY, "FastFactory");
     34  this->setName(fastFactoryName);
     35
     36  this->storedClassID = classID;
     37  this->next = NULL;
     38
     39  FastFactory::registerFastFactory(this);
     40}
     41
     42/** a reference to the First FastFactory */
     43FastFactory* FastFactory::first = NULL;
     44
     45/**
     46 *  destructor
     47 *  clear the Q
     48 */
     49FastFactory::~FastFactory ()
     50{
     51  if (this == first)
     52    this->first = NULL;
     53
     54  if (this->next)
     55    delete this->next;
     56}
     57
     58/**
     59 * add a FastFactory to the FastFactory Queue
     60 * @param factory a FastFactory to be registered
     61 */
     62void FastFactory::registerFastFactory( FastFactory* factory)
     63{
     64  PRINTF(3)("Registered FastFactory for '%s'\n", factory->getName());
     65
     66  if( FastFactory::first == NULL)
     67    FastFactory::first = factory;
     68  else
     69  {
     70    FastFactory* tmpFac = FastFactory::first;
     71    while( tmpFac->next != NULL)
     72    {
     73      tmpFac = tmpFac->next;
     74    }
     75    tmpFac->setNext(factory);
     76  }
     77}
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
    2691/**
    2792 *  standard constructor
     
    3297  this->setName("ObjectManager");
    3398
    34   this->managedObjectList = new tList<BaseObject>*[CL_NUMBER];
    35   for(int i = 0; i < CL_NUMBER; ++i)
    36     {
    37       this->managedObjectList[i] = NULL;
    38     }
    3999}
    40100
     
    54114
    55115/**
    56  *  adds an element to the list of dead objects
    57  * @param index: The type of object to add
    58  * @param object: pointer to the object at hand
    59 */
    60 void ObjectManager::addToDeadList(int index, BaseObject* object)
    61 {
    62   if( likely(this->managedObjectList[index] != NULL))
    63     this->managedObjectList[index]->add(object);
    64   else
    65     PRINTF(0)(" Critical: unable to add object to the list nr. %i: no list initialized - ignoring\n", index);
    66 }
    67 
    68 /**
    69  *  resurects an object
    70  * @param index: the type of resource to load
    71  * @param number: how many of them
    72 
    73    @todo if it is unable to get an object from the deadList, it should create it
    74 */
    75 BaseObject* ObjectManager::getFromDeadList(int index, int number)
    76 {
    77   if( likely(this->managedObjectList[index] != NULL))
    78     {
    79       BaseObject* obj = this->managedObjectList[index]->firstElement();
    80       this->managedObjectList[index]->remove(obj);
    81       if( unlikely(obj == NULL))
    82         {
    83           PRINTF(0)("Critical: there was no object anymore in the dead list! This could result in Segfaults\n");
    84         }
    85       return obj;
    86     }
    87   else
    88     PRINTF(0)(" Critical: unable to get object from the list nr. %i: no elements initialized - ignoring\n", index);
    89   return NULL;
    90 }
    91 
    92 /**
    93116 *  outputs some simple debug information about the ObjectManage
    94117*/
     
    96119{
    97120  PRINT(0)("\n==========================| ObjectManager::debug() |===\n");
    98   PRINT(0)("=  Number of registerable classes: %i\n", CL_NUMBER );
    99   PRINT(0)("=  Currently cached objects: \n");
    100   for(int i = 0; i < CL_NUMBER; ++i)
    101     {
     121/* PRINT(0)("=  Number of registerable classes: %i\n", CL_NUMBER );
     122 PRINT(0)("=  Currently cached objects: \n");
     123 for(int i = 0; i < CL_NUMBER; ++i)
     124   {
    102125      if( this->managedObjectList[i] != NULL)
    103126        PRINT(0)("=   o Class Nr. %i has cached %i object(s)\n", i, this->managedObjectList[i]->getSize());
    104127      else
    105128        PRINT(0)("=   o Class Nr. %i has cached 0 object(s)\n", i);
    106     }
     129    }*/
    107130  PRINT(0)("=======================================================\n");
    108131}
    109132
    110133
    111 
    112 /**
    113  *  constructor
    114 
    115    set everything to zero and define factoryName
    116  */
    117 FastObject::FastObject (const char* fastObjectName, ClassID classID)
    118 {
    119   this->setClassID(CL_FACTORY, "FastObject");
    120   this->setName(fastObjectName);
    121 
    122   this->storedClassID = classID;
    123   this->next = NULL;
    124 
    125   FastObject::registerFastObject(this);
    126 }
    127 
    128 /** a reference to the First FastObject */
    129 FastObject* FastObject::first = NULL;
    130 
    131 /**
    132  *  destructor
    133 
    134    clear the Q
    135  */
    136 FastObject::~FastObject ()
    137 {
    138   //  printf("%s\n", this->factoryName);
    139   //  FastObject* tmpDel = this->next;
    140   //  this->next = NULL;
    141   if (this->next)
    142     delete this->next;
    143 }
    144 
    145 /**
    146  *  add a FastObject to the FastObject Queue
    147  * @param factory a FastObject to be registered
    148  */
    149 void FastObject::registerFastObject( FastObject* factory)
    150 {
    151   PRINTF(4)("Registered FastObject for '%s'\n", factory->getName());
    152 
    153   if( FastObject::first == NULL)
    154     FastObject::first = factory;
    155   else
    156   {
    157     FastObject* tmpFac = FastObject::first;
    158     while( tmpFac->next != NULL)
    159     {
    160       tmpFac = tmpFac->next;
    161     }
    162     tmpFac->setNext(factory);
    163   }
    164 }
  • orxonox/trunk/src/util/object_manager.h

    r4931 r4932  
    2626class GarbageCollector;
    2727
     28
     29/**
     30 * Creates a FastFactory to a Loadable FastFactory.
     31 */
     32#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \
     33    tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
     34
     35//! A struct, that holds Lists of Objects of a certain type.
     36typedef struct FastObjectMember
     37{
     38  BaseObject*          objectPointer;
     39
     40  FastObjectMember*    next;
     41};
     42
     43//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
     44class FastFactory : public BaseObject {
     45
     46  public:
     47    FastFactory (const char* fastFactoryName, ClassID classID);
     48    virtual ~FastFactory ();
     49
     50
     51    BaseObject* resurect(ClassID classID);
     52    void kill(ClassID classID, BaseObject* object);
     53
     54    virtual BaseObject* fabricate(ClassID classID) = NULL;
     55
     56    static void registerFastFactory(FastFactory* fastFactory);
     57    static void registerFastFactory(const char* fastFactoryName, ClassID classID);
     58
     59    /** @returns the first FastFactory */
     60    static FastFactory* getFirst() { return FastFactory::first; };
     61
     62  protected:
     63    /** sets the Next factory in the list @param nextFactory the next factory */
     64    inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
     65    /** @returns the next FastFactory */
     66    FastFactory* getNext() const { return this->next; };
     67
     68  protected:
     69    ClassID               storedClassID;        //!< The classID of the specified class.
     70
     71  private:
     72    static FastFactory*   first;                //!< A pointer to the first FastFactory.
     73
     74    FastFactory*          next;                 //!< pointer to the next FastFactory.
     75};
     76
     77/**
     78 *  a FastFactory that is able to load any kind of Object from a ClassID
     79 * (this is a Functor)
     80 */
     81template<class T> class tFastFactory : public FastFactory
     82{
     83  public:
     84    tFastFactory(const char* fastFactoryName, ClassID fastFactory);
     85
     86  private:
     87    virtual BaseObject* fabricate(ClassID classID);
     88};
     89
     90/**
     91 * construnts a FastFactory with
     92 * @param fastFactoryName the name of the FastFactory
     93 * @param fastFactory the ID of the class
     94 */
     95template<class T>
     96    tFastFactory<T>::tFastFactory(const char* fastFactoryName, ClassID fastFactory) : FastFactory(fastFactoryName, fastFactory)
     97{
     98  PRINTF(5)("Class: %s loadable as a FastFactory\n", this->getName());
     99}
     100
     101template<class T>
     102BaseObject* tFastFactory<T>::fabricate(ClassID classID)
     103{
     104  if (this->storedClassID == classID)
     105    return new T ();
     106  else if( getNext() != NULL)
     107    return getNext()->fabricate(classID);
     108  else
     109    return NULL;
     110}
     111
     112
     113
     114
     115////////////////////
     116// OBJECT MANAGER //
     117////////////////////
     118
    28119//! the object manager itself
    29120class ObjectManager : public BaseObject {
     
    35126
    36127  void registerClass(ClassID classID);
    37 
    38   /** a class handled by the objectManage */
    39   void addToDeadList(int index, BaseObject* object);
    40   BaseObject* getFromDeadList(int index, int number = 1);
    41128
    42129  BaseObject* resurect();
     
    52139  static ObjectManager*      singletonRef;          //!< The singleton reference to the only reference of this class
    53140
    54   tList<BaseObject>**        managedObjectList;     //!< A list of managed objects (handles different types and lists of them)
    55141};
    56142
    57143
    58144
    59 /**
    60  * Creates a factory to a Loadable FastObject.
    61  */
    62 #define CREATE_FAST_OBJECT(CLASS_NAME, CLASS_ID) \
    63     tFastObject<CLASS_NAME>* global_##CLASS_NAME##_FastObject = new tFastObject<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
    64 
    65 //! The FastObject is a loadable object handler
    66 class FastObject : public BaseObject {
    67 
    68   public:
    69     FastObject (const char* fastObjectName = NULL, ClassID classID = CL_NULL);
    70     virtual ~FastObject ();
    71 
    72     virtual BaseObject* fabricate(ClassID classID) = NULL;
    73 
    74     static void registerFastObject(FastObject* fastObject);
    75     /** sets the Next factory in the list @param nextFactory the next factory */
    76     inline void setNext( FastObject* nextFastObject) { this->next = nextFastObject; };
    77     /** @returns the first FastObject */
    78     static FastObject* getFirst() { return FastObject::first; };
    79     /** @returns the next FastObject */
    80     FastObject* getNext() const { return this->next; };
    81 
    82   protected:
    83     ClassID              storedClassID;        //!< The classID of the specified class.
    84 
    85   private:
    86     FastObject*          next;                 //!< pointer to the next FastObject.
    87     static FastObject*   first;                //!< A pointer to the first FastObject.
    88 };
    89 
    90 /**
    91  *  a FastObject that is able to load any kind of Object
    92  * (this is a Functor)
    93  */
    94 template<class T> class tFastObject : public FastObject
    95 {
    96   public:
    97     tFastObject(const char* fastObjectName, ClassID fastObject);
    98 
    99   private:
    100     virtual BaseObject* fabricate(ClassID fastObject);
    101 };
    102 
    103 /**
    104  * construnts a FastObject with
    105  * @param fastObjectName the name of the FastObject
    106  * @param fastObject the ID of the class
    107  */
    108 template<class T>
    109 tFastObject<T>::tFastObject(const char* fastObjectName, ClassID fastObject) : FastObject(fastObjectName, fastObject)
    110 {
    111   PRINTF(5)("Class: %s loadable as a FastObject\n", this->getName());
    112 }
    113 
    114 template<class T>
    115 BaseObject* tFastObject<T>::fabricate(ClassID fastObject)
    116 {
    117 //  if(this->classID == fastObject)
    118     //return new T ();
    119   //else if( getNext() != NULL)
    120 //    return getNext()->fabricate();
    121 //  else
    122     return NULL;
    123 }
    124 
    125145#endif /* _OBJECT_MANAGER_H */
  • orxonox/trunk/src/world_entities/weapons/projectile.cc

    r4927 r4932  
    3131 *  standard constructor
    3232*/
    33 Projectile::Projectile (Weapon* weapon) : WorldEntity()
     33Projectile::Projectile () : WorldEntity()
    3434{
    3535  this->setClassID(CL_PROJECTILE, "Projectile");
    3636
    37   this->weapon = weapon;
    3837  this->lifeCycle = 0.0;
    3938  this->lifeSpan = 0.75f; /* sec */
  • orxonox/trunk/src/world_entities/weapons/projectile.h

    r4927 r4932  
    2525
    2626class Vector;
    27 class Weapon;
    2827class ParticleEmitter;
    2928
     
    3130{
    3231  public:
    33     Projectile (Weapon* weapon);
     32    Projectile ();
    3433    virtual ~Projectile ();
    3534
     
    5756
    5857    Vector                flightDirection;           //!< direction in which the shoot flighs
    59     Weapon*               weapon;                    //!< weapon the shoot belongs to.
    6058
    6159    Vector                velocity;                  //!< velocity of the projectile.
  • orxonox/trunk/src/world_entities/weapons/test_bullet.cc

    r4892 r4932  
    2121#include "model.h"
    2222#include "vector.h"
     23#include "object_manager.h"
    2324
    2425using namespace std;
    2526
     27CREATE_FAST_FACTORY(TestBullet, CL_TEST_BULLET);
    2628
    2729/**
    2830 *  standard constructor
    2931*/
    30 TestBullet::TestBullet (Weapon* weapon) : Projectile(weapon)
     32TestBullet::TestBullet () : Projectile()
    3133{
    3234  this->setClassID(CL_TEST_BULLET, "TestBullet");
  • orxonox/trunk/src/world_entities/weapons/test_bullet.h

    r4892 r4932  
    1515{
    1616 public:
    17   TestBullet (Weapon* weapon);
     17  TestBullet ();
    1818  virtual ~TestBullet ();
    1919
  • orxonox/trunk/src/world_entities/weapons/test_gun.cc

    r4931 r4932  
    3535#include "object_manager.h"
    3636
    37 CREATE_FAST_OBJECT(TestGun, CL_TEST_GUN);
    3837
    3938using namespace std;
     
    9796  this->weaponSource = new SoundSource(this->fireSound, this);
    9897  this->weaponSource->setRolloffFactor(.1);*/
    99   Projectile* p = new TestBullet(this);
     98  Projectile* p = new TestBullet();
    10099
    101100  //  ObjectManager::getInstance()->cache(CL_TEST_BULLET, 100, p);
     
    156155void TestGun::fire()
    157156{
    158   Projectile* pj =  new TestBullet(this);//dynamic_cast<Projectile*>(ObjectManager::getInstance()->getFromDeadList(CL_TEST_BULLET & CL_MASK_LOWLEVEL_CLASS));
     157  Projectile* pj =  new TestBullet();//dynamic_cast<Projectile*>(ObjectManager::getInstance()->getFromDeadList(CL_TEST_BULLET & CL_MASK_LOWLEVEL_CLASS));
    159158//  weaponSource->play();
    160159
Note: See TracChangeset for help on using the changeset viewer.