青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

看到ATL中有3個類的代碼比較比較重復,在atlbase.h中,分別是CAutoVectorPtr, CAutoPtr和CAutoStackPtr,他們的功能其實很類似STL中的autoptr, 但是這里因為針對不同的分配對象而用了3個不同的類,其中CAutoVectorPtr是針對數組類型的,CAutoPtr是針對普通的非數組類型,而CAutoStackPtr針對的是_malloca分配的類型,因為最后釋放方式的不同,它這里用了3份代碼來實現。

CAutoVectorPtr:
template< typename T >
class CAutoVectorPtr
{
public:
    CAutoVectorPtr() throw() :
        m_p( NULL )
    {
    }
    CAutoVectorPtr( CAutoVectorPtr< T >& p ) throw()
    {
        m_p = p.Detach();  // Transfer ownership
    }
    explicit CAutoVectorPtr( T* p ) throw() :
        m_p( p )
    {
    }
    ~CAutoVectorPtr() throw()
    {
        Free();
    }

    operator T*() const throw()
    {
        return( m_p );
    }

    CAutoVectorPtr< T >& operator=( CAutoVectorPtr< T >& p ) throw()
    {
        if(*this==p)
        {
            if(m_p == NULL)
            {
                // This branch means both two pointers are NULL, do nothing.
            }
            else if(this!=&p)
            {
                // If this assert fires, it means you attempted to assign one CAutoVectorPtr to another when they both contained 
                
// a pointer to the same underlying vector. This means a bug in your code, since your vector will get 
                
// double-deleted. 
                ATLASSERT(FALSE);

                // For safety, we are going to detach the other CAutoVectorPtr to avoid a double-free. Your code still
                
// has a bug, though.
                p.Detach();
            }
            else
            {
                // Alternatively, this branch means that you are assigning a CAutoVectorPtr to itself, which is
                
// pointless but permissible

                
// nothing to do
            }
        }
        else
        {
            Free();
            Attach( p.Detach() );  // Transfer ownership
        }
        return( *this );
    }

    // basic comparison operators
    bool operator!=(CAutoVectorPtr<T>& p) const
    {
        return !operator==(p);
    }

    bool operator==(CAutoVectorPtr<T>& p) const
    {
        return m_p==p.m_p;
    }

    // Allocate the vector
    bool Allocate( size_t nElements ) throw()
    {
        ATLASSUME( m_p == NULL );
        ATLTRY( m_p = new T[nElements] );
        if( m_p == NULL )
        {
            returnfalse );
        }

        returntrue );
    }
    // Attach to an existing pointer (takes ownership)
    void Attach( T* p ) throw()
    {
        ATLASSUME( m_p == NULL );
        m_p = p;
    }
    // Detach the pointer (releases ownership)
    T* Detach() throw()
    {
        T* p;

        p = m_p;
        m_p = NULL;

        return( p );
    }
    // Delete the vector pointed to, and set the pointer to NULL
    void Free() throw()
    {
        delete[] m_p;
        m_p = NULL;
    }

public:
    T* m_p;
};





CAutoPtr:
template< typename T >
class CAutoPtr
{
public:
    CAutoPtr() throw() :
        m_p( NULL )
    {
    }
    template< typename TSrc >
    CAutoPtr( CAutoPtr< TSrc >& p ) throw()
    {
        m_p = p.Detach();  // Transfer ownership
    }
    CAutoPtr( CAutoPtr< T >& p ) throw()
    {
        m_p = p.Detach();  // Transfer ownership
    }
    explicit CAutoPtr( T* p ) throw() :
        m_p( p )
    {
    }
    ~CAutoPtr() throw()
    {
        Free();
    }

