• <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 閱讀(1693) 評論(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貼都會準時出現的  回復  更多評論   

            導航

            統計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色综合色天天久久婷婷基地| 热re99久久精品国99热| 99久久精品久久久久久清纯| 欧美激情精品久久久久久久九九九 | 91精品国产综合久久久久久| 久久精品亚洲日本波多野结衣| 久久久久久狠狠丁香| 99久久做夜夜爱天天做精品| www.久久热.com| 久久人人爽人人爽人人片AV麻烦| 久久九九亚洲精品| 中文字幕无码久久人妻| 99久久99久久精品国产片果冻| 欧美亚洲国产精品久久高清| 亚洲狠狠综合久久| 久久久久亚洲av无码专区喷水| 国产综合精品久久亚洲| 九九99精品久久久久久| 久久久久av无码免费网| 久久se这里只有精品| 99久久婷婷国产一区二区| 久久综合狠狠综合久久| 午夜精品久久久久久久久| 欧美一级久久久久久久大| 国内精品伊人久久久久网站| 久久香综合精品久久伊人| 亚洲午夜久久久久久久久久| 欧美久久天天综合香蕉伊| 九九久久精品国产| 精品久久人人做人人爽综合| 成人资源影音先锋久久资源网| 久久精品国产99国产精品亚洲| 综合久久精品色| 久久精品一本到99热免费| 国产亚洲精久久久久久无码77777| 久久九色综合九色99伊人| 久久精品夜色噜噜亚洲A∨| 久久久无码精品午夜| 伊人色综合九久久天天蜜桃| 久久天天躁狠狠躁夜夜2020一| 久久人人添人人爽添人人片牛牛|