Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10578


Ignore:
Timestamp:
Sep 9, 2015, 4:10:21 PM (5 years ago)
Author:
landauf
Message:

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')

Location:
code/branches/core7/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core7/src/libraries/core/BaseObject.cc

    r10576 r10578  
    7777        {
    7878            this->setFile(this->creator_->getFile());
    79             this->setNamespace(this->creator_->namespace_);
    80             this->setScene(this->creator_->scene_, this->creator_->sceneID_);
    81             this->setGametype(this->creator_->gametype_);
    82             this->setLevel(this->creator_->level_);
     79
     80            // store strong-pointers on all four base objects by default (can be overwritten with weak-ptr after the constructor if necessary)
     81            this->setNamespace(this->creator_->namespace_.createStrongPtr());
     82            this->setScene    (this->creator_->scene_    .createStrongPtr(), this->creator_->sceneID_);
     83            this->setGametype (this->creator_->gametype_ .createStrongPtr());
     84            this->setLevel    (this->creator_->level_    .createStrongPtr());
    8385        }
    8486        else
  • code/branches/core7/src/libraries/core/BaseObject.h

    r10576 r10578  
    6363    {
    6464        template <class T> friend class XMLPortClassParamContainer;
     65
     66        public:
     67            template <class T>
     68            class StrongOrWeakPtr
     69            {
     70                public:
     71                    inline StrongOrWeakPtr();
     72                    inline StrongOrWeakPtr(const StrongPtr<T>& ptr);
     73                    inline StrongOrWeakPtr(const WeakPtr<T>& ptr);
     74
     75                    inline T* get() const;
     76                    inline StrongPtr<T> createStrongPtr() const;
     77
     78                private:
     79                    StrongPtr<T> strongPtr_;
     80                    WeakPtr<T> weakPtr_;
     81            };
    6582
    6683        public:
     
    138155                { return this->templates_; }
    139156
    140             inline void setNamespace(const StrongPtr<Namespace>& ns) { this->namespace_ = ns; }
    141             inline Namespace* getNamespace() const { return this->namespace_; }
     157            inline void setNamespace(const StrongOrWeakPtr<Namespace>& ns) { this->namespace_ = ns; }
     158            inline Namespace* getNamespace() const { return this->namespace_.get(); }
    142159
    143160            inline void setCreator(BaseObject* creator) { this->creator_ = creator; }
    144161            inline BaseObject* getCreator() const { return this->creator_; }
    145162
    146             inline void setScene(const StrongPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
    147             inline Scene* getScene() const { return this->scene_; }
     163            inline void setScene(const StrongOrWeakPtr<Scene>& scene, uint32_t sceneID) { this->scene_ = scene; this->sceneID_=sceneID; }
     164            inline Scene* getScene() const { return this->scene_.get(); }
    148165            inline virtual uint32_t getSceneID() const { return this->sceneID_; }
    149166
    150             inline void setGametype(const StrongPtr<Gametype>& gametype) { this->gametype_ = gametype; }
    151             inline Gametype* getGametype() const { return this->gametype_; }
    152 
    153             inline void setLevel(const StrongPtr<Level>& level) { this->level_ = level; }
    154             inline Level* getLevel() const { return this->level_; }
     167            inline void setGametype(const StrongOrWeakPtr<Gametype>& gametype) { this->gametype_ = gametype; }
     168            inline Gametype* getGametype() const { return this->gametype_.get(); }
     169
     170            inline void setLevel(const StrongOrWeakPtr<Level>& level) { this->level_ = level; }
     171            inline Level* getLevel() const { return this->level_.get(); }
    155172
    156173            void addEventSource(BaseObject* source, const std::string& state);
     
    199216            void registerEventStates();
    200217
    201             bool                   bInitialized_;              //!< True if the object was initialized (passed the object registration)
    202             const XMLFile*         file_;                      //!< The XMLFile that loaded this object
    203             Element*               lastLoadedXMLElement_;      //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
     218            bool                       bInitialized_;          //!< True if the object was initialized (passed the object registration)
     219            const XMLFile*             file_;                  //!< The XMLFile that loaded this object
     220            Element*                   lastLoadedXMLElement_;  //!< Non 0 if the TinyXML attributes have already been copied to our own lowercase map
    204221            std::map<std::string, std::string> xmlAttributes_; //!< Lowercase XML attributes
    205             std::string            loaderIndentation_;         //!< Indentation of the debug output in the Loader
    206             StrongPtr<Namespace>  namespace_;
    207             BaseObject*            creator_;
    208             StrongPtr<Scene>       scene_;
    209             uint32_t               sceneID_;
    210             StrongPtr<Gametype>    gametype_;
    211             StrongPtr<Level>       level_;
    212             std::set<Template*>    templates_;
     222            std::string                loaderIndentation_;     //!< Indentation of the debug output in the Loader
     223            StrongOrWeakPtr<Namespace> namespace_;
     224            BaseObject*                creator_;
     225            StrongOrWeakPtr<Scene>     scene_;
     226            uint32_t                   sceneID_;
     227            StrongOrWeakPtr<Gametype>  gametype_;
     228            StrongOrWeakPtr<Level>     level_;
     229            std::set<Template*>        templates_;
    213230
    214231            std::map<BaseObject*, std::string>  eventSources_;           //!< List of objects which send events to this object, mapped to the state which they affect
     
    224241    SUPER_FUNCTION(4, BaseObject, XMLEventPort, false);
    225242    SUPER_FUNCTION(8, BaseObject, changedName, false);
     243
     244    template <class T>
     245    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr()
     246    {
     247    }
     248
     249    template <class T>
     250    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const StrongPtr<T>& ptr) : strongPtr_(ptr)
     251    {
     252    }
     253
     254    template <class T>
     255    BaseObject::StrongOrWeakPtr<T>::StrongOrWeakPtr(const WeakPtr<T>& ptr) : weakPtr_(ptr)
     256    {
     257    }
     258
     259    template <class T>
     260    T* BaseObject::StrongOrWeakPtr<T>::get() const
     261    {
     262        if (this->strongPtr_)
     263            return this->strongPtr_;
     264        else if (this->weakPtr_)
     265            return this->weakPtr_;
     266        else
     267            return NULL;
     268    }
     269
     270    template <class T>
     271    StrongPtr<T> BaseObject::StrongOrWeakPtr<T>::createStrongPtr() const
     272    {
     273        if (this->strongPtr_)
     274            return this->strongPtr_; // creates a copy
     275        else
     276            return this->weakPtr_; // converts automatically to StrongPtr
     277    }
    226278}
    227279
  • code/branches/core7/src/libraries/core/Namespace.cc

    r10562 r10578  
    4747        RegisterObject(Namespace);
    4848
    49         this->setNamespace(StrongPtr<Namespace>(this, false));
     49        this->setNamespace(WeakPtr<Namespace>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
    5050    }
    5151
  • code/branches/core7/src/libraries/core/object/StrongPtr.h

    r10555 r10578  
    142142            }
    143143
    144             /// 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)
    145             inline StrongPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)
    146             {
    147                 if (this->base_ && bAddRef)
     144            /// Constructor: Initializes the strong pointer with a pointer to an object. @param pointer The pointer
     145            inline StrongPtr(T* pointer) : pointer_(pointer), base_(pointer)
     146            {
     147                if (this->base_)
    148148                    this->base_->incrementReferenceCount();
    149149            }
  • code/branches/core7/src/orxonox/Level.cc

    r10576 r10578  
    4949        RegisterObject(Level);
    5050
    51         this->setLevel(StrongPtr<Level>(this, false));
     51        this->setLevel(WeakPtr<Level>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
    5252
    5353        this->registerVariables();
     
    123123        Gametype* rootgametype = orxonox_cast<Gametype*>(identifier->fabricate(this));
    124124
    125         rootgametype->setLevel(NULL); // avoid circular references
    126         this->setGametype(rootgametype);
     125        rootgametype->setLevel(StrongPtr<Level>(NULL)); // avoid circular references
     126        this->setGametype(StrongPtr<Gametype>(rootgametype));
    127127        rootgametype->init(); // call init() AFTER the gametype was set
    128128
  • code/branches/core7/src/orxonox/Scene.cc

    r10571 r10578  
    6262        RegisterObject(Scene);
    6363
    64         this->setScene(StrongPtr<Scene>(this, false), this->getObjectID());
     64        this->setScene(WeakPtr<Scene>(this), this->getObjectID()); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
    6565
    6666        this->bShadows_ = true;
  • code/branches/core7/src/orxonox/gametypes/Gametype.cc

    r10575 r10578  
    6161        RegisterObject(Gametype);
    6262
    63         this->setGametype(StrongPtr<Gametype>(this, false));
     63        this->setGametype(WeakPtr<Gametype>(this)); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
    6464
    6565        this->gtinfo_ = new GametypeInfo(context);
  • code/branches/core7/src/orxonox/infos/PlayerInfo.cc

    r10576 r10578  
    9898    {
    9999        Gametype* oldGametype = this->getGametype();
    100         this->setGametype(gametype);
     100        this->setGametype(StrongPtr<Gametype>(gametype));
    101101        Gametype* newGametype = this->getGametype();
    102102
Note: See TracChangeset for help on using the changeset viewer.