C++实现对象和接口的弱引用(四)

2013-05-03 18:17:55 · 作者: · 浏览: 168
 
  
//接口的强引用类型。  
template<typename T> struct safe_ref_t  
{  
private:  
    interface_type* m;  
public:  
    safe_ref_t(): m(0) { };  
    safe_ref_t(const safe_ref_t& value): m(value.m)  
    {  
        if(this->m != 0)  
            this->m->add_safe_ref();  
    };  
    safe_ref_t(T* ptr_t): m(dynamic_cast<interface_type*>(ptr_t))  
    {  
        if(this->m != 0)  
            this->m->add_safe_ref();  
    };  
    ~safe_ref_t()  
    {  
        if(this->m != 0)  
            this->m->safe_release();  
    };  
    operator T*()  
    {  
        return dynamic_cast<T*>(this->m);  
    };  
    operator const T*() const  
    {  
        return dynamic_cast<const T*>(this->m);  
    };  
    safe_ref_t& operator =(T* ptr_t)  
    {  
        if(dynamic_cast<interface_type*>(ptr_t) != 0)  
            static_cast<interface_type*>(ptr_t)->add_safe_ref();  
        if(this->m != 0)  
            this->m->safe_release();  
        this->m = dynamic_cast<interface_type*>(ptr_t);  
        return (*this);  
    };  
    safe_ref_t& operator =(const safe_ref_t& value)  
    {  
        if(value.m != 0)  
            value.m->add_safe_ref();  
        if(this->m != 0)  
            this->m->safe_release();  
        this->m = value.m;  
        return (*this);  
    };  
    bool operator ==(const safe_ref_t& value) const  
    {  
        return (this->m == value.m);  
    };  
    bool operator ==(T* ptr_t) const  
    {  
        return (this->m == static_cast<interface_type*>(ptr_t));  
    };  
    bool operator !=(const safe_ref_t& value) const  
    {  
        return (this->m != value.m);  
    };  
    bool operator !=(T* ptr_t) const  
    {  
        return (this->m != static_cast<interface_type*>(ptr_t));  
    };  
    bool operator <(const safe_ref_t& value) const  
    {  
        return (this->m < value.m);  
    };  
    T* operator ->() const  
    {  
        return static_cast<T*>(this->m);  
    };  
};  
  
//接口的弱引用类型。  
template<typename T> struct weak_ref_t  
{  
private:  
    interface_type* m;  
public:  
    weak_ref_t(): m(0) { };  
    weak_ref_t(const weak_ref_t& value): m(0)  
    {  
        if(value.m != 0)  
            value.m->add_weak_ref(&this->m);  
    };  
    weak_ref_t(T* ptr_t): m(0)  
    {  
        if(dynamic_cast<interface_type*>(ptr_t) != 0)  
            static_cast<interface_type*>(ptr_t)->add_weak_ref(&this->m);  
    };  
    ~weak_ref_t()  
    {  
        if(this->m != 0)  
            this->m->weak_release(&this->m);  
    };  
    operator T*()  
    {  
        return dynamic_cast<T*>(this->m);  
    };  
    operator const T*() const  
    {  
        return dynamic_cast<const T*>(this->m);  
    };  
    weak_ref_t& operator =(T* ptr_t)  
    {  
        if(this->m != 0)  
            this->m->weak_release(&this->m);  
        if(dynamic_cast<interface_type*>(ptr_t) != 0)  
        {  
            static_cast<interface_type*>(ptr_t)->add_weak_ref(&this->m);  
        }  
        else  
        {  
            this->m = 0;  
        }  
        return (*this);  
    };  
    weak_ref_t& operator =(const weak_ref_t& value)  
    {  
        if(this->m != 0)  
            this->m->weak_release(&this->m);  
        if(value.m != 0)  
        {  
            value.m->add_weak_ref(&this->m);  
        }  
        else  
        {  
            this->m = 0;  
        }  
        return (*this);  
    };  
    bool operator ==(const weak_ref_t& value) const  
    {  
        return (this->m == value.m);  
    };  
    bool operator ==(T* ptr_t) const  
    {  
        return (this->m == static_cast<interface_type*>(ptr_t));  
    };  
    bool operator !=(const weak_ref_t& value) const  
    {  
        return (this->m != value.m);  
    };  
    bool operator !=(T* ptr_t) const  
    {  
        return (this->m != static_cast<interface_type*>(ptr_t));  
    };  
    bool operator <(const weak_ref_t& value) const  
    {  
        return (this->m < value.m);  
    };  
    T* operator ->() const  
    {  
        return static_cast<T*>(this->m);  
    };  
};  
#endif