• <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
             *  智能指針, 不提供引用計數功能, 離開作用域自動釋放指針
             *  非線程安全
             
            */
            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
             *  智能指針, 不提供引用計數功能, 需要使用者繼承 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
             *  智能指針, 提供引用計數功能
             *  非線程安全
             
            */
            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 空明流轉

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

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

            work without tr1?  回復  更多評論   

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

            @空明流轉
            lz這么做的目的就是準時MC,這種MC+NC貼都會準時出現的  回復  更多評論   

            導航

            統計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久99国产精品二区不卡| 亚洲欧美日韩久久精品| 久久精品欧美日韩精品| 国产精品99精品久久免费| 国产精品成人99久久久久| 久久久久九国产精品| 一本一道久久a久久精品综合 | 久久亚洲精品国产精品婷婷| 色偷偷偷久久伊人大杳蕉| 狠狠色丁香久久婷婷综合五月| 国产高清国内精品福利99久久| 久久久久久久久66精品片| 久久香蕉综合色一综合色88| 中文字幕亚洲综合久久菠萝蜜| 国内精品伊人久久久久av一坑| 久久久久亚洲AV无码专区网站 | 久久精品成人免费看| 亚洲国产成人乱码精品女人久久久不卡| 97精品国产97久久久久久免费| 国产99久久九九精品无码| 综合人妻久久一区二区精品| 亚洲国产天堂久久综合网站| 无码伊人66久久大杳蕉网站谷歌| 日韩十八禁一区二区久久| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久最新免费视频| 国产亚洲成人久久| 精品久久8x国产免费观看| 伊人久久无码中文字幕| 亚洲国产精品成人AV无码久久综合影院| 久久精品中文闷骚内射| 久久久久免费精品国产| 精品久久久无码中文字幕| 69SEX久久精品国产麻豆| 欧洲精品久久久av无码电影| 一本久道久久综合狠狠躁AV| 99久久免费国产精品| 99久久伊人精品综合观看| 香港aa三级久久三级| 久久精品国产福利国产秒| 欧美亚洲国产精品久久蜜芽|