[Orxonox-commit 5919] r10578 - in code/branches/core7/src: libraries/core libraries/core/object orxonox orxonox/gametypes orxonox/infos

landauf at orxonox.net landauf at orxonox.net
Wed Sep 9 16:10:21 CEST 2015


Author: landauf
Date: 2015-09-09 16:10:21 +0200 (Wed, 09 Sep 2015)
New Revision: 10578

Modified:
   code/branches/core7/src/libraries/core/BaseObject.cc
   code/branches/core7/src/libraries/core/BaseObject.h
   code/branches/core7/src/libraries/core/Namespace.cc
   code/branches/core7/src/libraries/core/object/StrongPtr.h
   code/branches/core7/src/orxonox/Level.cc
   code/branches/core7/src/orxonox/Scene.cc
   code/branches/core7/src/orxonox/gametypes/Gametype.cc
   code/branches/core7/src/orxonox/infos/PlayerInfo.cc
Log:
removed the 'bAddRef' argument from StrongPtr's constructor. it was a bad hack and shouldn't be used. in fact, it was only used to avoid recursive dependencies if one of the four base objects (Namespace, Level, Gametype, Scene) referenced itself with a StrongPtr.
instead of the 'bAddRef'-hack BaseObject now stores the references in a new construct called StrongOrWeakPtr which can either store the reference in a StrongPtr (like it did before) or in a WeakPtr (which is a cleaner alternative to 'bAddRef=true')

