Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9676 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Aug 21, 2006, 11:09:53 PM (18 years ago)
Author:
bensch
Message:

trunk: Better ID handling, now it is possible, to use pre-defined ID's, as it was with the old appreach

Location:
trunk/src/lib/lang
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/lang/new_object_list.cc

    r9675 r9676  
    2020
    2121
    22 NewObjectListBase::NewObjectListBase(const std::string& className)
    23     : _id(-1), _name(className)
     22NewObjectListBase::NewObjectListBase(const std::string& className, int id)
     23    : _name(className)
    2424{
    2525  if (NewObjectListBase::_classesByID == NULL)
     
    2929    NewObjectListBase::_classesByName = new classNameMap;
    3030  }
    31   assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name/ID (key value)");
     31  assert(!NewObjectListBase::classNameExists(className) && "Classes should only be included once, and no two classes should have the same name (key value)");
    3232
    33 
    34   this->_id = NewObjectListBase::_idCounter++;
     33  if (id != -1)
     34  {
     35    assert(!NewObjectListBase::classIDExists(id) && "Classes should only be included once, and no two classes should have the same ID (key value)");
     36  }
     37  else
     38  {
     39    this->_id = NewObjectListBase::_classesByID->size();
     40  }
    3541
    3642  (*NewObjectListBase::_classesByID)[this->_id] = this;
     
    3844}
    3945
     46
     47/**
     48 * Destructor.
     49 *
     50 * This destructor deletes the NewObjectList, and cleans up the NewObjectList sorted Maps.
     51 */
    4052NewObjectListBase::~NewObjectListBase()
    4153{
     
    5365}
    5466
    55 int NewObjectListBase::_idCounter = 0;
    5667NewObjectListBase::classIDMap* NewObjectListBase::_classesByID = NULL;
    5768NewObjectListBase::classNameMap* NewObjectListBase::_classesByName = NULL;
    5869
    5970
     71
     72/**
     73 * @returns the Registered Class Count.
     74 */
     75unsigned int NewObjectListBase::classCount()
     76{
     77  assert (NewObjectListBase::_classesByID != NULL);
     78  return NewObjectListBase::_classesByID->size();
     79};
    6080
    6181/**
     
    6787{
    6888  return (NewObjectListBase::_classesByName->find(name) != NewObjectListBase::_classesByName->end());
    69   //  classNameMap::iterator it;
    70   //   for (it = NewObjectListBase::_classesByID->begin(); it != NewObjectListBase::_classesByID->end(); it++)
    71   //     if(*it != NULL && (*it)->name() != name)
    72   //       return true;
    73   //   return false;
     89}
     90
     91/**
     92 * @brief Checks if a Class with name already exists.
     93 * @param name The Name of the Class to check.
     94 * @return true if such a class already exists.
     95 */
     96bool NewObjectListBase::classIDExists(int id)
     97{
     98  return (NewObjectListBase::_classesByID->find(id) != NewObjectListBase::_classesByID->end());
    7499}
    75100
  • trunk/src/lib/lang/new_object_list.h

    r9675 r9676  
    3737  bool operator==(const std::string& name) const { return _name == name; };
    3838
    39 
    40   /// Comparing operators.
    41 //   struct CompareID  {
    42 //     bool operator()(const NewObjectListBase* less, const NewObjectListBase* more) { return less->id() < more->id(); };
    43 //   };
    44 //   struct CompareName{
    45 //     bool operator()(const NewObjectListBase* less, const NewObjectListBase* more) { return less->name() < more->name(); };
    46 //   };
    47 
    4839  virtual void debug() const = 0;
    4940
    50   static unsigned int                   classCount() { return _idCounter; };
     41  static unsigned int                   classCount();
    5142  static const std::string&             IDToString(int classID);
    5243  static int                            StringToID(const std::string& className);
     
    5748
    5849protected:
    59   NewObjectListBase(const std::string& className);
     50  NewObjectListBase(const std::string& className, int id = -1);
    6051  virtual ~NewObjectListBase();
    6152
     
    6354  NewObjectListBase(const NewObjectListBase&);
    6455
     56  static bool classIDExists(int id);
    6557  static bool classNameExists(const std::string& className);
    6658
     
    7365
    7466private:
    75 
    76   static int                    _idCounter;         //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
    7767  static classIDMap*            _classesByID;       //!< A Map of all the classes in existance.
    7868  static classNameMap*          _classesByName;     //!< A Map of all the classes in existance.
     
    110100
    111101public:
    112   NewObjectList(const std::string& name);
     102  NewObjectList(const std::string& name, int id = -1);
    113103  ~NewObjectList();
    114104
     
    137127/////////////////////////
    138128template <class T>
    139 NewObjectList<T>::NewObjectList(const std::string& name)
    140     : NewObjectListBase(name)
     129NewObjectList<T>::NewObjectList(const std::string& name, int id)
     130    : NewObjectListBase(name, id)
    141131{}
    142132
Note: See TracChangeset for help on using the changeset viewer.