[Orxonox-commit 87] r2784 - trunk/src/core

rgrieder at orxonox.net rgrieder at orxonox.net
Sun Mar 15 01:26:23 CET 2009


Author: rgrieder
Date: 2009-03-15 00:26:23 +0000 (Sun, 15 Mar 2009)
New Revision: 2784

Modified:
   trunk/src/core/Identifier.h
   trunk/src/core/Iterator.h
   trunk/src/core/ObjectListBase.cc
   trunk/src/core/ObjectListBase.h
   trunk/src/core/ObjectListIterator.h
Log:
- Using std::vector instead of std::list in register/unregister Iterator/ObjectListIterator. That is approximately 6 times faster because the list has very few elements.
- Inlined getIdentifier() for sure by exporting the heavy part to another function.
- Also eliminated the need of isFirstCall() because the "static initialisation chaos" only applies to variables that cannot be evaluated at compile time.

Modified: trunk/src/core/Identifier.h
===================================================================
--- trunk/src/core/Identifier.h	2009-03-13 14:39:54 UTC (rev 2783)
+++ trunk/src/core/Identifier.h	2009-03-15 00:26:23 UTC (rev 2784)
@@ -356,11 +356,11 @@
             static ClassIdentifier<T> *getIdentifier();
             static ClassIdentifier<T> *getIdentifier(const std::string& name);
             void addObject(T* object);
-            static bool isFirstCall();
 
             void updateConfigValues(bool updateChildren = true) const;
 
         private:
+            static void initialiseIdentifier();
             ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
             ClassIdentifier()
             {
@@ -375,29 +375,9 @@
     };
 
     template <class T>
-    ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s;
+    ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0;
 
     /**
-        @brief Returns true if the function gets called the first time, false otherwise.
-        @return True if this function got called the first time.
-    */
-    template <class T>
-    bool ClassIdentifier<T>::isFirstCall()
-    {
-        static bool bFirstCall = true;
-
-        if (bFirstCall)
-        {
-            bFirstCall = false;
-            return true;
-        }
-        else
-        {
-            return false;
-        }
-    }
-
-    /**
         @brief Returns the only instance of this class.
         @return The unique Identifier
     */
@@ -405,28 +385,9 @@
     ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
     {
         // check if the static field has already been filled
-        if (ClassIdentifier<T>::isFirstCall())
-        {
-            // Get the name of the class
-            std::string name = typeid(T).name();
+        if (ClassIdentifier<T>::classIdentifier_s == 0)
+            ClassIdentifier<T>::initialiseIdentifier();
 
-            // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
-            ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
-
-            // Get the entry from the map
-            ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)Identifier::getIdentifierSingleton(name, proposal);
-
-            if (ClassIdentifier<T>::classIdentifier_s == proposal)
-            {
-                COUT(4) << "*** Identifier: Requested Identifier for " << name << " was not yet existing and got created." << std::endl;
-            }
-            else
-            {
-                COUT(4) << "*** Identifier: Requested Identifier for " << name << " was already existing and got assigned." << std::endl;
-            }
-        }
-
-        // Finally return the unique ClassIdentifier
         return ClassIdentifier<T>::classIdentifier_s;
     }
 
@@ -444,6 +405,31 @@
     }
 
     /**
+        @brief Assigns the static field for the identifier singleton.
+    */
+    template <class T>
+    void ClassIdentifier<T>::initialiseIdentifier()
+    {
+        // Get the name of the class
+        std::string name = typeid(T).name();
+
+        // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
+        ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
+
+        // Get the entry from the map
+        ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)Identifier::getIdentifierSingleton(name, proposal);
+
+        if (ClassIdentifier<T>::classIdentifier_s == proposal)
+        {
+            COUT(4) << "*** Identifier: Requested Identifier for " << name << " was not yet existing and got created." << std::endl;
+        }
+        else
+        {
+            COUT(4) << "*** Identifier: Requested Identifier for " << name << " was already existing and got assigned." << std::endl;
+        }
+    }
+
+    /**
         @brief Adds an object of the given type to the ObjectList.
         @param object The object to add
     */

