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

            導航

            統計

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久久波多野结衣高潮| 久久精品亚洲精品国产色婷| 久久免费视频一区| 中文字幕无码精品亚洲资源网久久| 久久综合亚洲色HEZYO社区| 色诱久久久久综合网ywww| 精品久久久久久亚洲| 四虎影视久久久免费观看| 一本色道久久综合狠狠躁| 久久AⅤ人妻少妇嫩草影院| 香蕉久久av一区二区三区| 91久久九九无码成人网站| 日韩人妻无码精品久久久不卡| 91精品久久久久久无码| 亚洲色婷婷综合久久| 久久人人爽人人爽AV片| 2021久久国自产拍精品| 精品久久久无码人妻中文字幕| 国产L精品国产亚洲区久久| 国产午夜免费高清久久影院 | 国内精品久久久久久久久电影网 | 久久婷婷激情综合色综合俺也去| 91精品国产91热久久久久福利| 99久久精品免费看国产一区二区三区| 国产一区二区精品久久岳| 久久综合九色综合网站| 国产亚洲精品久久久久秋霞| 久久亚洲国产成人影院网站| 国产精品va久久久久久久| 国产精品99久久久久久人| 伊人久久综合成人网| 亚洲人成伊人成综合网久久久| 一本久久免费视频| 人妻无码精品久久亚瑟影视| 国产精品欧美久久久久天天影视| 99精品久久久久中文字幕| 久久免费精品一区二区| 色综合久久最新中文字幕| 国产精品伦理久久久久久| 久久久久久A亚洲欧洲AV冫| 久久精品免费网站网|