Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 9, 2015, 4:10:21 PM (9 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')

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.