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

看到ATL中有3個(gè)類的代碼比較比較重復(fù),在atlbase.h中,分別是CAutoVectorPtr, CAutoPtr和CAutoStackPtr,他們的功能其實(shí)很類似STL中的autoptr, 但是這里因?yàn)獒槍?duì)不同的分配對(duì)象而用了3個(gè)不同的類,其中CAutoVectorPtr是針對(duì)數(shù)組類型的,CAutoPtr是針對(duì)普通的非數(shù)組類型,而CAutoStackPtr針對(duì)的是_malloca分配的類型,因?yàn)樽詈筢尫欧绞降牟煌@里用了3份代碼來(lái)實(shí)現(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這樣寫是不是歷史原因,我們下面嘗試對(duì)它進(jìn)行重構(gòu)。

可以看到其實(shí)他們只是最終釋放(Free)的時(shí)候稍微有些差別,我們明顯可以把寫差別提取出來(lái),作為一個(gè)釋放的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; }
};

然后我們把上面的各種釋放行為作為一個(gè)模板參數(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;
};

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

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

既然typedef不行,那我們就通過繼承來(lái)生成一個(gè)新類:
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),用不起來(lái),我們新類的構(gòu)造函數(shù)需要重寫才行。

接下來(lái)我們考慮生成一個(gè)新類,然后在內(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>
但是用的時(shí)候太嘔心了:
CSimplePtr(int) p(new int);
CArrayPtr(int) p1(new int[20]);

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

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

FeedBack:
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 07:33 | 萬(wàn)連文
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ù)  更多評(píng)論
  
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 08:36 | Richard Wei
@萬(wàn)連文
確實(shí),C++11里把這個(gè)叫住Alias templates  回復(fù)  更多評(píng)論
  
# 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) 可以定義一個(gè)公共方法的宏來(lái)減少代碼重復(fù),但缺點(diǎn)是不利于調(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)模板和宏的共同點(diǎn)是都能避免重復(fù)代碼,但模板更優(yōu)越,這也是C++相對(duì)C進(jìn)步的表現(xiàn)。

4)c++11新標(biāo)準(zhǔn)支持帶模板參數(shù)的typedef,又是一個(gè)進(jìn)步。  回復(fù)  更多評(píng)論
  
