[Orxonox-commit 5735] r10395 - in code/branches/core7: src/libraries/core src/libraries/core/class test/core/object

landauf at orxonox.net landauf at orxonox.net
Sat Apr 25 22:49:34 CEST 2015


Author: landauf
Date: 2015-04-25 22:49:34 +0200 (Sat, 25 Apr 2015)
New Revision: 10395

Modified:
   code/branches/core7/src/libraries/core/CoreIncludes.h
   code/branches/core7/src/libraries/core/class/Identifier.cc
   code/branches/core7/src/libraries/core/class/Identifier.h
   code/branches/core7/src/libraries/core/class/IdentifierManager.cc
   code/branches/core7/src/libraries/core/class/IdentifierManager.h
   code/branches/core7/test/core/object/ContextTest.cc
   code/branches/core7/test/core/object/IteratorTest.cc
   code/branches/core7/test/core/object/ListableTest.cc
   code/branches/core7/test/core/object/ObjectListIteratorTest.cc
Log:
create and initialize Identifiers explicitly via registerClass(). registerClass() must be called before using an Identifier of this type.
(some tests will currently fail)

Modified: code/branches/core7/src/libraries/core/CoreIncludes.h
===================================================================
--- code/branches/core7/src/libraries/core/CoreIncludes.h	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/src/libraries/core/CoreIncludes.h	2015-04-25 20:49:34 UTC (rev 10395)
@@ -133,7 +133,7 @@
     @param ClassName The name of the class
 */
 #define RegisterObject(ClassName) \
