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

看到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 閱讀(1927) 評論(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>
            亚洲视频在线观看网站| 亚洲片在线资源| 亚洲综合导航| 亚洲小说区图片区| 国产精品一区一区三区| 欧美一区二区在线| 性久久久久久| 亚洲国产精品成人| 亚洲精选久久| 国产精品日韩高清| 卡一卡二国产精品| 欧美激情导航| 午夜亚洲福利| 久久久91精品国产一区二区三区| 精品成人国产| 亚洲国产网站| 国产精品国产三级国产普通话三级| 午夜亚洲福利| 蜜桃久久精品一区二区| 在线视频日韩| 久久国产精品色婷婷| 最新国产拍偷乱拍精品| 亚洲性人人天天夜夜摸| 在线观看日韩欧美| 日韩视频免费观看高清在线视频 | 欧美激情一区二区三区在线| 欧美激情第一页xxx| 午夜一级在线看亚洲| 榴莲视频成人在线观看| 亚洲欧美国产毛片在线| 久久天天躁狠狠躁夜夜av| 一区二区三区国产在线观看| 亚洲欧美在线观看| 亚洲精品老司机| 午夜视频一区在线观看| 亚洲精品在线三区| 久久精品国产久精国产爱| 一区二区三区不卡视频在线观看 | 99精品欧美一区| 国产一区再线| 亚洲天堂av在线免费观看| 亚洲黄色影院| 欧美一区二区三区啪啪| 夜夜嗨av一区二区三区四季av| 久久国产精品久久精品国产 | 狠狠干成人综合网| 亚洲天堂av高清| 一本色道综合亚洲| 久久综合激情| 久久亚洲私人国产精品va| 国产精品国产亚洲精品看不卡15 | 久久综合九色| 国产欧美日韩亚洲精品| 一本久道综合久久精品| 日韩亚洲在线观看| 免费看精品久久片| 免费不卡在线观看av| 国产午夜精品在线| 亚洲欧美日韩国产一区二区三区| 在线综合视频| 欧美精品一区二区三区在线播放| 欧美大片在线观看一区| 极品尤物久久久av免费看| 欧美一区二区三区另类| 久久er99精品| 国产亚洲欧美在线| 香蕉久久国产| 久久爱另类一区二区小说| 国产老肥熟一区二区三区| 亚洲无限乱码一二三四麻| 亚洲男人第一av网站| 国产精品福利av| 亚洲一级黄色av| 欧美一区三区二区在线观看| 国产欧美日韩精品一区| 欧美一区二区视频97| 久久久噜噜噜久久| 亚洲电影成人| 欧美激情一区二区三区四区| 亚洲精品国产品国语在线app| 99www免费人成精品| 欧美日韩亚洲高清一区二区| 正在播放日韩| 久久国产精品99国产| 国内精品福利| 欧美jjzz| 亚洲视频在线免费观看| 久久9热精品视频| 亚洲成色最大综合在线| 欧美精品一区在线| 亚洲在线1234| 欧美成人影音| 亚洲在线成人精品| 国产亚洲欧美另类中文| 模特精品在线| 中文一区二区| 欧美mv日韩mv国产网站| 一区二区精品| 国产在线精品一区二区夜色| 免费在线成人| 亚洲一区二区三区国产| 欧美.日韩.国产.一区.二区| 99综合在线| 国内一区二区三区| 欧美日韩不卡| 久久动漫亚洲| 99国产精品国产精品久久| 久久一区二区三区国产精品| 一区二区三区欧美激情| 一区二区三区在线看| 欧美日本成人| 久久婷婷久久一区二区三区| 国产精品99久久久久久久vr| 欧美mv日韩mv国产网站| 性欧美大战久久久久久久免费观看 | 一本久久a久久免费精品不卡| 久久久成人精品| 亚洲网址在线| 亚洲黄色av一区| 国产三级精品在线不卡| 欧美日韩黄色大片| 久久一二三区| 欧美一区二区三区另类| 宅男66日本亚洲欧美视频| 欧美激情亚洲国产| 久久影院亚洲| 亚洲欧美另类在线观看| 日韩午夜av电影| 亚洲激情综合| 136国产福利精品导航网址| 国产欧美日韩亚洲一区二区三区| 欧美日韩视频一区二区三区| 免费在线观看精品| 老牛嫩草一区二区三区日本 | 一区二区三区国产在线观看| 亚洲国产一区二区视频| 免费不卡在线视频| 老司机免费视频一区二区三区| 欧美夜福利tv在线| 亚洲欧美日韩精品久久亚洲区| 亚洲精品久久久一区二区三区| ●精品国产综合乱码久久久久| 国产在线观看91精品一区| 国产麻豆精品久久一二三| 国产精品理论片| 国产精品色一区二区三区| 国产精品欧美日韩一区二区| 国产精品久久久久久久久婷婷| 欧美日韩视频一区二区| 欧美香蕉视频| 国产精品欧美精品| 国产日韩亚洲| 国内久久精品| 亚洲黄色在线| 999亚洲国产精| 亚洲视频在线看| 亚洲综合第一| 久久久噜噜噜久噜久久| 裸体一区二区三区| 欧美激情一区二区三区高清视频| 亚洲国产精品va在看黑人| 亚洲国产影院| 一区二区三区免费观看| 亚洲自拍偷拍一区| 久久久精品国产99久久精品芒果| 久久夜色精品亚洲噜噜国产mv| 免费看黄裸体一级大秀欧美| 欧美日韩国语| 国产婷婷色综合av蜜臀av| 亚洲成人在线免费| 中文网丁香综合网| 久久国产精品久久久| 免费视频最近日韩| av成人福利| 欧美资源在线| 欧美日韩视频一区二区三区| 国产精品一区二区久久久久| 在线欧美三区| 亚洲综合好骚| 欧美成人综合在线| 亚洲网站在线看| 免费在线亚洲| 国产日韩亚洲欧美精品| 亚洲精品社区| 久久久久久久综合色一本| 亚洲精品综合| 久久九九全国免费精品观看| 欧美午夜不卡影院在线观看完整版免费 | 新67194成人永久网站| 欧美岛国在线观看| 国产日韩精品视频一区二区三区| 亚洲国产欧美一区二区三区同亚洲| 亚洲午夜高清视频| 欧美大片一区二区| 亚洲欧美日韩精品在线| 欧美日本不卡高清| 影音先锋欧美精品| 久久国产精品高清| 亚洲天堂av在线免费| 欧美r片在线|