• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            martin

            thinking

            常用鏈接

            統計

            software

            最新評論

            智能指針的代碼實例

            前段時間,寫了一點關于智能指針的東西,有讀者反映沒有代碼比較難懂.現給出源碼,并稍微加以解釋.

            智能指針類用到的基類的定義:
            template<typename T>
            class HandleBase
            {
            public:

                typedef T element_type;

                T* get() const
                {
                    return _ptr;
                }
               
               //重載->操作符,返回所指對象的指針.

                T* operator->() const
                {
                    if(!_ptr)
                    {
                        //
                        // We don't throw directly NullHandleException here to
                        // keep the code size of this method to a minimun (the
                        // assembly code for throwing an exception is much bigger
                        // than just a function call). This maximises the chances
                        // of inlining by compiler optimization.
                        //
                        throwNullHandleException(__FILE__, __LINE__);
                    }

                    return _ptr;
                }

            //  通過智能指針獲取所指對象的引用.

                T& operator*() const
                {
                    if(!_ptr)
                    {
                        //
                        // We don't throw directly NullHandleException here to
                        // keep the code size of this method to a minimun (the
                        // assembly code for throwing an exception is much bigger
                        // than just a function call). This maximises the chances
                        // of inlining by compiler optimization.
                        //
                        throwNullHandleException(__FILE__, __LINE__);
                    }

                    return *_ptr;
                }

                operator bool() const
                {
                    return _ptr ? true : false;
                }

                void swap(HandleBase& other)
                {
                    std::swap(_ptr, other._ptr);
                }

                T* _ptr;

            private:

                void throwNullHandleException(const char *, int) const;
            };

            ......


            // 智能指針類定義

            template<typename T>
            class Handle : public HandleBase<T>
            {
            public:

                Handle(T* p = 0)               //智能指針的構造函數
                {
                    this->_ptr = p;

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();         //在構造函數中增加所指對象的引用計數
                    }
                }

                template<typename Y>                  //拷貝構造函數
                Handle(const Handle<Y>& r)
                {
                    this->_ptr = r._ptr;

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();   //在構造函數中增加所指對象的引用計數
                    }
                }

                Handle(const Handle& r)         //拷貝構造函數
                {
                    this->_ptr = r._ptr;

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();    //在構造函數中增加所指對象的引用計數
                    }
                }

                ~Handle()
                {
                    if(this->_ptr)
                    {
                        this->_ptr->__decRef();      //在析構函數中減少所指對象的引用計數
                    }
                }

            // 重載=操作符, 要注意所有權 (即,對原實例的處理).

                Handle& operator=(T* p)         
                {
                    if(this->_ptr != p)
                    {
                        if(p)
                        {
                            p->__incRef();      //增加新指對象的引用計數
                        }

                        T* ptr = this->_ptr;
                        this->_ptr = p;

                        if(ptr)
                        {
                            ptr->__decRef();   //減少原來所指對象的引用計數
                        }
                    }
                    return *this;
                }

                template<typename Y>
                Handle& operator=(const Handle<Y>& r)
                {
                    if(this->_ptr != r._ptr)
                    {
                        if(r._ptr)
                        {
                            r._ptr->__incRef();   //增加新指對象的引用計數
                        }

                        T* ptr = this->_ptr;
                        this->_ptr = r._ptr;

                        if(ptr)
                        {
                            ptr->__decRef();      //減少原來所指對象的引用計數
                        }
                    }
                    return *this;
                }

                Handle& operator=(const Handle& r)
                {
                    if(this->_ptr != r._ptr)
                    {
                        if(r._ptr)
                        {
                            r._ptr->__incRef();            //增加新指對象的引用計數
                        }

                        T* ptr = this->_ptr;
                        this->_ptr = r._ptr;

                        if(ptr)
                        {
                            ptr->__decRef();            //減少原來所指對象的引用計數
                        }
                    }
                    return *this;
                }

             跟智能指針配合使用的對象.要能夠跟指針智能配合使用,這些對象應該是從下列類的派生類的實例.
            class SimpleShared
            {
            public:

                SimpleShared();
                SimpleShared(const SimpleShared&);

                virtual ~SimpleShared()
                {
                }

                SimpleShared& operator=(const SimpleShared&)
                {
                    return *this;
                }

                void __incRef()
                {
                    assert(_ref >= 0);
                    ++_ref;
                }

                void __decRef()
                {
                    assert(_ref > 0);
                    if(--_ref == 0)               // 如果引用計數為0,則摧毀對象本身.
                    {
                        if(!_noDelete)
                        {
                            _noDelete = true;
                            delete this;
                        }
                    }
                }

                int __getRef() const
                {
                    return _ref;
                }

                void __setNoDelete(bool b)
                {
                    _noDelete = b;
                }

            private:

                int _ref;
                bool _noDelete;
            };

            class Shared
            {
            public:

                Shared();
                Shared(const Shared&);

                virtual ~Shared()
                {
                }

                Shared& operator=(const Shared&)
                {
                    return *this;
                }

                virtual void __incRef();
                virtual void __decRef();
                virtual int __getRef() const;
                virtual void __setNoDelete(bool);

            protected:

            #if defined(_WIN32)
                LONG _ref;
            #elif defined(ICE_HAS_ATOMIC_FUNCTIONS)
                volatile int _ref;
            #else
                int _ref;
                Mutex _mutex;
            #endif
                bool _noDelete;
            };

            posted on 2009-03-09 16:07 martin_yahoo 閱讀(2237) 評論(6)  編輯 收藏 引用

            評論

            # re: 智能指針的代碼實例 2009-03-09 17:18 eXile

            ICE中的實現吧,這個并沒有解決循環引用的問題。所以他還有一個GCShared   回復  更多評論   

            # re: 智能指針的代碼實例[未登錄] 2009-03-09 18:01 martin_yahoo

            u r right. These souce code is abstracted from ICE.  回復  更多評論   

            # re: 智能指針的代碼實例 2009-03-11 09:45 夢在天涯

            mark  回復  更多評論   

            # re: 智能指針的代碼實例 2009-05-05 09:02 brightcoder

            一個句柄類而已  回復  更多評論   

            # re: 智能指針的代碼實例 2013-04-26 23:58 WWW

            這是哪個開源項目中的啊?  回復  更多評論   

            # re: 智能指針的代碼實例 2013-06-08 17:13 tb

            模仿寫一下  回復  更多評論   

            国产成人精品久久免费动漫| 亚洲综合熟女久久久30p| 久久久久久午夜成人影院| 浪潮AV色综合久久天堂| 热久久国产精品| 日本加勒比久久精品| 性做久久久久久久| 久久www免费人成看国产片| 亚洲人成无码网站久久99热国产 | 国产精品久久久久乳精品爆 | 亚洲欧美成人综合久久久| 嫩草影院久久99| 亚洲国产精品无码久久久不卡| 久久青青草原综合伊人| 波多野结衣久久| 国产精品九九久久免费视频 | 亚洲伊人久久精品影院| 99热都是精品久久久久久| 伊人久久精品无码av一区| 久久电影网| 免费观看成人久久网免费观看| 国产亚洲精品久久久久秋霞 | 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品亚洲欧美日韩久久| 热re99久久精品国99热| 性做久久久久久久久| 国产成人久久精品麻豆一区 | 亚洲人成精品久久久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久精品无码一区二区三区免费 | 合区精品久久久中文字幕一区| 久久久久免费精品国产| 99精品国产在热久久无毒不卡| 久久无码中文字幕东京热 | 国产精品久久久久国产A级| 成人久久免费网站| 99久久精品免费看国产一区二区三区| 欧美精品福利视频一区二区三区久久久精品 | 欧美亚洲国产精品久久高清| 亚洲精品国精品久久99热| 久久国产视屏|