Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9709 in orxonox.OLD for branches/new_class_id/src/lib/lang


Ignore:
Timestamp:
Aug 31, 2006, 10:51:08 PM (18 years ago)
Author:
bensch
Message:

orxonox/branches/new_class_id: new_class ID working, adapdet many classes, and reinvented some of the ClassID stuff

Location:
branches/new_class_id/src/lib/lang
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/new_class_id/src/lib/lang/base_object.h

    r9705 r9709  
    4444  //inline const std::string& getClassName() const { return this->className; }
    4545  /** @returns the className of the corresponding Object as a C-compliant string (const char*) */
    46   inline const char* getClassCName() const { return this->className.c_str(); };
     46  inline const char* getClassCName() const { return _classes.front()._objectList->name().c_str(); };
    4747  /** @returns the ClassName of the Topmost Object of the ClassStack */
    48   inline const std::string& getClassName() const { return _classes.front()._objectList->name(); }
     48  inline const std::string& getClassName() const { return _classes.front()._objectList->name(); };
    4949
    5050  inline const NewClassID& getClassID() const { return *_leafClassID; }
  • branches/new_class_id/src/lib/lang/new_class_id.cc

    r9701 r9709  
    1919#include "new_object_list.h"
    2020
     21/**
     22 * @brief a Default Constructor.
     23 * this Always points to the ID of the NullClass.
     24 */
    2125NewClassID::NewClassID()
    2226{
    23   *this = NullClass::classID();
     27  NullClass::acquireID(this->_id, this->_name);
    2428};
    2529
    26 NewClassID::NewClassID(const NewObjectListBase* objList)
     30/**
     31 * @brief Acquiring the ID of a objectList.
     32 * @param objList The NewObjectList to acquire the ID from.
     33 */
     34NewClassID::NewClassID(const NewObjectListBase* const objList)
    2735{
    28 
    29   objList->acquireID(_id, _name);
    30 
     36  objList->acquireID(this->_id, this->_name);
    3137}
    3238
    3339
    3440NewClassID NullClass::_classID;
     41
     42int NullClass::_nullID;
     43const std::string NullClass::_nullName = std::string("NullClass");
  • branches/new_class_id/src/lib/lang/new_class_id.h

    r9701 r9709  
    1212class NewObjectListBase;
    1313
    14 //! A class to dynamically allocate ClassID's and support a isA operator
     14//! A class to dynamically allocate ClassID's and to support a isA operator
     15/**
     16 * A NewClassID can only be aquired over a NewObjectList,
     17 * thus enabling the developer to have permanent access to the correct ID and ClassName
     18 *
     19 * The Idea behind this concept is, that storing a NewClassID that changes its internal state
     20 * all NewClassID's will be updated correctly.
     21 *
     22 * Since the Existance of any NewObjectList is a requirement during the working process all
     23 * ID's should reference a valid ID and ClassName
     24 */
    1525class NewClassID
    1626{
    1727public:
    1828  NewClassID();
    19   NewClassID(const NewObjectListBase* id);
    20   // the copy constructor is also defined.
     29  NewClassID(const NewObjectListBase* const id);
     30  /// the copy constructor is also defined implicitely.
     31
     32  /** @returns A constant reference to the ID. */
    2133  const int& id() const { return *_id; };
     34  /** @returns A constant reference to the Name. */
    2235  const std::string& name() const { return *_name; };
    2336
     37  /** @param id the id to compare @returns true on match (match is same ID) @brief compares two id's */
    2438  bool operator==(const NewClassID& id) const { return *_id == *id._id /* || _name == id._name */; };
     39  /** @param id the id to compare @returns true on match (match is same ID) @brief compares two id's */
    2540  bool operator==(int id) const { return *_id == id; };
     41  /** @param name the id to compare @returns true on match (match is same Name) @brief compares an ID with a ClassName */
    2642  bool operator==(const std::string& name) const { return *_name == name; };
     43  /** @param id the id to compare @returns false on match (match is same ID) @brief compares two id's */
    2744  bool operator!=(const NewClassID& id) const { return *_id != *id._id /* && _name != id._name*/;  };
     45  /** @param id the id to compare @returns false on match (match is same ID) @brief compares two id's */
    2846  bool operator!=(int id) const { return *_id != id; };
     47  /** @param name the id to compare @returns false on match (match is same Name) @brief compares an ID with a ClassName */
    2948  bool operator!=(const std::string& name) const { return *_name != name; };
    3049
    3150private:
    32   const int*                _id;
    33   const std::string*        _name;
     51  const int*                _id;           //!< A pointer to the ID of the ClassID.
     52  const std::string*        _name;         //!< A pointer to the Name of the ClassID.
    3453};
    3554
     55//! A NullClass. This is the Null of the ClassID.
     56/**
     57 * implemented as a Class id can be used to reference NullObjects.
     58 */
    3659class NullClass
    3760{
    3861public:
    39   static NewClassID classID() { return NullClass::_classID; }
     62  /** @returns the NullClass' ID. */
     63  static const NewClassID& classID() { return NullClass::_classID; }
     64  /** @param id the ID to acquire @param name the name to acquire @brief acquires the ID of this Class */
     65  static void acquireID(const int*& id, const std::string*& name) { id = &_nullID; name = &_nullName; };
     66
    4067private:
    41   NullClass();
     68  NullClass() {}; //!< The Default Constructor is hidden from the User.
     69
    4270private:
    43   static NewClassID _classID;
     71  static NewClassID         _classID;      //!< The NullClass' ID
     72  static const std::string  _nullName;     //!< The NullClass' Name ("NullClass")
     73  static int                _nullID;       //!< The NullClass' ID
    4474};
    4575
  • branches/new_class_id/src/lib/lang/new_object_list.cc

    r9705 r9709  
    2020
    2121
     22#include <stdio.h>
     23
    2224/**
    2325 * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's)
     
    2729 */
    2830NewObjectListBase::NewObjectListBase(const std::string& className, int id)
    29   : _name(className)
    30 {
     31    : _name(className)
     32{
     33  printf("NewObjectList, Registered %s::%d\n", className.c_str(), id);
    3134  if (NewObjectListBase::_classesByID == NULL)
    3235  {
     
    4750  _id = id;
    4851  /// Some Output, that will fall out later
    49   //std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
     52  std::cout << "register new ObjectList " << className << " ID: " << this->_id << std::endl;
    5053
    5154  this->_identity = NewClassID(this);
     
    98101 * @return true if such a class already exists.
    99102 */
     103bool NewObjectListBase::classIDExists(int id)
     104{
     105  return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
     106}
     107
     108/**
     109 * @brief Checks if a Class with name already exists.
     110 * @param name The Name of the Class to check.
     111 * @return true if such a class already exists.
     112 */
    100113bool NewObjectListBase::classNameExists(const std::string& name)
    101114{
     
    108121 * @returns the ClassID if found and NullClass' identity if not.
    109122 */
    110 const NewClassID& NewObejctListBase::retrieveIdentity(int id)
     123const NewClassID& NewObjectListBase::retrieveIdentity(int id)
    111124{
    112125  const NewObjectListBase* const base = NewObjectListBase::getObjectList(id);
     
    117130    return NullClass::classID();
    118131}
     132
    119133
    120134/**
     
    130144    return base->_identity;
    131145  else
    132     return NullObject::classID();
    133 }
    134 
    135 
    136 /**
    137  * @brief Checks if a Class with name already exists.
    138  * @param name The Name of the Class to check.
    139  * @return true if such a class already exists.
    140  */
    141 bool NewObjectListBase::classIDExists(int id)
    142 {
    143   return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
     146    return NullClass::classID();
    144147}
    145148
     
    230233}
    231234
     235#include "base_object.h"
     236
     237/**
     238 * @brief Prints out some debugging information about a given List.
     239 */
     240void NewObjectListBase::debug(unsigned int level) const
     241{
     242  base_list list;
     243  this->getBaseObjectList(&list);
     244
     245  if (level > 1 || !list.empty())
     246    printf(" ObjectList of class %s(id:%d) contains %d objects\n", this->name().c_str(), this->id(), list.size());
     247
     248  if (level >= 2)
     249  {
     250    printf("  - listing Instances: \n");
     251    for (base_iterator it = list.begin();
     252         it != list.end();
     253         ++it)
     254    {
     255      printf("   + %s::%s\n", (*it)->getClassCName(), (*it)->getCName());
     256    }
     257  }
     258}
     259
     260
     261void NewObjectListBase::debugAll(unsigned int level)
     262{
     263  printf("Listing all %d ObjectLists \n", NewObjectListBase::_classesByID->size());
     264
     265  for (classNameMap::const_iterator it = NewObjectListBase::_classesByName->begin();
     266       it != NewObjectListBase::_classesByName->end();
     267       ++it)
     268  {
     269    (*it).second->debug(level);
     270
     271  }
     272
     273}
     274
     275
    232276
    233277/**
  • branches/new_class_id/src/lib/lang/new_object_list.h

    r9705 r9709  
    4848  class IteratorBase { };
    4949
    50   typedef std::list<BaseObject*>            base_list;
    51   typedef base_list::iterator               base_iterator;
     50  /** @brief A Typedefinition for the Base-List that can be retrieved with getBaseObjectList(base_list*) */
     51  typedef std::list<BaseObject*>        base_list;
     52  /** @brief An iterator for the base_list */
     53  typedef base_list::iterator           base_iterator;
    5254
    5355public:
    5456  /** @returns The Identity of the Class stored within. */
    55   inline const NewClassID& identity() const { return _identity; }
     57  inline const NewClassID&              identity() const { return _identity; }
    5658  /** @returns the ID of the Identity of the ObjectList */
    57   inline int id() const { return _id; };
     59  inline int                            id() const { return _id; };
    5860  /** @returns The Name of the Class stored in this ObjectList */
    59   inline const std::string& name() const { return _name; };
     61  inline const std::string&             name() const { return _name; };
    6062  /** @param id The id to compare @returns true on match, false otherwise */
    61   inline bool operator==(int id) const { return _id == id; };
     63  inline bool                           operator==(int id) const { return _id == id; };
    6264  /** @param id The id to compare @returns true on match, false otherwise */
    63   inline bool operator==(const NewClassID& id) const { return id == _id; };
     65  inline bool                           operator==(const NewClassID& id) const { return id == _id; };
    6466  /** @param name The name to compare @returns true on match, false otherwise */
    65   inline bool operator==(const std::string& name) const { return _name == name; };
     67  inline bool                           operator==(const std::string& name) const { return _name == name; };
    6668  /** @param id The id to check @returns true on match, false otherwise */
    67   inline void acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
     69  inline void                           acquireID(const int*& id, const std::string*& name) const { id = &_id; name = &_name; };
    6870  /** @brief fills a list of Objects into a BaseObject*-List. @param list the list to fill */
    69   virtual void getBaseObjectList(base_list* list) const = 0;
    70 
    71   static const NewClassID& retrieveIdentity(int id);
    72   static const NewClassID& retrieveIdentity(const std::string& name);
     71  virtual void                          getBaseObjectList(base_list* list) const = 0;
     72
     73  static const NewClassID&              retrieveIdentity(int id);
     74  static const NewClassID&              retrieveIdentity(const std::string& name);
    7375
    7476  static const NewObjectListBase* const getObjectList(int classID);
     
    7678  static const NewObjectListBase* const getObjectList(const NewClassID& classID);
    7779
    78   static BaseObject* getBaseObject(int classID, const std::string& objectName);
    79   static BaseObject* getBaseObject(const std::string& className, const std::string& objectName);
    80   static BaseObject* getBaseObject(const NewClassID& classID, const std::string& objectName);
     80  static BaseObject*                    getBaseObject(int classID, const std::string& objectName);
     81  static BaseObject*                    getBaseObject(const std::string& className, const std::string& objectName);
     82  static BaseObject*                    getBaseObject(const NewClassID& classID, const std::string& objectName);
    8183
    8284  /** @returns an Object with Name name out of this List @param name the name of the Object. */
    83   virtual BaseObject* getBaseObject(const std::string& name) const = 0;
     85  virtual BaseObject*                   getBaseObject(const std::string& name) const = 0;
    8486
    8587  static const std::list<std::string>&  getClassNames();
    8688
     89
    8790  static unsigned int                   classCount();
     91  void                                  debug(unsigned int level) const;
     92  static void                           debugAll(unsigned int level);
     93
    8894  static const std::string&             IDToString(int classID);
    8995  static int                            StringToID(const std::string& className);
    9096
    91   virtual void debug() const = 0;
    92 
    9397  //! Only uset to unsubscribe a BaseObject.
    94   virtual void unregisterObject(IteratorBase* _iterators) = 0;
     98  virtual void                          unregisterObject(IteratorBase* _iterators) = 0;
    9599
    96100protected:
     
    101105  NewObjectListBase(const NewObjectListBase&);
    102106
    103   static bool classIDExists(int id);
    104   static bool classNameExists(const std::string& className);
     107  static bool                         classIDExists(int id);
     108  static bool                         classNameExists(const std::string& className);
    105109
    106110
     
    126130//! Defines a ObjectsList handler for objects of type T.
    127131/**
     132 * The ObjectList is a generic way to store every object created from a Class 'T'
     133 *  into a List. The list can be retrieved, and used constantly over iterators,
     134 *  as with normal std::list.
     135 *
     136 * Furthermore the linkage over the single Lists is given over the Superclass NewObjectListBase.
     137 *
     138 *
     139 *
     140 *
    128141 * To define a Class with a ObjectList, you have to:
    129142 *  1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning)
     
    132145 *
    133146 * @note The Class must define the compare with const std::string& operator for this to work.
     147 *
     148 *
     149 *
     150 * Limitations:
     151 *  ObjectList cannot be used with other Factory style Classes, if the class is also a BaseObject,
     152 *   and not loaded before the ObjectList.
     153 *
     154 * @example Iterating: Iteration is made easy, and fast as follows:
     155 *   for (NewObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin();
     156 *      it != PlayerStats::objectList().end();
     157 *     ++it)
     158 *   {
     159 *    (*it)->dosomething
     160 *   }
     161 *
     162 * @example Find an Object:
     163 * Playable* playable = Playable::objectList("orxonox-super-rocket-fighter"); // searches an Object By its name.
     164 *
    134165 */
    135166template<class T>
     
    154185  ~NewObjectList();
    155186
    156   virtual BaseObject*     getBaseObject(const std::string& name) const;
    157   T*                      getObject(const std::string& name) const;
    158   inline const list&      objects() const { return _objects; };
    159   bool exists(const T* const object) const;
    160 
    161   inline iterator begin() { return _objects.begin(); };
    162   inline const_iterator begin() const { return _objects.begin(); };
    163   inline iterator  end() { return _objects.end(); };
    164   inline const_iterator  end() const { return _objects.end(); };
    165 
    166   inline bool empty() const { return _objects.empty(); };
    167   inline int size() const { return _objects.size(); };
    168   inline T* front() const { return _objects.front(); };
    169   inline T* back() const { return _objects.back(); };
    170 
    171 
    172   NewObjectListBase::IteratorBase* registerObject(T* object);
    173   virtual void unregisterObject(IteratorBase* iterator);
    174 
    175   virtual void debug() const;
     187  virtual BaseObject*                getBaseObject(const std::string& name) const;
     188  T*                                 getObject(const std::string& name) const;
     189  inline const list&                 objects() const { return _objects; };
     190  bool                               exists(const T* const object) const;
     191
     192  /** @returns an Iterator to the beginning of the List. */
     193  inline iterator                    begin() { return _objects.begin(); };
     194  /** @returns a constant Iterator to the beginning of the List. */
     195  inline const_iterator              begin() const { return _objects.begin(); };
     196  /** @returns an Iterator to the end of the List. */
     197  inline iterator                    end() { return _objects.end(); };
     198  /** @returns a constant Iterator to the beginning of the List. */
     199  inline const_iterator              end() const { return _objects.end(); };
     200
     201  /** @returns true if the List is empty. */
     202  inline bool                        empty() const { return _objects.empty(); };
     203  /** @returns the size of the List */
     204  inline int                         size() const { return _objects.size(); };
     205  /** @returns the frontmost Element of the list (normaly the last added Object). */
     206  inline T*                          front() const { return _objects.front(); };
     207  /** @returns the last added Object */
     208  inline T*                          back() const { return _objects.back(); };
     209
     210
     211  NewObjectListBase::IteratorBase*   registerObject(T* object);
     212  virtual void                       unregisterObject(IteratorBase* iterator);
    176213
    177214protected:
    178   virtual void getBaseObjectList(NewObjectListBase::base_list* list) const;
     215  virtual void                       getBaseObjectList(NewObjectListBase::base_list* list) const;
    179216
    180217
     
    184221
    185222private:
    186   list                _objects;
     223  list                _objects;     //!< The List of stored Objects of Type T.
    187224};
    188225
     
    212249  if (!_objects.empty())
    213250  {
    214     std::cout << "There are still Object in the ObjectList of " << this->name() << "(id:" << this->id() << ")\n";
    215     this->debug();
     251    // std::cout << "There are " << this->size() << " objects from class " << this->name() << "(id:" << this->id() << ") in existance\n";
    216252  }
    217253}
     
    279315NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object)
    280316{
    281   this->_objects.push_front(object);
    282   return new Iterator(this->_objects.begin());
     317  this->_objects.push_back(object);
     318  return new Iterator(--this->_objects.end());
    283319}
    284320
     
    294330}
    295331
    296 /**
    297  * @brief print out some debug information
    298  * @note this function will most probably vanish from here and be completely moved to the base class.
    299  */
    300 template <class T>
    301 void NewObjectList<T>::debug() const
    302 {
    303   const_iterator it;
    304   for (it = this->_objects.begin(); it != this->_objects.end(); ++it)
    305   {
    306     std::cout << (*it)->getName() << std::endl;
    307   }
    308 }
    309 
    310332#endif /* _NEW_OBJECT_LIST_H */
Note: See TracChangeset for help on using the changeset viewer.