Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 4933 in orxonox.OLD for orxonox/trunk/src/util


Ignore:
Timestamp:
Jul 22, 2005, 6:20:42 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: more elaborate FastFactory, that should work…
now i have to test it… this will generate segfaults for sure :/

Location:
orxonox/trunk/src/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/util/object_manager.cc

    r4932 r4933  
    2929 *  constructor, sets everything to zero and define factoryName
    3030 */
    31 FastFactory::FastFactory (const char* fastFactoryName, ClassID classID)
     31FastFactory::FastFactory (ClassID classID, const char* fastFactoryName)
    3232{
    3333  this->setClassID(CL_FAST_FACTORY, "FastFactory");
     
    3636  this->storedClassID = classID;
    3737  this->next = NULL;
     38
     39  this->storedDeadObjects = 0;
    3840
    3941  FastFactory::registerFastFactory(this);
     
    5658}
    5759
    58 /**
    59  * add a FastFactory to the FastFactory Queue
    60  * @param factory a FastFactory to be registered
    61  */
    62 void FastFactory::registerFastFactory( FastFactory* factory)
     60void FastFactory::registerFastFactory(FastFactory* fastFactory)
    6361{
    64   PRINTF(3)("Registered FastFactory for '%s'\n", factory->getName());
     62  PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
    6563
    6664  if( FastFactory::first == NULL)
    67     FastFactory::first = factory;
     65    FastFactory::first = fastFactory;
    6866  else
    6967  {
    7068    FastFactory* tmpFac = FastFactory::first;
    7169    while( tmpFac->next != NULL)
    72     {
    7370      tmpFac = tmpFac->next;
    74     }
    75     tmpFac->setNext(factory);
     71    tmpFac->setNext(fastFactory);
    7672  }
    7773}
    7874
    7975
     76/**
     77 * searches for a FastFactory
     78 * @param factoryName the Name of the Factory to search for (not used)
     79 * @param classID the ClassID of the FastFactory to search for
     80 * @returns true if found, false otherwise.
     81 */
     82FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName)
     83{
     84  if (FastFactory::first == NULL)
     85    return NULL;
     86   else
     87   {
     88     FastFactory* tmpFac = FastFactory::first;
     89     while (tmpFac != NULL)
     90     {
     91       if (tmpFac->storedClassID == classID)
     92         return tmpFac;
     93       tmpFac = tmpFac->next;
     94     }
     95   }
     96   return NULL;
     97}
    8098
    8199
  • orxonox/trunk/src/util/object_manager.h

    r4932 r4933  
    3030 * Creates a FastFactory to a Loadable FastFactory.
    3131 */
    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)
     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)
    3434
    3535//! A struct, that holds Lists of Objects of a certain type.
    36 typedef struct FastObjectMember
    37 {
    38   BaseObject*          objectPointer;
    39 
    40   FastObjectMember*    next;
     36template <class T> struct FastObjectMember
     37{
     38  T*                      objectPointer;
     39
     40  FastObjectMember<T>*    next;
    4141};
    4242
    4343//! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
     44/**
     45 * FastFactory is needed to glue all the tFastFactory<T>'s together.
     46 * Furthermore one can retrieve the corresponding tFastFactory over
     47 * tFastFactory<T>* = FastFacroty::getFastFactory(ID);
     48 */
    4449class FastFactory : public BaseObject {
    4550
    4651  public:
    47     FastFactory (const char* fastFactoryName, ClassID classID);
    4852    virtual ~FastFactory ();
    4953
    50 
     54    // functions to push and pop elements of this class
    5155    BaseObject* resurect(ClassID classID);
    5256    void kill(ClassID classID, BaseObject* object);
    5357
    54     virtual BaseObject* fabricate(ClassID classID) = NULL;
    55 
    56     static void registerFastFactory(FastFactory* fastFactory);
    57     static void registerFastFactory(const char* fastFactoryName, ClassID classID);
     58    // retrival functions for fast Ineraction
     59    //FastFactory* getFastFactory(ClassID classID);
    5860
    5961    /** @returns the first FastFactory */
     
    6668    FastFactory* getNext() const { return this->next; };
    6769
     70    virtual BaseObject* fabricate(ClassID classID) = NULL;
     71
     72  private:
     73    FastFactory (ClassID classID, const char* fastFactoryName = NULL);
     74    static void registerFastFactory(FastFactory* fastFactory);
     75    static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
     76
    6877  protected:
    6978    ClassID               storedClassID;        //!< The classID of the specified class.
     79    unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
    7080
    7181  private:
     
    8292{
    8393  public:
    84     tFastFactory(const char* fastFactoryName, ClassID fastFactory);
    85 
    86   private:
    87     virtual BaseObject* fabricate(ClassID classID);
     94    static FastFactory* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
     95
     96    void prepare(unsigned int count);
     97
     98    T* resurect();
     99    void kill(T* object);
     100
     101  private:
     102    tFastFactory(ClassID classID, const char* fastFactoryName);
     103
     104    T* fabricate();
     105
     106  private:
     107    FastObjectMember<T>*     deadList;             //!< A List of all stored dead Objects of this class.
     108    FastObjectMember<T>*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
     109
    88110};
    89111
     
    94116 */
    95117template<class T>
    96     tFastFactory<T>::tFastFactory(const char* fastFactoryName, ClassID fastFactory) : FastFactory(fastFactoryName, fastFactory)
     118    tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
     119  : FastFactory(classID, fastFactoryName)
    97120{
    98121  PRINTF(5)("Class: %s loadable as a FastFactory\n", this->getName());
    99 }
    100 
    101 template<class T>
    102 BaseObject* 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);
     122
     123  this->deadList = NULL;
     124  this->unusedContainers = NULL;
     125}
     126
     127template<class T>
     128    FastFactory* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
     129{
     130  tFastFactory<T>* tmpFac = NULL;
     131  if (FastFactory::getFirst() != NULL)
     132    tmpFac = FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName);
     133
     134  if (tmpFac != NULL)
     135    return tmpFac;
    108136  else
    109     return NULL;
    110 }
    111 
    112 
    113 
     137    return new tFastFactory<T>;
     138}
     139
     140
     141template<class T>
     142    T* tFastFactory<T>::fabricate()
     143{
     144  FastObjectMember<T>* tmpFirstDead = this->deadList;
     145  tmpFirstDead->objectPointer = new T();
     146  tmpFirstDead->next = this->deadList;
     147  ++this->storedDeadObjects;
     148
     149  this->deadList = tmpFirstDead;
     150  return this->deadList;
     151}
     152
     153template<class T>
     154    void tFastFactory<T>::prepare(unsigned int count)
     155{
     156  if (this->storedDeadObjects + this->storedLivingObjects >= count)
     157  {
     158    PRINTF(3)("not creating new Objects for class %s, because the requested count already exists\n", this->getClassName());
     159  }
     160  for (int i = this->storedDeadObjects + this->storedLivingObjects; i < count; i++)
     161  {
     162    this->fabricate();
     163  }
     164}
     165
     166template<class T>
     167    T* tFastFactory<T>::resurect()
     168{
     169  if (unlikely(this->deadList == NULL))
     170  {
     171    PRINTF(2)("The deadList of Class %s is empty, this may be either because it has not been filled yet, or the cache is to small.\n" \
     172        "Fabricating a new %s", this->getName(), this->getName());
     173    return this->fabricate();
     174  }
     175  else
     176  {
     177    FastObjectMember<T>* tmpC = deadList;
     178    this->deadList = this->deadList->next;
     179
     180    tmpC->next = this->unusedContainers;
     181    this->unusedContainers->tmpC;
     182
     183    return tmpC;
     184  }
     185}
     186
     187template<class T>
     188    void tFastFactory<T>::kill(T* object)
     189{
     190  FastObjectMember<T>* tmpC;
     191  if (unlikely(this->unusedContainers == NULL))
     192  {
     193    tmpC = new FastObjectMember<T>;
     194  }
     195  else
     196  {
     197    tmpC = this->unusedContainers;
     198    this->unusedContainers = this->unusedContainers->next;
     199  }
     200
     201  tmpC->next = this->deadList;
     202  tmpC->objectPointer = object;
     203}
    114204
    115205////////////////////
Note: See TracChangeset for help on using the changeset viewer.