    // Templated version to allow pBase = pDerived
    template< typename TSrc >
    CAutoPtr< T >& operator=( CAutoPtr< TSrc >& p ) throw()
    {
        if(m_p==p.m_p)
        {
            // This means that two CAutoPtrs of two different types had the same m_p in them
            
// which is never correct
            ATLASSERT(FALSE);
        }
        else
        {
            Free();
            Attach( p.Detach() );  // Transfer ownership
        }
        return( *this );
    }
    CAutoPtr< T >& operator=( CAutoPtr< T >& p ) throw()
    {
        if(*this==p)
        {
            if(this!=&p)
            {
                // If this assert fires, it means you attempted to assign one CAutoPtr to another when they both contained 
                
// a pointer to the same underlying object. This means a bug in your code, since your object will get 
                
// double-deleted. 
#ifdef ATL_AUTOPTR_ASSIGNMENT_ASSERT
                ATLASSERT(FALSE);
#endif

                // For safety, we are going to detach the other CAutoPtr to avoid a double-free. Your code still
                
// has a bug, though.
                p.Detach();
            }
            else
            {
                // Alternatively, this branch means that you are assigning a CAutoPtr to itself, which is
                
// pointless but permissible

                
// nothing to do
            }
        }
        else
        {
            Free();
            Attach( p.Detach() );  // Transfer ownership
        }
        return( *this );
    }

    // basic comparison operators
    bool operator!=(CAutoPtr<T>& p) const
    {
        return !operator==(p);
    }

    bool operator==(CAutoPtr<T>& p) const
    {
        return m_p==p.m_p;
    }

    operator T*() const throw()
    {
        return( m_p );
    }
    T* operator->() const throw()
    {
        ATLASSUME( m_p != NULL );
        return( m_p );
    }

    // Attach to an existing pointer (takes ownership)
    void Attach( T* p ) throw()
    {
        ATLASSUME( m_p == NULL );
        m_p = p;
    }
    // Detach the pointer (releases ownership)
    T* Detach() throw()
    {
        T* p;

        p = m_p;
        m_p = NULL;

        return( p );
    }
    // Delete the object pointed to, and set the pointer to NULL
    void Free() throw()
    {
        delete m_p;
        m_p = NULL;
    }

public:
    T* m_p;
};

CAutoStackPtr:
/* Automatic cleanup for _malloca objects */
template< typename T >
class CAutoStackPtr
{
public:
    CAutoStackPtr() throw() :
        m_p( NULL )
    {
    }
    template< typename TSrc >
    CAutoStackPtr( CAutoStackPtr< TSrc >& p ) throw()
    {
        m_p = p.Detach();  // Transfer ownership
    }
    CAutoStackPtr( CAutoStackPtr< T >& p ) throw()
    {
        m_p = p.Detach();  // Transfer ownership
    }
    explicit CAutoStackPtr( T* p ) throw() :
        m_p( p )
    {
    }
    ~CAutoStackPtr() throw()
    {
        Free();
    }

    // Templated version to allow pBase = pDerived
    template< typename TSrc >
    CAutoStackPtr< T >& operator=( CAutoStackPtr< TSrc >& p ) throw()
    {
        if(m_p==p.m_p)
        {
            // This means that two CAutoPtrs of two different types had the same m_p in them
            
// which is never correct
            ATLASSERT(FALSE);
        }
        else
        {
            Free();
            Attach( p.Detach() );  // Transfer ownership
        }
        return( *this );
    }
    CAutoStackPtr< T >& operator=( CAutoStackPtr< T >& p ) throw()
    {
        if(*this==p)
        {
            if(this!=&p)
            {
                // If this assert fires, it means you attempted to assign one CAutoPtr to another when they both contained 
                
// a pointer to the same underlying object. This means a bug in your code, since your object will get 
                
// double-deleted. 
                ATLASSERT(FALSE);

                // For safety, we are going to detach the other CAutoPtr to avoid a double-free. Your code still
                
// has a bug, though.
                p.Detach();
            }
            else
            {
                // Alternatively, this branch means that you are assigning a CAutoPtr to itself, which is
                
// pointless but permissible

                
// nothing to do
            }
        }
        else
        {
            Free();
            Attach( p.Detach() );  // Transfer ownership
        }
        return( *this );
    }