-    if (ClassIdentifier<ClassName>::getIdentifier(#ClassName)->initializeObject(this)) \
+    if (ClassIdentifier<ClassName>::getIdentifier()->initializeObject(this)) \
         return; \
     else \
         ((void)0)
@@ -167,9 +167,8 @@
     inline Identifier* registerClass(const std::string& name, Factory* factory, bool bLoadable = true)
     {
         orxout(verbose, context::misc::factory) << "Create entry for " << name << " in Factory." << endl;
-        Identifier* identifier = ClassIdentifier<T>::getIdentifier(name);
-        identifier->setFactory(factory);
-        identifier->setLoadable(bLoadable);
+        Identifier* identifier = new ClassIdentifier<T>(name, factory, bLoadable);
+        IdentifierManager::getInstance().addIdentifier(identifier);
         return identifier;
     }
 

Modified: code/branches/core7/src/libraries/core/class/Identifier.cc
===================================================================
--- code/branches/core7/src/libraries/core/class/Identifier.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/src/libraries/core/class/Identifier.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -49,12 +49,13 @@
     /**
         @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
     */
-    Identifier::Identifier()
+    Identifier::Identifier(const std::string& name, Factory* factory, bool bLoadable)
         : classID_(IdentifierManager::getInstance().getUniqueClassId())
     {
-        this->factory_ = 0;
+        this->name_ = name;
+        this->factory_ = factory;
+        this->bLoadable_ = bLoadable;
         this->bInitialized_ = false;
-        this->bLoadable_ = false;
         this->bIsVirtualBase_ = false;
 
         this->bHasConfigValues_ = false;
@@ -79,27 +80,7 @@
             delete (it->second);
     }
 
-    /**
-        @brief Sets the name of the class.
-    */
-    void Identifier::setName(const std::string& name)
-    {
-        if (name != this->name_)
-        {
-            this->name_ = name;
-            IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
-        }
-    }
 
-    void Identifier::setFactory(Factory* factory)
-    {
-        if (this->factory_)
-            delete this->factory_;
-
-        this->factory_ = factory;
-    }
-
-
     /**
         @brief Creates an object of the type the Identifier belongs to.
         @return The new object
@@ -126,7 +107,7 @@
     void Identifier::setNetworkID(uint32_t id)
     {
         this->networkID_ = id;
-        IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
+        IdentifierManager::getInstance().addIdentifier(this);
     }
 
     /**

Modified: code/branches/core7/src/libraries/core/class/Identifier.h
===================================================================
--- code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-25 20:49:34 UTC (rev 10395)
@@ -81,6 +81,7 @@
 #include <loki/TypeTraits.h>
 
 #include "util/Output.h"
+#include "util/OrxAssert.h"
 #include "core/object/ObjectList.h"
 #include "core/object/Listable.h"
 #include "core/object/Context.h"
@@ -108,13 +109,12 @@
     class _CoreExport Identifier : public Destroyable
     {
         public:
-            Identifier();
+            Identifier(const std::string& name, Factory* factory, bool bLoadable);
             Identifier(const Identifier& identifier); // don't copy
             virtual ~Identifier();
 
             /// Returns the name of the class the Identifier belongs to.
             inline const std::string& getName() const { return this->name_; }
-            void setName(const std::string& name);
 
             /// Returns the name of the class as it is returned by typeid(T).name()
             virtual const std::string& getTypeidName() = 0;
@@ -126,8 +126,6 @@
             /// Returns the unique ID of the class.
             ORX_FORCEINLINE unsigned int getClassID() const { return this->classID_; }
 
-            /// Sets the Factory.
-            void setFactory(Factory* factory);
             /// Returns true if the Identifier has a Factory.
             inline bool hasFactory() const { return (this->factory_ != 0); }
 
@@ -135,8 +133,6 @@
 
             /// Returns true if the class can be loaded through XML.
             inline bool isLoadable() const { return this->bLoadable_; }
-            /// Set the class to be loadable through XML or not.
-            inline void setLoadable(bool bLoadable) { this->bLoadable_ = bLoadable; }
 
             /// Returns true if child classes should inherit virtually from this class.
             inline bool isVirtualBase() const { return this->bIsVirtualBase_; }
@@ -263,9 +259,19 @@
         #endif
 
         public:
-            static ClassIdentifier<T>* getIdentifier();
-            static ClassIdentifier<T>* getIdentifier(const std::string& name);
+            ClassIdentifier(const std::string& name, Factory* factory, bool bLoadable) : Identifier(name, factory, bLoadable)
+            {
+                OrxVerify(ClassIdentifier<T>::classIdentifier_s == NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
+                ClassIdentifier<T>::classIdentifier_s = this;
 
+                this->typeidName_ = typeid(T).name();
+                SuperFunctionInitialization<0, T>::initialize(this);
+            }
+            ~ClassIdentifier()
+            {
+                SuperFunctionDestruction<0, T>::destroy(this);
+            }
+
             bool initializeObject(T* object);
 
             void setConfigValues(T* object, Configurable*) const;
@@ -282,19 +288,10 @@
             virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) const
                 { return dynamic_cast<T*>(object) != 0; }
 
-        private:
-            static void initializeIdentifier();
+            static ClassIdentifier<T>* getIdentifier();
 
+        private:
             ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
-            ClassIdentifier()
-            {
-                this->typeidName_ = typeid(T).name();
-                SuperFunctionInitialization<0, T>::initialize(this);
-            }
-            ~ClassIdentifier()
-            {
-                SuperFunctionDestruction<0, T>::destroy(this);
-            }
 
             void updateConfigValues(bool updateChildren, Listable*) const;
             void updateConfigValues(bool updateChildren, Identifiable*) const;
@@ -311,50 +308,16 @@
         @return The unique Identifier
     */
     template <class T>
-    inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
+    /*static*/ inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
     {
-        // check if the Identifier already exists
-        if (!ClassIdentifier<T>::classIdentifier_s)
-            ClassIdentifier<T>::initializeIdentifier();
+        if (ClassIdentifier<T>::classIdentifier_s == NULL)
+            ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*) IdentifierManager::getInstance().getIdentifierByTypeidName(typeid(T).name());
 
+        OrxVerify(ClassIdentifier<T>::classIdentifier_s != NULL, "Assertion failed in ClassIdentifier of type " << typeid(T).name());
         return ClassIdentifier<T>::classIdentifier_s;
     }
 
     /**
-        @brief Does the same as getIdentifier() but sets the name if this wasn't done yet.
-        @param name The name of this Identifier
-        @return The Identifier
-    */
-    template <class T>
-    inline ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name)
-    {
-        ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
-        identifier->setName(name);
-        return identifier;
-    }
-
-    /**
-        @brief Assigns the static field for the identifier singleton.
-    */
-    template <class T>
-    /*static */ void ClassIdentifier<T>::initializeIdentifier()
-    {
-        // create a new identifier anyway. Will be deleted if not used.
-        ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
-
-        // Get the entry from the map
-        ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)IdentifierManager::getInstance().getGloballyUniqueIdentifier(proposal);
-
-        if (ClassIdentifier<T>::classIdentifier_s == proposal)
-            orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was not yet existing and got created." << endl;
-        else
-        {
-            orxout(verbose, context::identifier) << "Requested Identifier for " << proposal->getTypeidName() << " was already existing and got assigned." << endl;
-            delete proposal; // delete proposal (it is not used anymore)
-        }
-    }
-
-    /**
         @brief Adds an object of the given type to the ObjectList.
         @param object The object to add
     */

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -57,42 +57,16 @@
     }
 
     /**
-        @brief Returns an identifier by name and adds it if not available
-        @param proposal A pointer to a newly created identifier for the case of non existence in the map
-        @return The identifier (unique instance)
-    */
-    Identifier* IdentifierManager::getGloballyUniqueIdentifier(Identifier* proposal)
-    {
-        const std::string& typeidName = proposal->getTypeidName();
-        std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
-
-        if (it != this->identifierByTypeidName_.end())
-        {
-            // There is already an entry: return it
-            return it->second;
-        }
-        else
-        {
-            // There is no entry: put the proposal into the map and return it
-            this->identifierByTypeidName_[typeidName] = proposal;
-            return proposal;
-        }
-    }
-
-    /**
      * Registers the identifier in all maps of the IdentifierManager.
      */
