Changeset 9709 in orxonox.OLD for branches/new_class_id/src/lib/lang
- Timestamp:
- Aug 31, 2006, 10:51:08 PM (18 years ago)
- 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 44 44 //inline const std::string& getClassName() const { return this->className; } 45 45 /** @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(); }; 47 47 /** @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(); }; 49 49 50 50 inline const NewClassID& getClassID() const { return *_leafClassID; } -
branches/new_class_id/src/lib/lang/new_class_id.cc
r9701 r9709 19 19 #include "new_object_list.h" 20 20 21 /** 22 * @brief a Default Constructor. 23 * this Always points to the ID of the NullClass. 24 */ 21 25 NewClassID::NewClassID() 22 26 { 23 *this = NullClass::classID();27 NullClass::acquireID(this->_id, this->_name); 24 28 }; 25 29 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 */ 34 NewClassID::NewClassID(const NewObjectListBase* const objList) 27 35 { 28 29 objList->acquireID(_id, _name); 30 36 objList->acquireID(this->_id, this->_name); 31 37 } 32 38 33 39 34 40 NewClassID NullClass::_classID; 41 42 int NullClass::_nullID; 43 const std::string NullClass::_nullName = std::string("NullClass"); -
branches/new_class_id/src/lib/lang/new_class_id.h
r9701 r9709 12 12 class NewObjectListBase; 13 13 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 */ 15 25 class NewClassID 16 26 { 17 27 public: 18 28 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. */ 21 33 const int& id() const { return *_id; }; 34 /** @returns A constant reference to the Name. */ 22 35 const std::string& name() const { return *_name; }; 23 36 37 /** @param id the id to compare @returns true on match (match is same ID) @brief compares two id's */ 24 38 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 */ 25 40 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 */ 26 42 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 */ 27 44 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 */ 28 46 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 */ 29 48 bool operator!=(const std::string& name) const { return *_name != name; }; 30 49 31 50 private: 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. 34 53 }; 35 54 55 //! A NullClass. This is the Null of the ClassID. 56 /** 57 * implemented as a Class id can be used to reference NullObjects. 58 */ 36 59 class NullClass 37 60 { 38 61 public: 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 40 67 private: 41 NullClass(); 68 NullClass() {}; //!< The Default Constructor is hidden from the User. 69 42 70 private: 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 44 74 }; 45 75 -
branches/new_class_id/src/lib/lang/new_object_list.cc
r9705 r9709 20 20 21 21 22 #include <stdio.h> 23 22 24 /** 23 25 * @brief Constructor, that creates an ObjectList while checking (development mode) for uniqueness of all Keys (names and ID's) … … 27 29 */ 28 30 NewObjectListBase::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); 31 34 if (NewObjectListBase::_classesByID == NULL) 32 35 { … … 47 50 _id = id; 48 51 /// 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; 50 53 51 54 this->_identity = NewClassID(this); … … 98 101 * @return true if such a class already exists. 99 102 */ 103 bool 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 */ 100 113 bool NewObjectListBase::classNameExists(const std::string& name) 101 114 { … … 108 121 * @returns the ClassID if found and NullClass' identity if not. 109 122 */ 110 const NewClassID& NewOb ejctListBase::retrieveIdentity(int id)123 const NewClassID& NewObjectListBase::retrieveIdentity(int id) 111 124 { 112 125 const NewObjectListBase* const base = NewObjectListBase::getObjectList(id); … … 117 130 return NullClass::classID(); 118 131 } 132 119 133 120 134 /** … … 130 144 return base->_identity; 131 145 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(); 144 147 } 145 148 … … 230 233 } 231 234 235 #include "base_object.h" 236 237 /** 238 * @brief Prints out some debugging information about a given List. 239 */ 240 void 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 261 void 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 232 276 233 277 /** -
branches/new_class_id/src/lib/lang/new_object_list.h
r9705 r9709 48 48 class IteratorBase { }; 49 49 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; 52 54 53 55 public: 54 56 /** @returns The Identity of the Class stored within. */ 55 inline const NewClassID& identity() const { return _identity; }57 inline const NewClassID& identity() const { return _identity; } 56 58 /** @returns the ID of the Identity of the ObjectList */ 57 inline int id() const { return _id; };59 inline int id() const { return _id; }; 58 60 /** @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; }; 60 62 /** @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; }; 62 64 /** @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; }; 64 66 /** @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; }; 66 68 /** @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; }; 68 70 /** @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); 73 75 74 76 static const NewObjectListBase* const getObjectList(int classID); … … 76 78 static const NewObjectListBase* const getObjectList(const NewClassID& classID); 77 79 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); 81 83 82 84 /** @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; 84 86 85 87 static const std::list<std::string>& getClassNames(); 86 88 89 87 90 static unsigned int classCount(); 91 void debug(unsigned int level) const; 92 static void debugAll(unsigned int level); 93 88 94 static const std::string& IDToString(int classID); 89 95 static int StringToID(const std::string& className); 90 96 91 virtual void debug() const = 0;92 93 97 //! Only uset to unsubscribe a BaseObject. 94 virtual void unregisterObject(IteratorBase* _iterators) = 0;98 virtual void unregisterObject(IteratorBase* _iterators) = 0; 95 99 96 100 protected: … … 101 105 NewObjectListBase(const NewObjectListBase&); 102 106 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); 105 109 106 110 … … 126 130 //! Defines a ObjectsList handler for objects of type T. 127 131 /** 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 * 128 141 * To define a Class with a ObjectList, you have to: 129 142 * 1. Include 'NewObjectListDeclaration(T);' in its Declaration (at the beginning) … … 132 145 * 133 146 * @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 * 134 165 */ 135 166 template<class T> … … 154 185 ~NewObjectList(); 155 186 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); 176 213 177 214 protected: 178 virtual void getBaseObjectList(NewObjectListBase::base_list* list) const;215 virtual void getBaseObjectList(NewObjectListBase::base_list* list) const; 179 216 180 217 … … 184 221 185 222 private: 186 list _objects; 223 list _objects; //!< The List of stored Objects of Type T. 187 224 }; 188 225 … … 212 249 if (!_objects.empty()) 213 250 { 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"; 216 252 } 217 253 } … … 279 315 NewObjectListBase::IteratorBase* NewObjectList<T>::registerObject(T* object) 280 316 { 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()); 283 319 } 284 320 … … 294 330 } 295 331 296 /**297 * @brief print out some debug information298 * @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() const302 {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 310 332 #endif /* _NEW_OBJECT_LIST_H */
Note: See TracChangeset
for help on using the changeset viewer.