Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Jul 22, 2005, 2:11:21 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: adapted the Factory algorithm to ObjectManager… still a logn way to go…

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

Legend:

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

    r4836 r4930  
    1919#include "list.h"
    2020
     21#include "debug.h"
    2122
    2223using namespace std;
     
    106107  PRINT(0)("=======================================================\n");
    107108}
     109
     110/**
     111 *  constructor
     112
     113   set everything to zero and define factoryName
     114 */
     115FastObject::FastObject (const char* fastObjectName, ClassID classID)
     116{
     117  this->setClassID(CL_FACTORY, "FastObject");
     118  this->setName(fastObjectName);
     119
     120  this->storedClassID = classID;
     121  this->next = NULL;
     122
     123  FastObject::registerFastObject(this);
     124}
     125
     126/** a reference to the First FastObject */
     127FastObject* FastObject::first = NULL;
     128
     129/**
     130 *  destructor
     131
     132   clear the Q
     133 */
     134FastObject::~FastObject ()
     135{
     136  //  printf("%s\n", this->factoryName);
     137  //  FastObject* tmpDel = this->next;
     138  //  this->next = NULL;
     139  if (this->next)
     140    delete this->next;
     141}
     142
     143/**
     144 *  add a FastObject to the FastObject Queue
     145 * @param factory a FastObject to be registered
     146 */
     147void FastObject::registerFastObject( FastObject* factory)
     148{
     149  PRINTF(4)("Registered FastObject for '%s'\n", factory->getName());
     150
     151  if( FastObject::first == NULL)
     152    FastObject::first = factory;
     153  else
     154  {
     155    FastObject* tmpFac = FastObject::first;
     156    while( tmpFac->next != NULL)
     157    {
     158      tmpFac = tmpFac->next;
     159    }
     160    tmpFac->setNext(factory);
     161  }
     162}
  • orxonox/trunk/src/util/object_manager.h

    r4836 r4930  
    1010
    1111    TO ADD SUPPORT FOR A CLASS do the following steps:
    12     1. include the hader file : #include "class_header.h"
     12    1. include the hader file : #include "class_id.h"
    1313    2. add the class to the type enum classID {}; in class_id.h
    1414    3. define a function void mCache( ClassName ) in class ObjectManager
     
    2121
    2222#include "base_object.h"
    23 #include "projectile.h"
    24 #include "list.h"
    2523
     24template<class T> class tList;
    2625class GarbageCollector;
    27 
    28 
    29 //! This defines the "template" macro function for cache(...)
    30 #define mCache( Class ) \
    31  cache(ClassID index, int number, Class * copyObject)        \
    32 {                                                              \
    33   this->managedObjectList[index] = new tList<BaseObject>(); \
    34   for(int i = 0; i < number; ++i)\
    35     {\
    36       this->managedObjectList[index]->add(new Class (*copyObject));\
    37     }\
    38 }
    39 
    40 
    4126
    4227//! the object manager itself
     
    5136
    5237  /** a class handled by the objectManage */
    53   void mCache(Projectile);
    5438  void addToDeadList(int index, BaseObject* object);
    5539  BaseObject* getFromDeadList(int index, int number = 1);
     40
     41  BaseObject* resurect();
     42  void kill(BaseObject* object);
     43
    5644
    5745  void debug() const;
     
    6856
    6957
     58/**
     59 * Creates a factory to a Loadable Class.
     60 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file)
     61 */
     62#define CREATE_FAST_FACTORY(CLASS_NAME, CLASS_ID) \
     63    tObject<CLASS_NAME>* global_##CLASS_NAME##_Object = new tObject<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
     64
     65//! The Factory is a loadable object handler
     66class 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 factory */
     78    static FastObject* getFirst() { return FastObject::first; };
     79    /** @returns the next factory */
     80    FastObject* getNext() const { return this->next; };
     81
     82  private:
     83    FastObject*          next;                 //!< pointer to the next factory.
     84    static FastObject*   first;                //!< A pointer to the first factory.
     85    ClassID              storedClassID;        //!< The classID of the specified class.
     86};
     87
     88/**
     89 *  a factory that is able to load any kind of Object
     90   (this is a Functor)
     91 */
     92template<class T> class tFastObject : public FastObject
     93{
     94  public:
     95    tFastObject(const char* fastObjectName, ClassID fastObject);
     96
     97  private:
     98    virtual BaseObject* fabricate(ClassID fastObject);
     99};
     100
     101/**
     102 * construnts a factory with
     103 * @param factoryName the name of the factory
     104 */
     105template<class T>
     106tFastObject<T>::tFastObject(const char* fastObjectName, ClassID fastObject) : FastObject(fastObjectName, fastObject)
     107{
     108  PRINTF(5)("Class: %s loadable as a FastObject\n", this->getName());
     109}
     110
     111template<class T>
     112BaseObject* tFastObject<T>::fabricate(ClassID fastObject)
     113{
     114  if(this->classID == fastObject)
     115    return new T ( root);
     116  else if( getNext() != NULL)
     117    return getNext()->fabricate( root);
     118  else
     119    return NULL;
     120}
     121
    70122#endif /* _OBJECT_MANAGER_H */
Note: See TracChangeset for help on using the changeset viewer.