Modified: trunk/src/core/Iterator.h
===================================================================
--- trunk/src/core/Iterator.h	2009-03-13 14:39:54 UTC (rev 2783)
+++ trunk/src/core/Iterator.h	2009-03-15 00:26:23 UTC (rev 2784)
@@ -74,7 +74,7 @@
             {
                 this->element_ = exp.element_;
                 this->list_ = exp.list_;
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
             }
 
             /**
@@ -85,7 +85,7 @@
             {
                 this->element_ = other.element_;
                 this->list_ = other.list_;
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
             }
 
             /**
@@ -97,7 +97,7 @@
             {
                 this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0;
                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
             }
 
             /**
@@ -109,7 +109,7 @@
             {
                 this->element_ = (other.element_) ? static_cast<ObjectListBaseElement*>(other.element_) : 0;
                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
             }
 
             /**
@@ -117,7 +117,7 @@
             */
             inline ~Iterator()
             {
-                this->list_->unregisterIterator(this->iterator_);
+                this->list_->unregisterIterator(this);
             }
 
             /**
@@ -127,11 +127,11 @@
             inline const Iterator<T>& operator=(const ObjectListBase::Export& exp)
             {
                 if (this->list_)
-                    this->list_->unregisterIterator(this->iterator_);
+                    this->list_->unregisterIterator(this);
 
                 this->element_ = exp.element_;
                 this->list_ = exp.list_;
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
 
                 return (*this);
             }
@@ -143,11 +143,11 @@
             inline const Iterator<T>& operator=(const Iterator<T>& other)
             {
                 if (this->list_)
-                    this->list_->unregisterIterator(this->iterator_);
+                    this->list_->unregisterIterator(this);
 
                 this->element_ = other.element_;
                 this->list_ = other.list_;
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
 
                 return (*this);
             }
@@ -160,11 +160,11 @@
             inline const Iterator<T>& operator=(ObjectListElement<O>* element)
             {
                 if (this->list_)
-                    this->list_->unregisterIterator(this->iterator_);
+                    this->list_->unregisterIterator(this);
 
                 this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0;
                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
 
                 return (*this);
                 return *this;
@@ -178,11 +178,11 @@
             inline const Iterator<T>& operator=(const ObjectListIterator<O>& other)
             {
                 if (this->list_)
-                    this->list_->unregisterIterator(this->iterator_);
+                    this->list_->unregisterIterator(this);
 
                 this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0;
                 this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
-                this->iterator_ = this->list_->registerIterator(this);
+                this->list_->registerIterator(this);
 
                 return (*this);
             }
@@ -299,7 +299,6 @@
         protected:
             ObjectListBaseElement* element_;       //!< The element the Iterator points at
             ObjectListBase* list_;                 //!< The list wherein the element is
-            std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
     };
 
     typedef Iterator<OrxonoxClass> BaseIterator;

Modified: trunk/src/core/ObjectListBase.cc
===================================================================
--- trunk/src/core/ObjectListBase.cc	2009-03-13 14:39:54 UTC (rev 2783)
+++ trunk/src/core/ObjectListBase.cc	2009-03-15 00:26:23 UTC (rev 2784)
@@ -74,9 +74,9 @@
     */
     void ObjectListBase::notifyIterators(OrxonoxClass* object) const
     {
-        for (std::list<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
+        for (std::vector<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
             ((Iterator<OrxonoxClass>*)(*it))->incrementIfEqual(object);
-        for (std::list<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
+        for (std::vector<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
             ((ObjectListIterator<OrxonoxClass>*)(*it))->incrementIfEqual(object);
     }
 

Modified: trunk/src/core/ObjectListBase.h
===================================================================
--- trunk/src/core/ObjectListBase.h	2009-03-13 14:39:54 UTC (rev 2783)
+++ trunk/src/core/ObjectListBase.h	2009-03-15 00:26:23 UTC (rev 2784)
@@ -37,7 +37,7 @@
 #ifndef _ObjectListBase_H__
 #define _ObjectListBase_H__
 
-#include <list>
+#include <vector>
 
 #include "CorePrereqs.h"
 
@@ -109,10 +109,30 @@
             /** @brief Returns a pointer to the element in front of the first element in the list. @return The element */
             inline Export rend() { return ObjectListBase::Export(this, 0); }
 
-            inline std::list<void*>::iterator registerIterator(void* iterator) { return this->iterators_.insert(this->iterators_.begin(), iterator); }
-            inline void unregisterIterator(const std::list<void*>::iterator& iterator) { this->iterators_.erase(iterator); }
-            inline std::list<void*>::iterator registerObjectListIterator(void* iterator) { return this->objectListIterators_.insert(this->objectListIterators_.begin(), iterator); }
-            inline void unregisterObjectListIterator(const std::list<void*>::iterator& iterator) { this->objectListIterators_.erase(iterator); }
+            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
+            inline void unregisterIterator(void* iterator)
+            {
+                for (unsigned int i = 0; i < this->iterators_.size(); ++i)
+                {
+                    if (iterators_[i] == iterator)
+                    {
+                        iterators_.erase(iterators_.begin() + i);
+                        break;
+                    }
+                }
+            }
+            inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
+            inline void unregisterObjectListIterator(void* iterator)
+            {
+                for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
+                {
+                    if (objectListIterators_[i] == iterator)
+                    {
+                        objectListIterators_.erase(objectListIterators_.begin() + i);
+                        break;
+                    }
+                }
+            }
             void notifyIterators(OrxonoxClass* object) const;
 
             inline Identifier* getIdentifier() const { return this->identifier_; }
@@ -121,8 +141,8 @@
             Identifier* identifier_;               //!< The Iterator owning this list
             ObjectListBaseElement* first_;         //!< The first element in the list
             ObjectListBaseElement* last_;          //!< The last element in the list
-            std::list<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
-            std::list<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
+            std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
+            std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
     };
 }
 

Modified: trunk/src/core/ObjectListIterator.h
===================================================================
--- trunk/src/core/ObjectListIterator.h	2009-03-13 14:39:54 UTC (rev 2783)
+++ trunk/src/core/ObjectListIterator.h	2009-03-15 00:26:23 UTC (rev 2784)
@@ -64,7 +64,7 @@
             inline ObjectListIterator()
             {
                 this->element_ = 0;
-                this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
+                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
             }
 
             /**
@@ -74,7 +74,7 @@
             inline ObjectListIterator(ObjectListElement<T>* element)
             {
                 this->element_ = element;
-                this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
+                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
             }
 
             /**
@@ -84,7 +84,7 @@
             inline ObjectListIterator(const ObjectListIterator<T>& other)
             {
                 this->element_ = other.element_;
-                this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
+                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
             }
 
             /**
@@ -92,7 +92,7 @@
             */
             inline ~ObjectListIterator()
             {
-                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this->iterator_);
+                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this);
             }
 
             /**
@@ -226,7 +226,6 @@
 
         private:
             ObjectListElement<T>* element_;        //!< The element the Iterator points at
-            std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
     };
 }
 




More information about the Orxonox-commit mailing list