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

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

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;
};

可以看到上面代碼明顯非常重復(fù),不知道ATL這樣寫是不是歷史原因,我們下面嘗試對它進(jìn)行重構(gòu)。

可以看到其實他們只是最終釋放(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; }
};

然后我們把上面的各種釋放行為作為一個模板參數(shù)傳進(jìn)去就可以了,代碼如下:
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;
};

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

好,現(xiàn)在我們可以這樣用了:
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>;
但是我們很塊發(fā)現(xiàn)上面的代碼編譯都過不了。

既然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> {};
我們很快又發(fā)現(xiàn),用不起來,我們新類的構(gòu)造函數(shù)需要重寫才行。

接下來我們考慮生成一個新類,然后在內(nèi)部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]);
可是這樣用和最初的用法似乎又沒多少改進(jìn)....

再最后想到了用宏:
#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 閱讀(1918) 評論(4)  編輯 收藏 引用 所屬分類: C++

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

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

貌似只能如此,不必太過糾結(jié)啊  回復(fù)  更多評論
  
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 08:36 | Richard Wei
@萬連文
確實,C++11里把這個叫住Alias templates  回復(fù)  更多評論
  
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr[未登錄]
2012-09-25 10:56 | 春秋十二月
1) CAutoReleasePtr的一些成員函數(shù)的參數(shù)少了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) 可以定義一個公共方法的宏來減少代碼重復(fù),但缺點是不利于調(diào)試。例如下:
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)模板和宏的共同點是都能避免重復(fù)代碼,但模板更優(yōu)越,這也是C++相對C進(jìn)步的表現(xiàn)。