Modified: code/branches/core7/src/libraries/core/BaseObject.cc
===================================================================
--- code/branches/core7/src/libraries/core/BaseObject.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/libraries/core/BaseObject.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -76,10 +76,12 @@
         if (this->creator_)
         {
             this->setFile(this->creator_->getFile());
-            this->setNamespace(this->creator_->namespace_);
-            this->setScene(this->creator_->scene_, this->creator_->sceneID_);
-            this->setGametype(this->creator_->gametype_);
-            this->setLevel(this->creator_->level_);
+
+            // store strong-pointers on all four base objects by default (can be overwritten with weak-ptr after the constructor if necessary)
+            this->setNamespace(this->creator_->namespace_.createStrongPtr());
+            this->setScene    (this->creator_->scene_    .createStrongPtr(), this->creator_->sceneID_);
+            this->setGametype (this->creator_->gametype_ .createStrongPtr());
+            this->setLevel    (this->creator_->level_    .createStrongPtr());
         }
         else
         {

Modified: code/branches/core7/src/libraries/core/BaseObject.h
===================================================================
--- code/branches/core7/src/libraries/core/BaseObject.h	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/libraries/core/BaseObject.h	2015-09-09 14:10:21 UTC (rev 10578)
@@ -64,6 +64,23 @@
         template <class T> friend class XMLPortClassParamContainer;
 
         public:
+            template <class T>
+            class StrongOrWeakPtr
+            {
+                public:
+                    inline StrongOrWeakPtr();
+                    inline StrongOrWeakPtr(const StrongPtr<T>& ptr);
+                    inline StrongOrWeakPtr(const WeakPtr<T>& ptr);
+
+                    inline T* get() const;
+                    inline StrongPtr<T> createStrongPtr() const;
+
+                private:
+                    StrongPtr<T> strongPtr_;
+                    WeakPtr<T> weakPtr_;
+            };
+
+        public:
             BaseObject(Context* context);
             virtual ~BaseObject();
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
@@ -137,21 +154,21 @@
             inline const std::set<Template*>& getTemplates() const
                 { return this->templates_; }
 
-            inline void setNamespace(const StrongPtr<Namespace>& ns) { this->namespace_ = ns; }
-            inline Namespace* getNamespace() const { return this->namespace_; }
+            inline void setNamespace(const StrongOrWeakPtr<Namespace>& ns) { this->namespace_ = ns; }
+            inline Namespace* getNamespace() const { return this->namespace_.get(); }
 
             inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
             inline BaseObject* getCreator() const { return this->creator_; }
 
-            inline void setScene(const StrongPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
-            inline Scene* getScene() const { return this->scene_; }
+            inline void setScene(const StrongOrWeakPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
+            inline Scene* getScene() const { return this->scene_.get(); }
             inline virtual uint32_t getSceneID() const { return this->sceneID_; }
 
-            inline void setGametype(const StrongPtr<Gametype>& gametype) { this->gametype_ = gametype; }
-            inline Gametype* getGametype() const { return this->gametype_; }
+            inline void setGametype(const StrongOrWeakPtr<Gametype>& gametype) { this->gametype_ = gametype; }
+            inline Gametype* getGametype() const { return this->gametype_.get(); }
 
-            inline void setLevel(const StrongPtr<Level>& level) { this->level_ = level; }
-            inline Level* getLevel() const { return this->level_; }
+            inline void setLevel(const StrongOrWeakPtr<Level>& level) { this->level_ = level; }
+            inline Level* getLevel() const { return this->level_.get(); }
 
             void addEventSource(BaseObject* source, const std::string& state);
             void removeEventSource(BaseObject* source);
@@ -198,18 +215,18 @@
             Template* getTemplate(unsigned int index) const;
             void registerEventStates();
 
-            bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
-            const XMLFile*         file_;                      //!< The XMLFile that loaded this object
-            Element*               lastLoadedXMLElement_;      //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
+            bool                       bInitialized_;          //!< True if the object was initialized (passed the object registration)
+            const XMLFile*             file_;                  //!< The XMLFile that loaded this object
+            Element*                   lastLoadedXMLElement_;  //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
             std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
-            std::string            loaderIndentation_;         //!< Indentation of the debug output in the Loader
-            StrongPtr<Namespace>   namespace_;
-            BaseObject*            creator_;
-            StrongPtr<Scene>       scene_;
-            uint32_t               sceneID_;
-            StrongPtr<Gametype>    gametype_;
-            StrongPtr<Level>       level_;
-            std::set<Template*>    templates_;
+            std::string                loaderIndentation_;     //!< Indentation of the debug output in the Loader
+            StrongOrWeakPtr<Namespace> namespace_;
+            BaseObject*                creator_;
+            StrongOrWeakPtr<Scene>     scene_;
+            uint32_t                   sceneID_;
+            StrongOrWeakPtr<Gametype>  gametype_;
+            StrongOrWeakPtr<Level>     level_;
+            std::set<Template*>        templates_;
 
             std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
             std::set<BaseObject*>               eventListeners_;         //!< List of objects which listen to the events of this object
@@ -223,6 +240,41 @@
     SUPER_FUNCTION(3, BaseObject, changedVisibility, false);
     SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
     SUPER_FUNCTION(8, BaseObject, changedName, false);
+
+    template <class T>
+    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr()
+    {
+    }
+
+    template <class T>
+    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const StrongPtr<T>& ptr) : strongPtr_(ptr)
+    {
+    }
+
+    template <class T>
+    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const WeakPtr<T>& ptr) : weakPtr_(ptr)
+    {
+    }
+
+    template <class T>
+    T* BaseObject::StrongOrWeakPtr<T>::get() const
+    {
+        if (this->strongPtr_)
+            return this->strongPtr_;
+        else if (this->weakPtr_)
+            return this->weakPtr_;
+        else
+            return NULL;
+    }
+
+    template <class T>
+    StrongPtr<T> BaseObject::StrongOrWeakPtr<T>::createStrongPtr() const
+    {
+        if (this->strongPtr_)
+            return this->strongPtr_; // creates a copy
+        else
+            return this->weakPtr_; // converts automatically to StrongPtr
+    }
 }
 
 #endif /* _BaseObject_H__ */

