Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


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??

File:
1 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/**
Note: See TracChangeset for help on using the changeset viewer.