Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9663 in orxonox.OLD for trunk


Ignore:
Timestamp:
Aug 20, 2006, 3:07:59 PM (18 years ago)
Author:
bensch
Message:

more work on classID's

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

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/lang/new_class_id.h

    r9660 r9663  
    1212#include <string>
    1313
     14#include "new_object_list.h"
     15
    1416//! A class to dynamically allocate ClassID's and support a isA operator
    1517class NewClassID
     
    1820  NewClassID();
    1921  ~NewClassID();
     22
     23  void registerObject(NewObjectListBase* object);
    2024
    2125private:
  • trunk/src/lib/lang/new_object_list.cc

    r9662 r9663  
    2525  if (NewObjectListBase::_classes == NULL)
    2626    NewObjectListBase::_classes = new cList;
    27   assert(!NewObjectListBase::exists(className) && "Classes should not be included once, and no two classes should have the same name (key value)");
     27  assert(!NewObjectListBase::classNameExists(className) && "Classes should not be included once, and no two classes should have the same name (key value)");
    2828
     29  this->_id = NewObjectListBase::_idCounter++;
    2930
    3031
     
    4647 * @return true if such a class already exists.
    4748 */
    48 bool NewObjectListBase::exists(const std::string& name)
     49bool NewObjectListBase::classNameExists(const std::string& name)
    4950{
    5051  cList::iterator it;
  • trunk/src/lib/lang/new_object_list.h

    r9662 r9663  
    1010#include "type_info.h"
    1111#include <set>
     12#include <list>
    1213#include <string>
    1314
    14 #define DeclareClass(ClassName) \
    15    NewObjectList ClassID_##ClassName(ClassName)
    1615
    17 #define DeclareInClass(ClassName) \
    18    NewObjectList objectList(ClassName)
     16#define NewObjectListDeclaration(ClassName) \
     17   static NewObjectList<ClassName> objectList
     18
     19#define NewObjectListDefinition(ClassName) \
     20   NewObjectList<ClassName> ClassName::objectList(#ClassName)
    1921
    2022class NewObjectListBase
     
    2325  int id() const { return _id; };
    2426  const std::string& name() const { return _name; };
     27  bool operator==(int id) const { return _id == id; };
     28  bool operator==(const std::string& name) const { return _name == name; };
    2529
    26   static int classCount() { return _idCounter; };
    2730
     31  /// Comparing operators.
    2832  bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); };
    2933  bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); };
     34
     35
     36  static unsigned int                   classCount() { return _idCounter; };
     37  static const std::string&             IDToString(int classID);
     38  static int                            StringToID(const std::string& className);
     39
     40  static const std::list<std::string>&  getClassNames();
    3041
    3142protected:
    3243  NewObjectListBase(const std::string& className);
    3344  ~NewObjectListBase();
     45
    3446private:
    3547  NewObjectListBase(const NewObjectListBase&);
    3648
    37   static bool exists(const std::string& className);
     49  static bool classNameExists(const std::string& className);
    3850
    3951private:
     
    4658  static int                           _idCounter;      //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe.
    4759  static cList*                        _classes;        //!< A Set of all the classes in existance.
     60  static std::list<std::string>        _classNames;     //!< A list of all the registered ClassNames.
    4861};
    4962
     
    5265//// TEMPLATISATION /////
    5366/////////////////////////
     67//! Defines a ObjectsList handler for objects of type T.
     68/**
     69 * @note The Class must define the compare with const std::string& operator for this to work.
     70 */
    5471template<class T> class NewObjectList : public NewObjectListBase
    5572{
     73public:
     74  typedef std::list<T*>               list;
     75  typedef typename list::iterator     iterator;
     76
     77
    5678public:
    5779  NewObjectList(const std::string& name);
    5880  ~NewObjectList();
    5981
     82  T*                      getObject(const std::string& name) const;
     83  inline const list&      objects() const { return _objects; };
     84
     85
    6086private:
    6187  //! the copy constructor will be hidden.
    6288  NewObjectList(const NewObjectList& definer) {};
     89
     90  list                _objects;
    6391};
    6492
     
    7098template <class T>
    7199NewObjectList<T>::~NewObjectList()
    72 {}
     100{
     101  assert(_objects.empty());
     102}
    73103
     104template <class T>
     105T* NewObjectList<T>::getObject(const std::string& name) const
     106{
     107  iterator it = std::find(this->_objects.begin(), this->_objects.end(), name);
     108  if (it != this->_objects.end())
     109    return *it;
     110  else
     111    return NULL;
     112}
    74113
    75114#endif /* _NEW_OBJECT_LIST_H */
Note: See TracChangeset for help on using the changeset viewer.