Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Jul 23, 2005, 12:24:04 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: moved the fast_factory to a file of its own.
This is because, the use is quite different from objectManager, and maybe we want to delete the ObjectManager totally from the Project
@patrick: can you please tell me what dou stink??

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

Legend:

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

    r4939 r4940  
    99   any later version.
    1010
    11    ### File Specific:
     11### File Specific:
    1212   main-programmer: Patrick Boenzli
    1313*/
     
    2222
    2323using namespace std;
    24 
    25 /**
    26  * Initializes a FastFactory
    27  * @param classID the ClassID this Class belongs to (the top-most)
    28  * @param fastFactoryName the Name of the ObjectClass-handled here
    29  * @return a new FastFactory
    30  */
    31 FastFactory::FastFactory (ClassID classID, const char* fastFactoryName)
    32 {
    33   this->setClassID(CL_FAST_FACTORY, "FastFactory");
    34   this->setName(fastFactoryName);
    35 
    36   this->storedClassID = classID;
    37   this->next = NULL;
    38 
    39   this->deadList = NULL;
    40   this->unusedContainers = NULL;
    41 
    42   this->storedDeadObjects = 0;
    43 
    44   FastFactory::registerFastFactory(this);
    45 }
    46 
    47 /** a reference to the First FastFactory */
    48 FastFactory* FastFactory::first = NULL;
    49 
    50 /**
    51  *  destructor
    52  * deletes all the Instances of the FastFactory.
    53  */
    54 FastFactory::~FastFactory ()
    55 {
    56   if (this == first)
    57     this->first = NULL;
    58 
    59   if (this->next)
    60     delete this->next;
    61 }
    62 
    63 /**
    64  * registers a Factory to the List of known factories.
    65  * @param fastFactory The factory to add
    66  *
    67  * needed, to step through all the FastFactories.
    68  */
    69 void FastFactory::registerFastFactory(FastFactory* fastFactory)
    70 {
    71   PRINTF(4)("Registered FastFactory for '%s'\n", fastFactory->getName());
    72 
    73   if( FastFactory::first == NULL)
    74     FastFactory::first = fastFactory;
    75   else
    76   {
    77     FastFactory* tmpFac = FastFactory::first;
    78     while( tmpFac->next != NULL)
    79       tmpFac = tmpFac->next;
    80     tmpFac->setNext(fastFactory);
    81   }
    82 }
    83 
    84 
    85 /**
    86  * searches for a FastFactory
    87  * @param factoryName the Name of the Factory to search for (not used)
    88  * @param classID the ClassID of the FastFactory to search for
    89  * @returns true if found, false otherwise.
    90  */
    91 FastFactory* FastFactory::searchFastFactory(ClassID classID, const char* fastFactoryName)
    92 {
    93   if (FastFactory::first == NULL)
    94     return NULL;
    95    else
    96    {
    97      FastFactory* tmpFac = FastFactory::first;
    98      while (tmpFac != NULL)
    99      {
    100        if (tmpFac->storedClassID == classID)
    101          return tmpFac;
    102        tmpFac = tmpFac->next;
    103      }
    104    }
    105    return NULL;
    106 }
    107 
    108 /**
    109  * Removes all the stored Containers, and sets the Lists back to emptienes.
    110  * @param hardFLUSH if true the containing Objects will also be deleted !! THIS IS DANGEROUS !!
    111  */
    112 void FastFactory::flushAll(bool hardFLUSH)
    113 {
    114   FastFactory* tmpFac = FastFactory::first;
    115   while (tmpFac != NULL)
    116   {
    117     tmpFac->flush(hardFLUSH);
    118     tmpFac = tmpFac->next;
    119   }
    120 }
    121 
    122 
    123 /**
    124  * ereases all the remaining containers, without deleting the stored Objects inside of them.
    125  * @param hardFLUSH if the the containing Objects will also be deleted !! THIS IS DANGEROUS !!
    126  */
    127 void FastFactory::flush(bool hardFLUSH)
    128 {
    129   FastObjectMember* tmpMember = this->deadList, *delMember = NULL;
    130   while (tmpMember != NULL)
    131   {
    132     delMember = tmpMember;
    133     tmpMember = tmpMember->next;
    134     if (unlikely(hardFLUSH == true))
    135       delete delMember->objectPointer;
    136     delete delMember;
    137   }
    138   this->deadList = NULL;
    139 
    140   tmpMember = this->unusedContainers;
    141   while (tmpMember != NULL)
    142   {
    143     delMember = tmpMember;
    144     tmpMember = tmpMember->next;
    145     delete delMember;
    146   }
    147   this->unusedContainers = NULL;
    148 }
    149 
    150 /**
    151  * generates count new Object of the Corresponding class. (precaching)
    152  * @param count How many instances of the class should be generated.
    153  */
    154 void FastFactory::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; i < count; i++)
    161   {
    162     this->fabricate();
    163   }
    164 }
    165 
    166 /**
    167  * gives back live to one Object.
    168  * @return the Object to resurrect.
    169  */
    170 BaseObject* FastFactory::resurrect()
    171 {
    172   PRINTF(4)("Resurecting Object of type %s\n", this->getName());
    173   if (unlikely(this->deadList == NULL))
    174   {
    175     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" \
    176         "Fabricating a new %s", this->getName(), this->getName());
    177     this->fabricate();
    178     return this->resurrect();
    179   }
    180   else
    181   {
    182   FastObjectMember* tmpC = deadList;
    183   this->deadList = this->deadList->next;
    184 
    185   tmpC->next = this->unusedContainers;
    186   this->unusedContainers = tmpC;
    187 
    188   return tmpC->objectPointer;
    189   }
    190 }
    191 
    192 /**
    193  * gives back live to one Object.
    194  * @param classID the class From which to resurrect an Object.
    195  * @return the Object to resurrect, NULL if classID is not found.
    196  */
    197 BaseObject* FastFactory::resurrect(ClassID classID)
    198 {
    199   FastFactory* tmpFac = FastFactory::getFirst();
    200 
    201   while (tmpFac != NULL)
    202   {
    203     if (classID == tmpFac->storedClassID)
    204       return tmpFac->resurrect();
    205     tmpFac = tmpFac->next;
    206   }
    207   return NULL;
    208 }
    209 
    210 /**
    211  * kills Object object, meaning, that it will be stored in the deadList of the FastFactory, and waiting for resurrection
    212  * @param object the Object to kill.
    213  *
    214  * synony that would be really grate would be abolish, but this is more like exterminate than pause-mode.
    215  */
    216 void FastFactory::kill(BaseObject* object)
    217 {
    218   FastObjectMember* tmpC;
    219   if (unlikely(this->unusedContainers == NULL))
    220   {
    221     tmpC = new FastObjectMember;
    222   }
    223   else
    224   {
    225     tmpC = this->unusedContainers;
    226     this->unusedContainers = this->unusedContainers->next;
    227   }
    228 
    229   tmpC->next = this->deadList;
    230   tmpC->objectPointer = object;
    231   this->deadList = tmpC;
    232 }
    233 
    234 
    235 void FastFactory::kill(BaseObject* object, bool searchForFastFactory)
    236 {
    237   if (likely(searchForFastFactory == true))
    238   {
    239     FastFactory* tmpFac = FastFactory::first;
    240     while (tmpFac != NULL)
    241     {
    242       if (object->isA(tmpFac->storedClassID))
    243       {
    244         tmpFac->kill(object);
    245         return;
    246       }
    247       tmpFac = tmpFac->next;
    248     }
    249 
    250   }
    251 }
    252 
    253 
    254 
    255 
    256 
    257 
    258 
    25924
    26025/**
  • orxonox/trunk/src/util/object_manager.h

    r4939 r4940  
    11/*!
    22 * @file object_manager.h
    3  * The ObjectManager (FastFactory) is designed, to automatically generate and remove
    4  * (without to much overhead) many instances of different classes.
    5  *
    6  * It is especially usefull for objects, that come only for a short time into existence,
    7  * and get killed after a small amount of time (like shots).
    8  *
    9  * The Creation of an Object is usually done in the Weapon Class, where one subscribes
    10  * a Projectile with:
    11  * this->bulletFactory = tFastFactory<TestBullet>::getFastFactory(CL_TEST_BULLET, "TestBullet");
    12  * (this might change over time).
    13  * Then you can at loading time initialize an amount of the class with something like:
    14  * this->bulletFactory->prepare(100); // creates 100 entities of TestBullet (dead ones)
    15  * afterwards one can just retrieve an Object form the Class with
    16  * this->bulletFactory->resurrect();  // this returns a BaseObject an Object of the class.
    17 */
    18 
     3 */
    194
    205#ifndef _OBJECT_MANAGER_H
     
    2611// FORWARD DECLARATION //
    2712class GarbageCollector;
    28 template<class T> class tList;
    29 
    30 /**
    31  * Creates a FastFactory to a Loadable FastFactory.
    32  */
    33 //#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \
    34     //tFastFactory<CLASS_NAME>* global_##CLASS_NAME##_FastFactory = new tFastFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
    35 
    36 //! A struct, that holds Lists of Objects of a certain type.
    37 struct FastObjectMember
    38 {
    39   BaseObject*          objectPointer;      //!< Pointer to the Object Stored in this Class (if it is the DeadList, else it is bork)
    40 
    41   FastObjectMember*    next;               //!< the next stored FastObjectMember. (or NULL if this is the last one stored in either the deadList or the unusedContainers)
    42 };
    43 
    44 //! The FastFactory is a fast loadable object creator, and Dynamic List of dead object handler.
    45 /**
    46  * The ObjectManager (FastFactory) is designed, to automatically generate and remove
    47  * (without to much overhead) many instances of different classes.
    48  *
    49  * FastFactory is needed to glue all the tFastFactory<T>'s together.
    50  * It is also the general class that implements the necessary functions
    51  * to generate, resurrect kill and stuff...
    52  */
    53 class FastFactory : public BaseObject {
    54 
    55   public:
    56     virtual ~FastFactory ();
    57 
    58     // functions to push and pop elements of this class
    59     BaseObject* resurrect();
    60     static BaseObject* resurrect(ClassID classID);
    61     void kill(BaseObject* object);
    62     static void kill(BaseObject* object, bool searchForFastFactory);
    63 
    64     void prepare(unsigned int count);
    65     // retrival functions for fast Ineraction
    66     //FastFactory* getFastFactory(ClassID classID);
    67 
    68     static void flushAll(bool hardFLUSH = false);
    69     void flush(bool hardFLUSH = false);
    70 
    71     /** @returns the first FastFactory */
    72     static FastFactory* getFirst() { return FastFactory::first; };
    73 
    74   protected:
    75     FastFactory (ClassID classID, const char* fastFactoryName = NULL);
    76 
    77     /** sets the Next factory in the list @param nextFactory the next factory */
    78     inline void setNext( FastFactory* nextFastFactory) { this->next = nextFastFactory; };
    79     /** @returns the next FastFactory */
    80     FastFactory* getNext() const { return this->next; };
    81 
    82     /** generates a new Object of the Class T */
    83     virtual void fabricate() = NULL;
    84     static FastFactory* searchFastFactory(ClassID classID, const char* fastFactoryName = NULL);
    85 
    86   private:
    87     static void registerFastFactory(FastFactory* fastFactory);
    88 
    89   protected:
    90     ClassID               storedClassID;        //!< The classID of the specified class.
    91     unsigned int          storedDeadObjects;    //!< How many dead objects are stored in this class
    92 
    93     FastObjectMember*     deadList;             //!< A List of all stored dead Objects of this class.
    94     FastObjectMember*     unusedContainers;     //!< This is a List of unused containers, that will be reused by kill.
    95 
    96   private:
    97     static FastFactory*   first;                //!< A pointer to the first FastFactory.
    98 
    99     FastFactory*          next;                 //!< pointer to the next FastFactory.
    100 };
    101 
    102 /**
    103  *  a FastFactory that is able to load any kind of Object from a ClassID
    104  * (this is a Functor)
    105  */
    106 template<class T> class tFastFactory : public FastFactory
    107 {
    108   public:
    109     static tFastFactory<T>* getFastFactory(ClassID classID, const char* fastFactoryName = NULL);
    110 
    111   private:
    112     tFastFactory(ClassID classID, const char* fastFactoryName);
    113 
    114     virtual void fabricate();
    115 };
    116 
    117 /**
    118  * construnts a FastFactory with
    119  * @param fastFactoryName the name of the FastFactory
    120  * @param fastFactory the ID of the class
    121  * @todo (can this be written in another form??)
    122  */
    12313template<class T>
    124     tFastFactory<T>::tFastFactory(ClassID classID, const char* fastFactoryName)
    125   : FastFactory(classID, fastFactoryName)
    126 {
    127 }
    128 
    129 /**
    130  * creates (if not existent) a Factory of Class T, and assigns some values to it
    131  * @param classID the ClassID to assign to this class
    132  * @param fastFactoryName the name to assign
    133  * @returns The FastFactory if existent a new Factory if not.
    134  */
    135 template<class T>
    136     tFastFactory<T>* tFastFactory<T>::getFastFactory(ClassID classID, const char* fastFactoryName)
    137 {
    138   tFastFactory<T>* tmpFac = NULL;
    139   if (FastFactory::getFirst() != NULL)
    140     tmpFac = static_cast<tFastFactory<T>*>(FastFactory::getFirst()->searchFastFactory(classID, fastFactoryName));
    141 
    142   if (tmpFac != NULL)
    143     return tmpFac;
    144   else
    145     return new tFastFactory<T>(classID, fastFactoryName);
    146 }
    147 
    148 
    149 /**
    150  * fabricates an Object of Class T, that corresponds to classID.
    151  */
    152 template<class T>
    153     void tFastFactory<T>::fabricate()
    154 {
    155   FastObjectMember* tmpFirstDead = new FastObjectMember;
    156   tmpFirstDead->objectPointer = new T();
    157   tmpFirstDead->next = this->deadList;
    158   ++this->storedDeadObjects;
    159 
    160   this->deadList = tmpFirstDead;
    161 }
    162 
    163 
    164 
    165 
    166 
    167 
    168 
    169 
     14    class tList;
    17015
    17116////////////////////
Note: See TracChangeset for help on using the changeset viewer.