Forever
Do not lose heart,you will be successful sooner or later。

#pragma once
#include <assert.h>

#define DISALLOW_COPY_AND_ASSIGN(TypeName)\
    TypeName(const TypeName&)();
    void operator(const TypeName&);

template<class T>
class Scope_ref{
public:
    explicit Scope_ref(T* ptr):ptr_(ptr){}
    ~Scope_ref(){
        delete ptr_;
    }

    T* get(){
        assert(ptr_);
        return ptr_;
    }

    void Reset(T* p){
        if (p != ptr_)
        {
            delete ptr_;
            ptr_ = p;
        }
    }
    void Release(){
        assert(ptr_);
        T* temp = ptr_;
        ptr_ = NULL;
        return temp;
    }

    T& operator*(){
        return *ptr_;
    }

    T* operator->(){
        return ptr_;
    }

    bool operator==(T* p)const {return p == ptr_;}
    bool operator!=(T* p) const {return p!= ptr_;}
private:
    T* ptr_;

    template<class T2> bool operator==(Scope_ref<T2> const&t2)const;
    template<class T2> bool operator!=(Scope_ref<T2> const&t2)const;
    DISALLOW_COPY_AND_ASSIGN(Scope_ref);
};

posted on 2012-04-25 16:54  sybtj  阅读(208)  评论(0编辑  收藏  举报