• <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

            常用鏈接

            統(tǒng)計(jì)

            software

            最新評(píng)論

            智能指針的代碼實(shí)例

            前段時(shí)間,寫了一點(diǎn)關(guān)于智能指針的東西,有讀者反映沒(méi)有代碼比較難懂.現(xiàn)給出源碼,并稍微加以解釋.

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

                typedef T element_type;

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

                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;
                }

            //  通過(guò)智能指針獲取所指對(duì)象的引用.

                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)               //智能指針的構(gòu)造函數(shù)
                {
                    this->_ptr = p;

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();         //在構(gòu)造函數(shù)中增加所指對(duì)象的引用計(jì)數(shù)
                    }
                }

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

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();   //在構(gòu)造函數(shù)中增加所指對(duì)象的引用計(jì)數(shù)
                    }
                }

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

                    if(this->_ptr)
                    {
                        this->_ptr->__incRef();    //在構(gòu)造函數(shù)中增加所指對(duì)象的引用計(jì)數(shù)
                    }
                }

                ~Handle()
                {
                    if(this->_ptr)
                    {
                        this->_ptr->__decRef();      //在析構(gòu)函數(shù)中減少所指對(duì)象的引用計(jì)數(shù)
                    }
                }

            // 重載=操作符, 要注意所有權(quán) (即,對(duì)原實(shí)例的處理).

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

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

                        if(ptr)
                        {
                            ptr->__decRef();   //減少原來(lái)所指對(duì)象的引用計(jì)數(shù)
                        }
                    }
                    return *this;
                }

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

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

                        if(ptr)
                        {
                            ptr->__decRef();      //減少原來(lái)所指對(duì)象的引用計(jì)數(shù)
                        }
                    }
                    return *this;
                }

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

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

                        if(ptr)
                        {
                            ptr->__decRef();            //減少原來(lái)所指對(duì)象的引用計(jì)數(shù)
                        }
                    }
                    return *this;
                }

             跟智能指針配合使用的對(duì)象.要能夠跟指針智能配合使用,這些對(duì)象應(yīng)該是從下列類的派生類的實(shí)例.
            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)               // 如果引用計(jì)數(shù)為0,則摧毀對(duì)象本身.
                    {
                        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) 評(píng)論(6)  編輯 收藏 引用

            評(píng)論

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

            ICE中的實(shí)現(xiàn)吧,這個(gè)并沒(méi)有解決循環(huán)引用的問(wèn)題。所以他還有一個(gè)GCShared   回復(fù)  更多評(píng)論   

            # re: 智能指針的代碼實(shí)例[未登錄](méi) 2009-03-09 18:01 martin_yahoo

            u r right. These souce code is abstracted from ICE.  回復(fù)  更多評(píng)論   

            # re: 智能指針的代碼實(shí)例 2009-03-11 09:45 夢(mèng)在天涯

            mark  回復(fù)  更多評(píng)論   

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

            一個(gè)句柄類而已  回復(fù)  更多評(píng)論   

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

            這是哪個(gè)開源項(xiàng)目中的啊?  回復(fù)  更多評(píng)論   

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

            模仿寫一下  回復(fù)  更多評(píng)論   


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            老色鬼久久亚洲AV综合| 久久天天躁狠狠躁夜夜av浪潮| 久久国产精品波多野结衣AV| 久久91综合国产91久久精品| 亚洲精品高清久久| 久久五月精品中文字幕| 国内精品人妻无码久久久影院导航| 伊人久久综合精品无码AV专区 | 国产69精品久久久久777| 久久婷婷综合中文字幕| 久久久久国产视频电影| 久久无码AV中文出轨人妻| 97久久超碰国产精品旧版| 久久久久国产精品三级网| 精品久久人妻av中文字幕| 久久久精品国产Sm最大网站| 亚洲国产精品无码久久SM| 国产精品欧美亚洲韩国日本久久| 久久国产精品二国产精品| 久久精品国产网红主播| 一本色综合久久| 久久免费高清视频| 午夜精品久久久久久久| 亚洲精品乱码久久久久久蜜桃| 久久精品亚洲日本波多野结衣| 久久国产成人亚洲精品影院| 久久男人Av资源网站无码软件| 久久精品一区二区影院| 久久精品人人做人人爽电影| 久久婷婷五月综合国产尤物app| 精品久久久久久国产三级| 99国产欧美精品久久久蜜芽| 精产国品久久一二三产区区别| 久久久青草久久久青草| 国产精品一久久香蕉国产线看 | 三上悠亚久久精品| 精品久久久久久久国产潘金莲 | 久久亚洲国产最新网站| 亚洲国产小视频精品久久久三级| 国内精品伊人久久久久影院对白| 国产精品一区二区久久精品|