智能指正和强弱指针的实现

#include <iostream>
#include <map>
#include <pthread.h>
using namespace std;
class maps_ptr
{
public:
        static maps_ptr * getptr()
        {
                if(inst==NULL)
                {

                    inst=new maps_ptr();
                }
                return inst;
        }
        void addRef(void *mptr)
        {
            map<void *,int>::iterator it=a.find(mptr);
            if(it!=a.end())
            {
                    it->second++;
                    return ;
            }
            a.emplace(mptr,1);

        }
        void delRef(void *mptr)
        {
                map<void *,int>::iterator it=a.find(mptr);
                if(it!=a.end())
                {
                        it->second--;
                        if(it->second==0)
                        {
                                a.erase(it);
                        }
                }
        }
        int  getRef(void *mptr)
        {
            map<void *,int>::iterator it=a.find(mptr);
            if(it!=a.end())
            {
                    return it->second;
            }
            return 0;
        }
private:
        maps_ptr()
        {};
        static maps_ptr *inst;
        map<void *,int> a;
};
maps_ptr* maps_ptr::inst=NULL;
template <typename T>
class Cweak;
template <typename T>
class smart_ptr
{

public:
        smart_ptr(T* ptr=NULL):mptr(ptr)
        {
                if(mptr!=NULL)
                {
                    a->addRef(mptr);
                }
        };
        ~smart_ptr()
        {
            a->delRef(mptr);
            if(a->getRef(mptr)==0)
            {
                    delete mptr;
            }
        }
        smart_ptr(const smart_ptr &src)
        {
                if(src.mptr!=NULL)
                {
                        mptr=src.mptr;
                        a->addRef(mptr);
                }
                else
                {
                        mptr=NULL;
                }
        }
        smart_ptr<T> (const Cweak<T> &src)
        {
            if(a->getRef(mptr)!=0)
            {
                    mptr=src.mptr;
                    a->addRef(mptr);
            }
            else
            {
                    mptr=NULL;
            }
        }
        smart_ptr<T>& operator =(const smart_ptr &src)
        {
                a->delRef(mptr);
                if(a->getRef(mptr)==0)
                {
                        delete mptr;
                }
                mptr=src.mptr;
                if(mptr!=NULL)
                {
                        a->addRef(mptr);
                }
                return *this;
        }
        T& operator *()
        {
                return *mptr; 
        }
        T* operator ->()
        {
                return mptr;
        }
        operator void  *()//使用类型转换如果需要类型转换
        {
            if(a->getRef(mptr)>0)
            {
                    return mptr;
            }
            return NULL;
        }
private:
        static maps_ptr *a;
        T *mptr;
        friend class Cweak<T>;
};
template<typename T>
maps_ptr* smart_ptr<T>::a=maps_ptr::getptr();
template <typename T>
class Cweak
{
public:
        friend class smart_ptr<T>;
        Cweak(T *p=NULL):mptr(p){};
        Cweak(const smart_ptr<T> &src)
        {
                //mptr=&*src;
                mptr=src.mptr;
        }
        Cweak& operator =(const smart_ptr<T> &src)
        {
                //mptr=&*src;
                mptr=src.mptr;
                return   *this;
        }
        smart_ptr<T>  lock()
        {
                return smart_ptr<T>(Cweak<T>(mptr));    
        }
private:
        T *mptr;
};
class A
{
        public:
                A(){cout<<"A"<<endl;}
                ~A(){cout<<"~A"<<endl;}
                void func()
                {
                    cout<<"hellowold"<<endl;
                }
};
int main()
{
        smart_ptr<A> ptr1(new A());
    //  smart_ptr<char>ptr2((char*)p);
        Cweak<A>ptr3(ptr1);
        smart_ptr<A> ptr=ptr3.lock();
        if(ptr!=NULL)
        {
         cout<<"fdsafdsfsdfsdfsdf"<<endl;
        }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值