    // basic comparison operators
    bool operator!=(CAutoStackPtr<T>& p) const
    {
        return !operator==(p);
    }

    bool operator==(CAutoStackPtr<T>& p) const
    {
        return m_p==p.m_p;
    }

    operator T*() const throw()
    {
        return( m_p );
    }
    T* operator->() const throw()
    {
        ATLASSUME( m_p != NULL );
        return( m_p );
    }

    // Attach to an existing pointer (takes ownership)
    void Attach( T* p ) throw()
    {
        ATLASSUME( m_p == NULL );
        m_p = p;
    }
    // Detach the pointer (releases ownership)
    T* Detach() throw()
    {
        T* p;

        p = m_p;
        m_p = NULL;

        return( p );
    }
    // Delete the object pointed to, and set the pointer to NULL
    void Free() throw()
    {
        /* Note: _freea only actually does anything if m_p was heap allocated
           If m_p was from the stack, it wouldn't be possible to actually free it here
           [wrong function] unless we got inlined. But really all we do if m_p is 
           stack-based is ignore it and let its alloca storage disappear at the end
           of the outer function.
        
*/
        _freea(m_p);
        m_p = NULL;
    }

public:
    T* m_p;
};

可以看到上面代碼明顯非常重復,不知道ATL這樣寫是不是歷史原因,我們下面嘗試對它進行重構。

可以看到其實他們只是最終釋放(Free)的時候稍微有些差別,我們明顯可以把寫差別提取出來,作為一個釋放的Policy。

struct DeleteFunctor
{
    template<typename T> static void Release(T* p) { delete p; }
};

struct DeleteArrayFunctor
{
    template<typename T> static void Release(T* p) { delete []p; }
};

struct DeleteStackFunctor
{
    template<typename T> static void Release(T* p) { _freea p; }
};

然后我們把上面的各種釋放行為作為一個模板參數傳進去就可以了,代碼如下:
template< typename T, typename ReleasePolicy>
class CAutoReleasePtr
{
public:
    CAutoReleasePtr() throw() :
      m_p( NULL )
      {
      }
      template< typename TSrc >
      CAutoReleasePtr( CAutoReleasePtr< TSrc >& p ) throw()
      {
          m_p = p.Detach();  // Transfer ownership
      }
      CAutoReleasePtr( CAutoReleasePtr< T >& p ) throw()
      {
          m_p = p.Detach();  // Transfer ownership
      }
      explicit CAutoReleasePtr( T* p ) throw() :
      m_p( p )
      {
      }
      ~CAutoReleasePtr() throw()
      {
          Free();
      }

      // Templated version to allow pBase = pDerived
      template< typename TSrc >
      CAutoReleasePtr< T >& operator=( CAutoReleasePtr< TSrc >& p ) throw()
      {
          if(m_p==p.m_p)
          {
              // This means that two CAutoPtrs of two different types had the same m_p in them
              
// which is never correct
              ATLASSERT(FALSE);
          }
          else
          {
              Free();
              Attach( p.Detach() );  // Transfer ownership
          }
          return( *this );
      }
      CAutoReleasePtr< T >& operator=( CAutoReleasePtr< T >& p ) throw()
      {
          if(*this==p)
          {
              if(this!=&p)
              {
                  // If this assert fires, it means you attempted to assign one CAutoPtr to another when they both contained 
                  
// a pointer to the same underlying object. This means a bug in your code, since your object will get 
                  
// double-deleted. 
#ifdef ATL_AUTOPTR_ASSIGNMENT_ASSERT
                  ATLASSERT(FALSE);
#endif

                  // For safety, we are going to detach the other CAutoPtr to avoid a double-free. Your code still
                  
// has a bug, though.
                  p.Detach();
              }
              else
              {
                  // Alternatively, this branch means that you are assigning a CAutoPtr to itself, which is
                  
// pointless but permissible

                  
// nothing to do
              }
          }
          else
          {
              Free();
              Attach( p.Detach() );  // Transfer ownership
          }
          return( *this );
      }