-    void IdentifierManager::addIdentifierToLookupMaps(Identifier* identifier)
+    void IdentifierManager::addIdentifier(Identifier* identifier)
     {
-        const std::string& typeidName = identifier->getTypeidName();
-        if (this->identifierByTypeidName_.find(typeidName) != this->identifierByTypeidName_.end())
-        {
-            this->identifierByString_[identifier->getName()] = identifier;
-            this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
-            this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
-        }
-        else
-            orxout(internal_warning) << "Trying to add an identifier to lookup maps which is not known to IdentifierManager" << endl;
+        orxout(verbose, context::identifier) << "Adding identifier for " << identifier->getName() << " / " << identifier->getTypeidName() << endl;
+
+        this->identifierByTypeidName_[identifier->getTypeidName()] = identifier;
+        this->identifierByString_[identifier->getName()] = identifier;
+        this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
+        this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
     }
 
     /**
@@ -271,6 +245,20 @@
     }
 
     /**
+        @brief Returns the Identifier with a given typeid-name.
+        @param name The typeid-name of the wanted Identifier
+        @return The Identifier
+    */
+    Identifier* IdentifierManager::getIdentifierByTypeidName(const std::string& typeidName)
+    {
+        std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
+        if (it != this->identifierByTypeidName_.end())
+            return it->second;
+        else
+            return 0;
+    }
+
+    /**
         @brief Cleans the NetworkID map (needed on clients for correct initialization)
     */
     void IdentifierManager::clearNetworkIDs()

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.h
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-04-25 20:49:34 UTC (rev 10395)
@@ -47,8 +47,7 @@
         public:
             static IdentifierManager& getInstance();
 
-            Identifier* getGloballyUniqueIdentifier(Identifier* proposal);
-            void addIdentifierToLookupMaps(Identifier* identifier);
+            void addIdentifier(Identifier* identifier);
 
             unsigned int getUniqueClassId()
                 { return this->classIDCounter_s++; }
@@ -74,6 +73,7 @@
             Identifier* getIdentifierByString(const std::string& name);
             Identifier* getIdentifierByLowercaseString(const std::string& name);
             Identifier* getIdentifierByID(uint32_t id);
+            Identifier* getIdentifierByTypeidName(const std::string& typeidName);
 
             void clearNetworkIDs();
 

Modified: code/branches/core7/test/core/object/ContextTest.cc
===================================================================
--- code/branches/core7/test/core/object/ContextTest.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/test/core/object/ContextTest.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -13,6 +13,8 @@
                 SubclassContext() : Context(NULL) { RegisterObject(SubclassContext); }
         };
 
+        RegisterClassNoArgs(SubclassContext);
+
         // Fixture
         class ContextTest : public ::testing::Test
         {

Modified: code/branches/core7/test/core/object/IteratorTest.cc
===================================================================
--- code/branches/core7/test/core/object/IteratorTest.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/test/core/object/IteratorTest.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -23,6 +23,9 @@
                 MOCK_METHOD0(test, void());
         };
 
+        RegisterClassNoArgs(TestInterface);
+        RegisterClassNoArgs(TestClass);
+
         // Fixture
         class IteratorTest : public ::testing::Test
         {

Modified: code/branches/core7/test/core/object/ListableTest.cc
===================================================================
--- code/branches/core7/test/core/object/ListableTest.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/test/core/object/ListableTest.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -18,6 +18,9 @@
                 ListableSubclassTest() { RegisterObject(ListableSubclassTest); }
         };
 
+        RegisterClassNoArgs(ListableClassTest);
+        RegisterClassNoArgs(ListableSubclassTest);
+
         template <class T>
         bool objectListContains(T* element, Context* context = Context::getRootContext())
         {

Modified: code/branches/core7/test/core/object/ObjectListIteratorTest.cc
===================================================================
--- code/branches/core7/test/core/object/ObjectListIteratorTest.cc	2015-04-24 07:08:52 UTC (rev 10394)
+++ code/branches/core7/test/core/object/ObjectListIteratorTest.cc	2015-04-25 20:49:34 UTC (rev 10395)
@@ -16,6 +16,8 @@
                 MOCK_METHOD0(test, void());
         };
 
+        RegisterClassNoArgs(ListableTest);
+
         // Fixture
         class ObjectListIteratorTest : public ::testing::Test
         {




More information about the Orxonox-commit mailing list