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

            Kin

            Kin

             

            仿boost接口的智能指針

            // smartpointer.h: interface for the SmartPointer classes.
            //
            //////////////////////////////////////////////////////////////////////

            #if !defined(SMARTPOINTER_H__BD2313DE_E4F7_49CB_BE4C_E25F030A9BA5__INCLUDED_)
            #define SMARTPOINTER_H__BD2313DE_E4F7_49CB_BE4C_E25F030A9BA5__INCLUDED_

            #if _MSC_VER > 1000
            #pragma once
            #endif // _MSC_VER > 1000

            #ifdef BOOL
            #undef BOOL
            #endif
            #include 
            <windows.h>

            #include 
            <algorithm>
            #include 
            <assert.h>

            /**
             *  class: CScopedPtrT
             *  智能指針, 不提供引用計(jì)數(shù)功能, 離開作用域自動(dòng)釋放指針
             *  非線程安全
             
            */
            template
            <class T> 
            class CScopedPtrT 
            {
            public:
                typedef T element_type;
                
                
            explicit CScopedPtrT( T* p = NULL ): m_p( p ) // never throws
                {
                }
                    
                
            ~CScopedPtrT() // never throws
                {
                    
            if ( m_p )
                        delete m_p;
                }
                
                
            void reset(T * p = NULL) // never throws
                {
                    assert( p 
            == NULL || p != m_p ); // catch self-reset errors
                    this_type(p).swap(*this);
                }
                
                T
            & operator*() const // never throws
                {
                    assert( m_p 
            != NULL );
                    
            return *m_p;
                }
                
                T
            * operator->() const // never throws
                {
                    assert( m_p 
            != NULL );
                    
            return m_p;
                }
                
                T
            * get(voidconst // never throws
                {
                    
            return m_p;
                }
                    
                
            void swap(CScopedPtrT & b) // never throws
                {
                    T
            * tmp = b.m_p;
                    b.m_p 
            = m_p;
                    m_p 
            = tmp;
                }

            private:    
                T 
            * m_p; // contained pointer
                typedef CScopedPtrT<T> this_type;
                
                
            // noncopyable
                CScopedPtrT(CScopedPtrT const &);
                CScopedPtrT 
            & operator=(CScopedPtrT const &);
                
                
            void operator==( CScopedPtrT const& ) const;
                
            void operator!=( CScopedPtrT const& ) const;
            };

            class CShared
            {
            public:
                CShared() : m_ref(
            0), m_bNoDelete(false)
                {
                    
                }
                
                CShared(
            const CShared&): m_ref(0), m_bNoDelete(false)
                {
                }
                
                
            virtual ~CShared()
                {
                    
                }
                
                
            void _incRef()
                {
            #if defined(_WIN32)
                    assert(InterlockedExchangeAdd(
            &m_ref, 0>= 0);
                    InterlockedIncrement(
            &m_ref);
            #endif
                }
                
                
            void _decRef()
                {
            #if defined(_WIN32)
                    assert(InterlockedExchangeAdd(
            &m_ref, 0> 0);
                    
            if(InterlockedDecrement(&m_ref) == 0 && !m_bNoDelete)
                    {
                        m_bNoDelete 
            = true;
                        delete 
            this;
                    }
            #endif
                }
                
                
            int _getRef() const
                {
            #if defined(_WIN32)
                    
            return InterlockedExchangeAdd(const_cast<LONG*>(&m_ref), 0);
            #endif
                }
                
                
            void _setNoDelete(bool b)
                {
                    m_bNoDelete 
            = b;
                }
                
            protected:    
            #if defined(_WIN32)
                
            long m_ref;
            #endif
                
            bool m_bNoDelete;
            };


            /**
             *  class: CIntrusivePtrT
             *  智能指針, 不提供引用計(jì)數(shù)功能, 需要使用者繼承 CShared 類
             *  非線程安全
             
            */
            template
            <class T> 
            class CIntrusivePtrT
            {
            private:
                typedef CIntrusivePtrT
            <T> this_type;

            public:

                typedef T element_type;

                CIntrusivePtrT(): m_p( NULL )
                {
                }

                CIntrusivePtrT( T 
            * p, bool add_ref = true ): m_p( p )
                {
                    
            if( m_p != NULL && add_ref ) 
                        
            this->m_p->_incRef();
                }

                CIntrusivePtrT( CIntrusivePtrT
            <T> const & rhs ) : m_p( rhs.get() )
                {
                    
            if( m_p != 0 ) 
                        
            this->m_p->_incRef();
                }

                
            ~CIntrusivePtrT()
                {
                    
            if( m_p != 0 ) 
                        
            this->m_p->_decRef();
                }

                template
            <class U> CIntrusivePtrT & operator=(CIntrusivePtrT<U> const & rhs)
                {
                    this_type(rhs).swap(
            *this);
                    
            return *this;
                }

                CIntrusivePtrT 
            & operator=(CIntrusivePtrT const & rhs)
                {
                    this_type(rhs).swap(
            *this);
                    
            return *this;
                }

                CIntrusivePtrT 
            & operator=(T * rhs)
                {
                    this_type(rhs).swap(
            *this);
                    
            return *this;
                }

                
            void reset()
                {
                    this_type().swap( 
            *this );
                }

                
            void reset( T * rhs )
                {
                    this_type( rhs ).swap( 
            *this );
                }

                T 
            * get() const
                {
                    
            return m_p;
                }

                T 
            & operator*() const
                {
                    assert( m_p 
            != 0 );
                    
            return *m_p;
                }

                T 
            * operator->() const
                {
                    assert( m_p 
            != 0 );
                    
            return m_p;
                }

                
            void swap(CIntrusivePtrT & rhs)
                {
                    T 
            * tmp = m_p;
                    m_p 
            = rhs.m_p;
                    rhs.m_p 
            = tmp;
                }

            private:
                T 
            * m_p; // contained pointer
            };

            /**
             *  class: CSharePtrT
             *  智能指針, 提供引用計(jì)數(shù)功能
             *  非線程安全
             
            */
            template
            <class T> 
            class CSharePtrT 
            {
            public:
                typedef T  element_type;
                typedef T  value_type;
                typedef T
            * pointer;

                CSharePtrT(): m_p(NULL), m_ref(NULL)
                {
                    m_ref 
            = new long();
                    
            *m_ref = 0;
                }

                
            explicit CSharePtrT( T* p ): m_p( p ), m_ref(NULL)
                {
                    m_ref 
            = new long();
                    
            *m_ref = 0;

                    
            if ( m_p != NULL )
                        InterlockedIncrement(m_ref);      
                }

                CSharePtrT( CSharePtrT
            <T> & r ) : m_p( r.m_p ), m_ref( r.m_ref ) // never throws
                {
                    InterlockedIncrement(m_ref);
                }

                
            ~CSharePtrT()
                {
                    
            if ( m_p != NULL )
                    {            
                        
            if( InterlockedDecrement(m_ref) == 0 )
                        {
                            delete m_ref;
                            delete m_p;
                        }
                    }
                }

                
            // assignment
                CSharePtrT & operator=( CSharePtrT & r ) // never throws
                {
                    this_type(r).swap(
            *this);
                    
            return *this;
                }

                
            void reset(void)
                {
                    this_type().swap(
            *this);
                }

                
            void reset(T* p)
                {
                    assert(p 
            == 0 || p != m_p); // catch self-reset errors
                    this_type(p).swap(*this);
                }

                T
            & operator* () const // never throws
                {
                    assert(m_p 
            != 0);
                    
            return *m_p;
                }

                T 
            * operator-> () const // never throws
                {
                    assert(m_p 
            != 0);
                    
            return m_p;
                }

                T 
            * get(voidconst // never throws
                {
                    
            return m_p;
                }

                
            bool unique(voidconst // never throws
                {
                    
            return use_count() == 1;
                }

                
            long use_count(voidconst // never throws
                {
                    
            if ( m_ref != NULL )
                    {
                        
            return *m_ref;
                    }
                    
            else
                    {
                        
            return 0;
                    }        
                }

                
            void swap(CSharePtrT<T> & other) // never throws
                {
                    std::swap(m_p,   other.m_p);
                    std::swap(m_ref, other.m_ref);
                }

            private:
                T
            *       m_p;   // contained pointer
                long*    m_ref; // reference counter

                typedef CSharePtrT
            <T> this_type;
            };

            #endif // !defined(SMARTPOINTER_H__BD2313DE_E4F7_49CB_BE4C_E25F030A9BA5__INCLUDED_)

            posted on 2010-09-05 13:52 kin 閱讀(1694) 評論(3)  編輯 收藏 引用

            評論

            # re: 仿boost接口的智能指針 2010-09-05 15:17 空明流轉(zhuǎn)

            不知道樓主這么做是想干什么。。。  回復(fù)  更多評論   

            # re: 仿boost接口的智能指針 2010-09-05 18:43 Pear

            work without tr1?  回復(fù)  更多評論   

            # re: 仿boost接口的智能指針 2010-09-08 00:13 匿了

            @空明流轉(zhuǎn)
            lz這么做的目的就是準(zhǔn)時(shí)MC,這種MC+NC貼都會(huì)準(zhǔn)時(shí)出現(xiàn)的  回復(fù)  更多評論   

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            人人狠狠综合久久亚洲88| 一本一道久久综合狠狠老| 亚洲国产精品18久久久久久| 欧美亚洲另类久久综合婷婷| 国产 亚洲 欧美 另类 久久| 久久国产精品99精品国产987| 久久精品国产久精国产一老狼| 精品伊人久久久| 99久久这里只精品国产免费| 97精品国产97久久久久久免费 | 久久久久亚洲av综合波多野结衣| 欧美亚洲日本久久精品| 久久毛片一区二区| 亚洲AV无码久久| 国产91色综合久久免费分享| 久久综合九色综合久99| 久久久久亚洲爆乳少妇无| 久久综合九色综合欧美就去吻| 狠狠色丁香婷婷久久综合| 色综合久久无码中文字幕| 久久精品国产一区| 久久丝袜精品中文字幕| 久久九九久精品国产免费直播| 国产精品女同久久久久电影院| 久久99精品九九九久久婷婷| 久久久久久午夜精品| …久久精品99久久香蕉国产 | 91精品国产91久久久久福利| 久久亚洲高清观看| 亚洲欧洲精品成人久久曰影片 | 精品国产99久久久久久麻豆| 成人久久久观看免费毛片| 欧美麻豆久久久久久中文| 国产精品美女久久久久| 色婷婷狠狠久久综合五月| 国产精品久久成人影院| 亚洲精品国产第一综合99久久| 久久精品国产亚洲沈樵| 亚洲国产精品无码久久98| 久久久精品久久久久久| 日本一区精品久久久久影院|