4)c++11新標(biāo)準(zhǔn)支持帶模板參數(shù)的typedef,又是一個進(jìn)步。  回復(fù)  更多評論
  
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 11:21 | Richard Wei
@春秋十二月
多謝,總結(jié)的挺好  回復(fù)  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            六月婷婷久久| 精品成人一区二区三区四区| 午夜视频久久久| 亚洲欧美不卡| 亚洲欧美久久| 久久不射2019中文字幕| 久久精品一二三| 久久综合伊人| 欧美三区在线观看| 国产偷久久久精品专区| 亚洲国产岛国毛片在线| 亚洲美女电影在线| 翔田千里一区二区| 免费亚洲视频| 日韩午夜电影av| 西西人体一区二区| 久久婷婷久久| 欧美三级免费| 亚洲电影自拍| 亚洲在线成人| 欧美激情第8页| 亚洲一区欧美二区| 欧美搞黄网站| 国产午夜亚洲精品羞羞网站| 亚洲人成网站色ww在线| 亚洲欧美日韩综合| 欧美激情一区二区三区在线视频观看 | 欧美激情偷拍| 国产日韩欧美中文| 亚洲精选视频在线| 久久精品国产一区二区三区 | 国产精品日韩一区二区三区| 午夜精品久久久久久久蜜桃app| 欧美一区二区在线免费播放| 欧美国产日韩一二三区| 国产日韩一区在线| 亚洲视频在线一区| 亚洲国产成人午夜在线一区| 亚洲一二三级电影| 欧美激情视频一区二区三区免费| 国产精品免费电影| 亚洲精品一级| 欧美成人午夜激情视频| 香蕉久久夜色精品国产| 欧美日韩一区二区免费在线观看| 在线观看视频一区| 久久久精品日韩| 亚洲女与黑人做爰| 国产精品久久亚洲7777| 亚洲毛片在线看| 欧美激情综合色综合啪啪 | 欧美一区二区视频网站| 亚洲精品一区在线观看| 欧美大色视频| 亚洲另类自拍| 模特精品裸拍一区| 久久久久欧美| 亚洲二区三区四区| 免费日韩成人| 美日韩在线观看| 亚洲大片一区二区三区| 麻豆国产精品777777在线| 欧美在线日韩在线| 狠狠色伊人亚洲综合网站色| 欧美中文字幕精品| 欧美一区二区日韩一区二区| 国产情人综合久久777777| 欧美一乱一性一交一视频| 亚洲一区二区黄色| 国产乱码精品一区二区三区不卡| 午夜精品影院| 欧美专区一区二区三区| 合欧美一区二区三区| 免费短视频成人日韩| 欧美va天堂| 国产精品99久久久久久白浆小说| 一区二区三区波多野结衣在线观看| 欧美日韩一二三四五区| 亚洲欧美色婷婷| 久久久免费av| 在线一区视频| 欧美自拍偷拍| 亚洲精品在线电影| 亚洲一区二区三区视频播放| 国产欧美精品xxxx另类| 久久久免费观看视频| 欧美丰满高潮xxxx喷水动漫| 亚洲一区二区三区精品视频| 欧美一区二区免费观在线| 在线观看的日韩av| 日韩亚洲欧美在线观看| 久久久久免费| 欧美日韩国产三区| 亚洲一区免费视频| 欧美在线二区| 在线一区欧美| 久久夜色精品国产噜噜av| 正在播放欧美视频| 久久国产直播| 亚洲一区二区毛片| 美女在线一区二区| 久久不射中文字幕| 欧美人与禽猛交乱配| 久久久综合网| 欧美色综合网| 欧美激情自拍| 在线成人小视频| 亚洲在线免费观看| 夜夜夜精品看看| 久久噜噜噜精品国产亚洲综合| 这里只有精品视频| 欧美aⅴ一区二区三区视频| 欧美伊人久久久久久午夜久久久久 | 欧美亚洲视频在线看网址| 99re66热这里只有精品3直播| 欧美一级淫片aaaaaaa视频| 在线视频欧美日韩精品| 久久一二三国产| 久久免费一区| 国产精一区二区三区| 99在线精品视频| 99精品视频一区| 美女日韩在线中文字幕| 久久久免费av| 国产亚洲福利社区一区| 亚洲男人的天堂在线观看| 亚洲综合色丁香婷婷六月图片| 欧美aⅴ一区二区三区视频| 欧美成人第一页| 在线观看日韩www视频免费| 久久国产黑丝| 久久精品视频在线观看| 国产精品永久入口久久久| 亚洲丝袜av一区| 亚洲综合电影| 国产精品你懂的在线| 亚洲尤物在线视频观看| 欧美一区二区精品久久911| 国产精品一区二区三区观看| 亚洲在线视频| 欧美制服丝袜| 激情丁香综合| 欧美成人免费va影院高清| 亚洲国产视频一区| 在线亚洲+欧美+日本专区| 欧美日韩中文在线| 亚洲特黄一级片| 久久丁香综合五月国产三级网站| 国产欧美激情| 噜噜噜在线观看免费视频日韩| 亚洲高清av在线| 亚洲视频狠狠| 国产欧美va欧美不卡在线| 久久国产精品色婷婷| 欧美精品一区在线| 一本一本久久a久久精品综合麻豆| 中文在线不卡视频| 国产日韩欧美日韩| 久久婷婷综合激情| 亚洲国产精品一区二区尤物区| 日韩一级大片在线| 国产精品亚洲网站| 久久久精品国产免大香伊| 亚洲人成网站色ww在线| 亚洲天堂网在线观看| 国产午夜亚洲精品羞羞网站| 免费成年人欧美视频| 中文在线资源观看视频网站免费不卡| 午夜精品区一区二区三| 黄网动漫久久久| 欧美三级在线| 久久久青草婷婷精品综合日韩| 亚洲国产小视频在线观看| 亚洲男人的天堂在线aⅴ视频| 国内外成人免费激情在线视频| 免费看亚洲片| 亚洲欧美日韩国产成人| 欧美激情1区2区3区| 午夜精品理论片| 日韩午夜电影| 亚洲电影免费| 国产亚洲人成a一在线v站 | 久久综合一区二区| 一区二区三区高清不卡| 国产在线精品二区| 欧美日韩中文在线观看| 免费成人av在线| 欧美综合激情网| 一区二区三区.www| 亚洲国产精品久久91精品| 久久精品综合一区| 亚洲综合久久久久| 日韩亚洲欧美中文三级| 激情久久中文字幕| 国产欧美综合一区二区三区| 欧美视频在线播放| 欧美激情一区二区久久久| 另类图片国产| 久久久久久网址| 性欧美在线看片a免费观看|