# re: 重構(gòu)ATL中的CAutoVectorPtr, CAutoPtr和CAutoStackPtr
2012-09-25 11:21 | Richard Wei
@春秋十二月
多謝,總結(jié)的挺好  回復(fù)  更多評(píng)論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            一本大道av伊人久久综合| 亚洲欧美日韩成人高清在线一区| 国产伊人精品| 国产精品成人免费| 欧美极品影院| 欧美日韩国产精品一卡| 欧美激情综合五月色丁香小说| 久久精品国产久精国产思思| 欧美怡红院视频| 久久国产精品99国产精| 久热国产精品视频| 欧美精品 日韩| 国产精品日韩二区| 1024亚洲| 亚洲午夜一区二区三区| 亚洲欧美一区二区原创| 欧美在线高清| 亚洲精品国产品国语在线app| 亚洲成色精品| 亚洲视频在线观看视频| 久久久久91| 国产精品久久久久永久免费观看| 国产麻豆精品theporn| 在线观看视频一区二区| 久久精品欧美日韩| 欧美精品一区二区精品网| 国产精品三级视频| 一区二区高清视频在线观看| 久久精品国产免费| 亚洲少妇最新在线视频| 久久综合亚州| 激情综合在线| 欧美在线视频一区二区| 一区二区三欧美| 久久影视精品| 亚洲国产精品一区| 麻豆国产va免费精品高清在线| 一本大道久久a久久综合婷婷| 久久亚洲精品伦理| 亚洲国产欧美另类丝袜| 久久综合久久久| 久久亚洲免费| 亚洲三级影片| 欧美日韩国产麻豆| 久久久久久久精| 中文在线不卡| 一本大道久久a久久精二百| 免播放器亚洲一区| 亚洲欧美一区二区精品久久久| 久久蜜桃香蕉精品一区二区三区| 亚洲三级影片| 欧美精品综合| 亚洲精品欧美一区二区三区| 伊人夜夜躁av伊人久久| 欧美一区二区三区播放老司机| 亚洲伦理在线| 欧美精品一区二区三区四区| 久久久.com| 在线观看91精品国产入口| 99国产精品久久久久久久成人热| 亚洲欧美日本伦理| 久久偷窥视频| 精品91免费| 欧美日韩国产成人在线| 亚洲激情欧美| 性做久久久久久久免费看| 国产视频一区欧美| 欧美专区第一页| 亚洲欧洲另类| 久久九九免费视频| 亚洲精品国产精品国自产观看| 欧美激情1区2区| 亚洲人成网站777色婷婷| 亚洲一区欧美激情| 亚洲国产欧美久久| 欧美三日本三级少妇三99| 欧美综合二区| 亚洲每日在线| 欧美成人在线网站| 欧美怡红院视频| 亚洲黄色免费电影| 欧美日韩极品在线观看一区| 日韩亚洲欧美一区| 久久久夜夜夜| 一区二区三欧美| 亚洲精品国产无天堂网2021| 欧美午夜电影在线观看| 久久精品电影| 99视频日韩| 亚洲欧美国产视频| 黄色另类av| 欧美日韩系列| 欧美影院精品一区| 久久久一本精品99久久精品66| 亚洲电影专区| 亚洲欧美www| 一本久道久久综合狠狠爱| 国产精品伦一区| 欧美一区二区免费观在线| 亚洲国产毛片完整版| 亚洲国产婷婷综合在线精品| 亚洲欧美中文另类| 妖精视频成人观看www| 麻豆freexxxx性91精品| 久久久久久尹人网香蕉| 亚洲午夜小视频| 日韩午夜激情电影| 亚洲精品一区久久久久久| 黄色精品免费| 伊人久久大香线蕉综合热线| 国产欧美一区二区精品婷婷| 国产精品推荐精品| 国产麻豆9l精品三级站| 国产欧美日韩一区二区三区| 欧美涩涩网站| 国产精品国产成人国产三级| 欧美日韩在线播放三区四区| 欧美日韩久久久久久| 国产精品www.| 韩日精品在线| 夜夜爽99久久国产综合精品女不卡| 国产精品日韩在线一区| 国产一区二区三区久久久| 激情小说另类小说亚洲欧美 | 久久精品国产亚洲精品| 久久大逼视频| 亚洲欧美不卡| 国产麻豆精品theporn| 在线视频你懂得一区| av不卡免费看| 欧美日韩精品免费看| 亚洲一区在线免费观看| 久久人人爽国产| 亚洲一区二区三区四区在线观看| 免费看亚洲片| 亚洲国语精品自产拍在线观看| 欧美制服丝袜第一页| 亚洲人线精品午夜| 久久婷婷综合激情| 亚洲精品日本| 久久精品国产亚洲5555| 国产精品午夜在线观看| 亚洲欧美日韩一区在线| 一区二区三区日韩精品视频| 欧美激情视频在线播放| 亚洲国产高清一区二区三区| 亚洲欧美日韩另类| 亚洲无线视频| 国产亚洲日本欧美韩国| 久久精品日产第一区二区三区| 亚洲精品欧美极品| 欧美伦理在线观看| 亚洲一区二区三区精品动漫| 91久久国产综合久久| 99香蕉国产精品偷在线观看| 极品中文字幕一区| 欧美日韩免费看| 亚洲风情在线资源站| 亚洲视频在线播放| 亚洲黄色视屏| 久久岛国电影| 亚洲桃花岛网站| 欧美风情在线| 美女国产精品| 国外成人免费视频| 亚洲免费一在线| 亚洲一区在线免费观看| 美女性感视频久久久| 欧美性久久久| 日韩视频永久免费| 久久国产一二区| 欧美激情在线| 国产精品看片资源| 亚洲国产精品99久久久久久久久| 国内揄拍国内精品少妇国语| 一本久道久久综合狠狠爱| 亚洲欧美国产不卡| 亚洲成色777777女色窝| 亚洲影院在线| 国产精品久久久久久亚洲毛片| 亚洲国产日韩欧美在线图片| 你懂的视频一区二区| 欧美精品播放| 一区二区三区四区蜜桃| 亚洲人午夜精品免费| 欧美三级午夜理伦三级中视频| 亚洲福利视频二区| 久久免费视频一区| 久久久免费av| 一区二区在线视频播放| 午夜视频一区在线观看| 日韩视频三区| 国产精品一区二区久久久| 亚洲大胆在线| 美女脱光内衣内裤视频久久影院| 国产日韩欧美精品在线| 免费亚洲电影在线| 国产视频亚洲| 亚洲欧洲日产国产网站| 久久久久久香蕉网|