设为首页 加入收藏

TOP

C++实现对象和接口的弱引用(四)
2013-05-03 18:17:55 来源: 作者: 【 】 浏览:146
Tags:实现 对象 接口 引用
 
  
//接口的强引用类型。  
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  
 

          

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇不使用sizeof, 计算int的位.. 下一篇C++中点操作符和箭头操作符

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: