/*! * @file new_object_list.h * @brief Definition of a dynamically allocating ClassID * */ #ifndef _NEW_OBJECT_LIST_H #define _NEW_OBJECT_LIST_H #include "type_info.h" #include #include #include #define NewObjectListDeclaration(ClassName) \ static NewObjectList objectList #define NewObjectListDefinition(ClassName) \ NewObjectList ClassName::objectList(#ClassName) class NewObjectListBase { public: int id() const { return _id; }; const std::string& name() const { return _name; }; bool operator==(int id) const { return _id == id; }; bool operator==(const std::string& name) const { return _name == name; }; /// Comparing operators. bool compareName(const NewObjectListBase& more) { return this->_name < more.name(); }; bool compareID(const NewObjectListBase& more) { return this->_id < more.id(); }; static unsigned int classCount() { return _idCounter; }; static const std::string& IDToString(int classID); static int StringToID(const std::string& className); static const std::list& getClassNames(); protected: class IteratorBase { }; NewObjectListBase(const std::string& className); ~NewObjectListBase(); private: NewObjectListBase(const NewObjectListBase&); static bool classNameExists(const std::string& className); private: int _id; std::string _name; private: typedef std::set cList; static int _idCounter; //!< A counter, that gives all classes a Unique ClassID. Access to this Variable is to be Thread-Safe. static cList* _classes; //!< A Set of all the classes in existance. static std::list _classNames; //!< A list of all the registered ClassNames. }; ///////////////////////// //// TEMPLATISATION ///// ///////////////////////// //! Defines a ObjectsList handler for objects of type T. /** * @note The Class must define the compare with const std::string& operator for this to work. */ template class NewObjectList : public NewObjectListBase { public: typedef std::list list; typedef typename list::iterator iterator; class Iterator : public NewObjectListBase::IteratorBase { public: typename NewObjectList::iterator it; }; public: NewObjectList(const std::string& name); ~NewObjectList(); T* getObject(const std::string& name) const; inline const list& objects() const { return _objects; }; void registerObject(T* object); void unregisterObject(const IteratorBase& iterator); private: //! the copy constructor will be hidden. NewObjectList(const NewObjectList& definer) {}; private: list _objects; }; ///////////////////////// //// IMPLEMENTATION ///// ///////////////////////// template NewObjectList::NewObjectList(const std::string& name) : NewObjectListBase(name) {} template NewObjectList::~NewObjectList() { assert(_objects.empty()); } template T* NewObjectList::getObject(const std::string& name) const { iterator it = std::find(this->_objects.begin(), this->_objects.end(), name); if (it != this->_objects.end()) return *it; else return NULL; } template void NewObjectList::registerObject(T* object) { this->_objects.push_back(object); } template void NewObjectList::unregisterObject(const IteratorBase& iterator) { this->_objects.erase(static_cast(iterator)); //_objects.erase(std::find(_objects.begin(), _objects.end(), object)); } #endif /* _NEW_OBJECT_LIST_H */