      // basic comparison operators
      bool operator!=(CAutoReleasePtr<T>& p) const
      {
          return !operator==(p);
      }

      bool operator==(CAutoReleasePtr<T>& p) const
      {
          return m_p==p.m_p;
      }

      operator T*() const throw()
      {
          return( m_p );
      }
      T* operator->() const throw()
      {
          ATLASSUME( m_p != NULL );
          return( m_p );
      }

      // Attach to an existing pointer (takes ownership)
      void Attach( T* p ) throw()
      {
          ATLASSUME( m_p == NULL );
          m_p = p;
      }
      // Detach the pointer (releases ownership)
      T* Detach() throw()
      {
          T* p;

          p = m_p;
          m_p = NULL;

          return( p );
      }
      // Delete the object pointed to, and set the pointer to NULL
      void Free() throw()
      {
          ReleasePolicy::Release(m_p);
          m_p = NULL;
      }

public:
    T* m_p;
};

可以看到我們上面其實就改了一行代碼,改了下最終的釋放策略.

好,現在我們可以這樣用了:
CAutoReleasePtr<T, DeleteFunctor> p1(new int);
CAutoReleasePtr<T, DeleteArrayFunctor> p2(new int[10]);
功能是可以了,但是是不是覺得上面這樣用起來不方便,typedef一下就好了:
 typedef CAutoReleasePtr<T, DeleteFunctor> CSimplePtr<T>;
 typedef CAutoReleasePtr<T, DeleteArrayFunctor> CArrayPtr<T>;
 typedef CAutoReleasePtr<T, DeleteStackFunctor> CStackPtr<T>;
但是我們很塊發現上面的代碼編譯都過不了。

既然typedef不行,那我們就通過繼承來生成一個新類:
template<typename T> class CSimplePtr: public CAutoReleasePtr<T, DeleteFunctor> {};
template<typename T> class CArrayPtr: public CAutoReleasePtr<T, DeleteArrayFunctor> {};
template<typename T> class CStackPtr: public CAutoReleasePtr<T, DeleteStackFunctor> {};
我們很快又發現,用不起來,我們新類的構造函數需要重寫才行。

接下來我們考慮生成一個新類,然后在內部typedef:
 template<typename T>
 struct CSimplePtr
 {
     typedef CAutoReleasePtr<T, DeleteFunctor> type;
 };
 
 template<typename T>
 struct CArrayPtr
 {
     typedef CAutoReleasePtr<T, DeleteArrayFunctor> type;
 };
 
 template<typename T>
 struct CStackPtr
 {
     typedef CAutoReleasePtr<T, DeleteStackFunctor> type;
 };
然后這樣用:
CSimplePtr<int>::type p(new int);
CArrayPtr<int>::type p1(new int[20]);
可是這樣用和最初的用法似乎又沒多少改進....

再最后想到了用宏:
#define CSimplePtr(T) CAutoReleasePtr<T, DeleteFunctor>
#define CArrayPtr(T) CAutoReleasePtr<T, DeleteArrayFunctor>
但是用的時候太嘔心了:
CSimplePtr(int) p(new int);
CArrayPtr(int) p1(new int[20]);

最后,實在沒有什么辦法了....
不知道大家有沒有什么好方法 ???

posted on 2012-09-24 22:59 Richard Wei 閱讀(1911) 評論(4)  編輯 收藏 引用 所屬分類: C++

FeedBack:
# re: 重構ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 07:33 | 萬連文
C++03標準下最推薦的是CSimplePtr<int>::type p(new int);這種

http://en.wikipedia.org/wiki/C%2B%2B0x#Alias_templates 支持c++11可以這樣

貌似只能如此,不必太過糾結啊  回復  更多評論
  
# re: 重構ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 08:36 | Richard Wei
@萬連文
確實,C++11里把這個叫住Alias templates  回復  更多評論
  
