Class shared_res

Synopsis

#include <include/shared_res.h>

template<class C>
class shared_res

Description

No description yet.

Methods

shared_res overload
~shared_res
freeRes
get_object
get_ref_count
operator bool
operator->
operator<
operator=
operator==
takeRes

Source

Lines 38-108 in include/shared_res.h.

template<class C>
class shared_res{
protected:
    shared_cnt_obj_pair<C>* res;

    inline void freeRes(){
        if(!res)
            return;
        res->dec_pair_users();
        if(res->getRefCount()==0)
            delete res;
    }
    inline void takeRes(shared_cnt_obj_pair<C>* r){
        res=r;
        if(!res)
            return;
        res->inc_pair_users();
    }

public:
    inline explicit shared_res(C* tg) : res(NULL) {
        if(!tg)
            throw std::logic_error("no reason to manage NULL object");
        shared_cnt_obj_pair<C>* share=new shared_cnt_obj_pair<C>(tg);
        takeRes(share);
    }

    inline shared_res() : res(NULL) {}

    inline shared_res(const shared_res<C>& x) : res(NULL) {
        takeRes(x.res);
    }

    inline virtual ~shared_res(){
        freeRes();
    }

    inline shared_res<C>& operator=(const shared_res<C>& x){
        if(x.res!=res){
            freeRes();
            takeRes(x.res);
        }
        return *this;
    }

    inline C* get_object(){
        if(!res)
            return NULL;
        return res->getRes();
    }

    inline C* operator->(){
        return get_object();
    }

    inline int get_ref_count(){
        if(!res)
            return 0;
        return res->getRefCount();
    }

    inline bool operator==(const shared_res<C>& x) const{
        return x.res==res;
    }
    inline bool operator<(const shared_res<C>& x) const{
        return res<x.res;
    }
    inline operator bool() const{
        return res!=NULL;
    }
};





Add Discussion as Guest

Log in