自己寫的智能指標源碼

來源:互聯網
上載者:User
//-------------------------------------------------
//                    Smart Pointer
//-------------------------------------------------

#ifndef _SMART_POINTER_H
#define _SMART_POINTER_H

extern std::list<short>        ptrCounter;

template <class T>
class smartPtr
{
private:
    T            *ptr;
    short        *counter;
#ifdef _DEBUG
#define _DEBUG_FILE_NAME_LEN        32
#define _DEBUG_RECORD_LEN            32
    typedef struct __debug_record {
        char    file[_DEBUG_FILE_NAME_LEN];
        int        line;
    } debug_record;
public:
    debug_record    ptr_changed_rec[_DEBUG_RECORD_LEN];
    int                ptr_changed_time;
#endif //_DEBUG
public:
#ifdef _DEBUG
    smartPtr(LPCSTR file = "Construction By Default",int line = __LINE__){
#else
    smartPtr(){
#endif //_DEBUG
        ptr = new T;
        ptrCounter.push_back(1);
        counter = &ptrCounter.back();
#ifdef _DEBUG
        memset(ptr_changed_rec,0,sizeof(debug_record)*_DEBUG_RECORD_LEN);
        strncpy(ptr_changed_rec[0].file,file,_DEBUG_FILE_NAME_LEN);
        ptr_changed_rec[0].line = line;
        ptr_changed_time = 1;
#endif //_DEBUG
    }
#ifdef _DEBUG
    smartPtr(const T *np,LPCSTR file = "Construction By Pointer",int line = __LINE__){
#else
    smartPtr(const T *np){
#endif //_DEBUG
        ptr = const_cast<T*> (np);
        ptrCounter.push_back(1);
        counter = &ptrCounter.back();
#ifdef _DEBUG
        memset(ptr_changed_rec,0,sizeof(debug_record)*_DEBUG_RECORD_LEN);
        strncpy(ptr_changed_rec[0].file,file,_DEBUG_FILE_NAME_LEN);
        ptr_changed_rec[0].line = line;
        ptr_changed_time = 1;
#endif //_DEBUG
    }
    smartPtr(const smartPtr<T> & np){
        ptr = np.ptr;
        counter = np.counter;
        (*counter) ++;
#ifdef _DEBUG
        LPCSTR file = "Construction By Copying";int line = __LINE__;
        memset(ptr_changed_rec,0,sizeof(debug_record)*_DEBUG_RECORD_LEN);
        strncpy(ptr_changed_rec[0].file,file,_DEBUG_FILE_NAME_LEN);
        ptr_changed_rec[0].line = line;
        ptr_changed_time = 1;
#endif //_DEBUG
    }
    void operator = (const smartPtr<T> & np){
        if(counter != NULL){
            (*counter) --;
            if((*counter) <= 0)
                delete ptr;
        }
        ptr = np.ptr;
        counter = np.counter;
        (*counter) ++;
#ifdef _DEBUG
        LPCSTR file = "Value Assignment";int line = __LINE__;
        strncpy(ptr_changed_rec[ptr_changed_time].file,file,_DEBUG_FILE_NAME_LEN);
        ptr_changed_rec[ptr_changed_time].line = line;
        ptr_changed_time ++;
#endif //_DEBUG
    }
    ~smartPtr(){
        (*counter) --;
        if((*counter) <= 0)
            delete ptr;
    }
    T *operator->(){
        return ptr;
    }
    T *real(){
        return ptr;
    }
};

#endif //_SMART_POINTER_H

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.