# re: 重構ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr[未登錄]
2012-09-25 10:56 | 春秋十二月
1) CAutoReleasePtr的一些成員函數的參數少了ReleasePolicy,例如下:
template<typename TSrc>
CAutoReleasePtr(CAutoReleasePtr<TSrc,ReleasePolicy>& p)throw()
{
m_p = p.Detach(); // Transfer ownership
}
CAutoReleasePtr( CAutoReleasePtr<T,ReleasePolicy>& p ) throw()
{
m_p = p.Detach(); // Transfer ownership
}

2) 可以定義一個公共方法的宏來減少代碼重復,但缺點是不利于調試。例如下:
template<T>
class CSimplePtr
{
COMMON_MEMBER_METHOD(T)
void Free() { delete m_p; }
private:
T* m_p;
}
template<T>
class CArrayPtr
{
COMMON_MEMBER_METHOD(T)
void Free() { delete []m_p; }
private:
T* m_p;
}
template<T>
class CStatckPtr
{
COMMON_MEMBER_METHOD(T)
void Free() { _freea m_p; }
private:
T* m_p;
}

3)模板和宏的共同點是都能避免重復代碼,但模板更優越,這也是C++相對C進步的表現。

4)c++11新標準支持帶模板參數的typedef,又是一個進步。  回復  更多評論
  