Modified: code/branches/core7/src/libraries/core/Namespace.cc
===================================================================
--- code/branches/core7/src/libraries/core/Namespace.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/libraries/core/Namespace.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -46,7 +46,7 @@
     {
         RegisterObject(Namespace);
 
-        this->setNamespace(StrongPtr<Namespace>(this, false));
+        this->setNamespace(WeakPtr<Namespace>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
     }
 
     Namespace::~Namespace()

Modified: code/branches/core7/src/libraries/core/object/StrongPtr.h
===================================================================
--- code/branches/core7/src/libraries/core/object/StrongPtr.h	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/libraries/core/object/StrongPtr.h	2015-09-09 14:10:21 UTC (rev 10578)
@@ -141,10 +141,10 @@
             {
             }
 
-            /// Constructor: Initializes the strong pointer with a pointer to an object. @param pointer The pointer @param bAddRef If true, the reference counter is increased. Don't set this to false unless you know exactly what you're doing! (for example to avoid circular references if the @c this pointer of the possessing object is stored)
-            inline StrongPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
+            /// Constructor: Initializes the strong pointer with a pointer to an object. @param pointer The pointer
+            inline StrongPtr(T* pointer) : pointer_(pointer), base_(pointer)
             {
-                if (this->base_ && bAddRef)
+                if (this->base_)
                     this->base_->incrementReferenceCount();
             }
 

Modified: code/branches/core7/src/orxonox/Level.cc
===================================================================
--- code/branches/core7/src/orxonox/Level.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/orxonox/Level.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -48,7 +48,7 @@
     {
         RegisterObject(Level);
 
-        this->setLevel(StrongPtr<Level>(this, false));
+        this->setLevel(WeakPtr<Level>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
 
         this->registerVariables();
         this->xmlfilename_ = this->getFilename();
@@ -122,8 +122,8 @@
 
         Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this));
 
-        rootgametype->setLevel(NULL); // avoid circular references
-        this->setGametype(rootgametype);
+        rootgametype->setLevel(StrongPtr<Level>(NULL)); // avoid circular references
+        this->setGametype(StrongPtr<Gametype>(rootgametype));
         rootgametype->init(); // call init() AFTER the gametype was set
 
         if (LevelManager::exists())

Modified: code/branches/core7/src/orxonox/Scene.cc
===================================================================
--- code/branches/core7/src/orxonox/Scene.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/orxonox/Scene.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -61,7 +61,7 @@
     {
         RegisterObject(Scene);
 
-        this->setScene(StrongPtr<Scene>(this, false), this->getObjectID());
+        this->setScene(WeakPtr<Scene>(this), this->getObjectID()); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
 
         this->bShadows_ = true;
         this->bDebugDrawPhysics_ = false;

Modified: code/branches/core7/src/orxonox/gametypes/Gametype.cc
===================================================================
--- code/branches/core7/src/orxonox/gametypes/Gametype.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/orxonox/gametypes/Gametype.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -60,7 +60,7 @@
     {
         RegisterObject(Gametype);
 
-        this->setGametype(StrongPtr<Gametype>(this, false));
+        this->setGametype(WeakPtr<Gametype>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
 
         this->gtinfo_ = new GametypeInfo(context);
 

Modified: code/branches/core7/src/orxonox/infos/PlayerInfo.cc
===================================================================
--- code/branches/core7/src/orxonox/infos/PlayerInfo.cc	2015-09-09 13:55:26 UTC (rev 10577)
+++ code/branches/core7/src/orxonox/infos/PlayerInfo.cc	2015-09-09 14:10:21 UTC (rev 10578)
@@ -97,7 +97,7 @@
     void PlayerInfo::switchGametype(Gametype* gametype)
     {
         Gametype* oldGametype = this->getGametype();
-        this->setGametype(gametype);
+        this->setGametype(StrongPtr<Gametype>(gametype));
         Gametype* newGametype = this->getGametype();
 
 




More information about the Orxonox-commit mailing list