QScopedPointer 源碼剖析

來源:互聯網
上載者:User
文章目錄
  • C++ template Day Day Up 第四天 Boost::checked_delete

 

析構時候清除pointer

template <typename T>
struct QScopedPointerDeleter
{
    static inline void cleanup(T *pointer)
    {
        // Enforce a complete type.
        // If you get a compile error here, read the secion on forward declared
        // classes in the QScopedPointer documentation.

//檢查類型是否完整,編譯期間檢查
        typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
        (void) sizeof(IsIncompleteType);

        delete pointer;
    }
};

template <typename T>
struct QScopedPointerArrayDeleter
{
    static inline void cleanup(T *pointer)
    {
        // Enforce a complete type.
        // If you get a compile error here, read the secion on forward declared
        // classes in the QScopedPointer documentation.
        typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
        (void) sizeof(IsIncompleteType);

        delete [] pointer;
    }
};

struct QScopedPointerPodDeleter
{
    static inline void cleanup(void *pointer) { if (pointer) qFree(pointer); }
};

template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer
{
#ifndef Q_CC_NOKIAX86
    typedef T *QScopedPointer:: *RestrictedBool;
#endif
public:
    explicit inline QScopedPointer(T *p = 0) : d(p)
    {
    }

    inline ~QScopedPointer()
    {
        T *oldD = this->d;
        Cleanup::cleanup(oldD);
        this->d = 0;
    }

    inline T &operator*() const
    {
        Q_ASSERT(d);
        return *d;
    }

    inline T *operator->() const
    {
        Q_ASSERT(d);
        return d;
    }

    inline bool operator!() const
    {
        return !d;
    }

#if defined(Q_CC_NOKIAX86) || defined(Q_QDOC)
    inline operator bool() const
    {
        return isNull() ? 0 : &QScopedPointer::d;
    }
#else
    inline operator RestrictedBool() const
    {
        return isNull() ? 0 : &QScopedPointer::d;
    }
#endif

    inline T *data() const
    {
        return d;
    }

    inline bool isNull() const
    {
        return !d;
    }

    inline void reset(T *other = 0)
    {
        if (d == other)
            return;
        T *oldD = d;
        d = other;
        Cleanup::cleanup(oldD);
    }

    inline T *take()
    {
        T *oldD = d;
        d = 0;
        return oldD;
    }

    inline void swap(QScopedPointer<T, Cleanup> &other)
    {
        qSwap(d, other.d);
    }

    typedef T *pointer;

protected:
    T *d;

private:
    Q_DISABLE_COPY(QScopedPointer)
};

template <class T, class Cleanup>
inline bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
{
    return lhs.data() == rhs.data();
}

template <class T, class Cleanup>
inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs)
{
    return lhs.data() != rhs.data();
}

template <class T, class Cleanup>
Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2)
{ p1.swap(p2); }

template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
class QScopedArrayPointer : public QScopedPointer<T, Cleanup>
{
public:
    explicit inline QScopedArrayPointer(T *p = 0)
        : QScopedPointer<T, Cleanup>(p)
    {
    }

    inline T &operator[](int i)
    {
        return this->d[i];
    }

    inline const T &operator[](int i) const
    {
        return this->d[i];
    }

private:
    Q_DISABLE_COPY(QScopedArrayPointer)
};

 

#define Q_DISABLE_COPY(Class) \
    Class(const Class &); \
    Class &operator=(const Class &);

 

聲明拷貝和賦值函數為私人

 

--------------------------------

參考

C++ template Day Day Up 第四天 Boost::checked_delete

//DeleteObject.h

class Item;

class DeleteObject
{
public:
    DeleteObject(void);
public:
~DeleteObject(void);

void DeleteItem(Item* p);
};

//DeleteObject.cpp

#include "StdAfx.h"
#include "DeleteObject.h"
#include <boost/checked_delete.hpp>

DeleteObject::DeleteObject(void)
{
}

DeleteObject::~DeleteObject(void)
{
}

void DeleteObject::DeleteItem( Item* p )
{
    delete p;
}

//item.h

#include <iostream>

class Item
{
public:
~Item()
{
        std::cout<<"Item destruction ";
    }
};

//main.cpp

#include "DeleteObject.h"
#include "Item.h"

DeleteObject del;
del.DeleteItem(new Item);

/////////////////////////////////

結果Item的解構函式沒有被調用~

這是因為DeleteObject並不知道Item的詳細定義,這是C++中一個很危險的錯誤!(還好編譯器一般都給warning)

怎麼解決這個問題呢?

利用Boost庫中的checked_delete

定義如下:

template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x;
}

template<class T> inline void checked_array_delete(T * x)
{
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}

可以看到利用C的一個文法——數組長度必須大於零,來解決這個問題,typedef是發生在編譯期,而sizeof同樣是在編譯期,所以這兩行代碼不會對程式的效率空間等等產生任何影響,只會在上面所舉的例子發生的時候報編譯錯誤。

現在將代碼改為:

void DeleteObject::DeleteItem( Item* p )
{
    boost::checked_delete(p);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.