# re: 重構ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 11:21 | Richard Wei
@春秋十二月
多謝,總結的挺好  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品久久久久99| 亚洲专区在线视频| 欧美专区福利在线| 亚洲精品久久久久久久久| 亚洲高清视频的网址| 亚洲欧美在线网| 一本色道久久综合亚洲精品不卡 | av成人免费在线| 亚洲国产高清自拍| 韩日欧美一区二区| 国产综合久久久久影院| 狠狠综合久久av一区二区小说 | 久久久亚洲欧洲日产国码αv| 亚洲午夜视频在线观看| 亚洲欧美日韩电影| 欧美一区三区三区高中清蜜桃| 狂野欧美激情性xxxx欧美| 久久久国产成人精品| 久久一区二区三区国产精品| 久久综合一区| 亚洲精品乱码久久久久久黑人 | 先锋影音网一区二区| 亚洲一区二区三区视频播放| 亚洲欧美一区二区三区久久| 亚洲永久精品大片| 免费国产一区二区| 国产精品国产三级国产专播品爱网| 国产农村妇女精品| 中文在线一区| 亚洲一区欧美激情| 久久女同互慰一区二区三区| 欧美日韩国产页| 亚洲第一色在线| 久久av红桃一区二区小说| 亚洲国产第一页| 欧美在线视频免费观看| 国产精品成人午夜| 日韩亚洲在线| 免费的成人av| 免费在线成人av| 国产丝袜一区二区三区| 一本色道久久综合| 99国产精品久久久久老师| 亚洲国产视频直播| 欧美大片第1页| 亚洲经典一区| 亚洲精品乱码久久久久久| 欧美xart系列在线观看| 99国产一区二区三精品乱码| 欧美国产日韩一区二区| 久久午夜精品| 亚洲视频一起| 亚洲欧美视频一区| 一区二区三区无毛| 欧美成人精品激情在线观看| 久久久久国产精品厨房| 亚洲精品免费电影| 在线视频亚洲一区| 韩日精品在线| 亚洲精品之草原avav久久| 欧美午夜性色大片在线观看| 欧美一区二区三区播放老司机| 亚洲免费影视| 欧美一级午夜免费电影| 国产在线精品成人一区二区三区| 美日韩精品免费| 欧美色欧美亚洲高清在线视频| 国产精品日韩精品| 久久综合伊人| 国产精品扒开腿做爽爽爽视频| 久久久福利视频| 国产精品卡一卡二卡三| 欧美暴力喷水在线| 影音先锋欧美精品| 欧美一二三区在线观看| 国产精品99免费看 | 欧美一区午夜精品| 欧美肥婆bbw| 久久字幕精品一区| 好吊日精品视频| 先锋影音久久| 久久精品系列| 亚洲国产毛片完整版| 欧美成人精品一区二区三区| 亚洲综合好骚| 午夜免费日韩视频| 亚洲欧美经典视频| 国产欧美一区二区色老头| 欧美一级片一区| 久久影视精品| 在线观看不卡| 欧美精品久久久久a| 一区二区欧美精品| 国产精品久久久久久久午夜| 亚洲国产精品www| 999亚洲国产精| 国产精品日日摸夜夜添夜夜av | 亚洲国产精品va在线观看黑人| 免费视频亚洲| 亚洲综合精品| 亚洲二区在线观看| 久久9热精品视频| 亚洲精品美女免费| 韩国成人福利片在线播放| 欧美福利电影网| 亚洲欧美精品中文字幕在线| 欧美高清视频一区二区| 久久久久9999亚洲精品| 91久久综合亚洲鲁鲁五月天| 欧美麻豆久久久久久中文| 久久久综合免费视频| 亚洲午夜免费视频| 日韩亚洲成人av在线| 在线免费不卡视频| 狠狠色丁香婷综合久久| 国产网站欧美日韩免费精品在线观看 | 国产深夜精品| 国产精品久久久久久久久搜平片 | 久久精品午夜| 久久久久国产成人精品亚洲午夜| 亚洲色图在线视频| 中文日韩在线视频| 亚洲少妇在线| 亚洲一区在线视频| 亚洲欧美激情视频在线观看一区二区三区 | 亚洲第一主播视频| 在线日本欧美| 亚洲精品永久免费精品| 亚洲大片av| 亚洲精选中文字幕| 国产日韩欧美制服另类| 国产香蕉97碰碰久久人人| 国产精品一区一区三区| 国产主播一区二区三区四区| 激情综合激情| 99精品免费| 久久er精品视频| 久久综合狠狠综合久久综青草| 欧美成人精品在线观看| 91久久国产综合久久| 一本色道久久88亚洲综合88| 亚洲欧美高清| 欧美视频导航| 亚洲黄色尤物视频| 亚洲女女女同性video| 久久福利一区| 亚洲国产日韩欧美| 久久久久久精| 国产精品一区毛片| 99re6这里只有精品视频在线观看| 亚洲欧美视频在线观看| 欧美激情亚洲一区| 久久精品99国产精品日本| 国产精品美女久久久浪潮软件| 国内精品久久久| 欧美一级在线播放| 亚洲视频在线一区| 欧美日韩美女| 国产精品美女主播| 亚洲大胆在线| 欧美激情视频免费观看| 久久久久在线| 亚洲国产精品一区二区久| 久久久久久国产精品mv| 亚洲一级黄色av| 国产精品社区| 欧美va天堂| 奶水喷射视频一区| 牛牛国产精品| 99www免费人成精品| 一区二区av在线| 国产亚洲一区二区在线观看| 久久色在线播放| 久热re这里精品视频在线6| 亚洲国产欧美国产综合一区| 夜夜嗨av一区二区三区四区 | 久久国产加勒比精品无码| 99视频在线精品国自产拍免费观看| 欧美激情在线有限公司| 亚洲免费在线视频| 欧美成人亚洲成人日韩成人| 中文亚洲字幕| 欧美成人精品在线| 久久不射2019中文字幕| 免费视频一区| 免费不卡中文字幕视频| 国产精品女同互慰在线看| 日韩午夜三级在线| 一区二区三区精品| 亚洲精品欧洲| 欧美中文字幕久久| 亚洲一区二区成人在线观看| 久久尤物电影视频在线观看| 新片速递亚洲合集欧美合集| 欧美激情一区二区三区高清视频| 可以免费看不卡的av网站| 狠狠色丁香久久婷婷综合丁香| 亚洲欧美日韩国产一区二区| 亚洲欧美影院| 狠狠色狠狠色综合日日91app|