Share_ptr的底层实现

Share_ptr的底层实现

#include <iostream>
#include <pthread.h>

using namespace std;
typedef struct
{
    int a;
    double b;
    char c;
}mytest; 

template <class T>
class myshare_ptr
{
private:
    T *ptr;
    int *count;
    pthread_mutex_t mutex;
public:
    myshare_ptr();//构造函数
    myshare_ptr(T *p);//默认构造函数
    myshare_ptr(const myshare_ptr<T> &orig);//自定义构造函数
    ~myshare_ptr();//析构函数

    myshare_ptr<T> &operator=(const myshare_ptr<T> &rhs);//赋值运算符重载

    T &operator*();//解引用重载
    T *operator->();//取成员重载
    T *operator+(int i);//加号重载

    int operator-(myshare_ptr<T> &other); //减号重载
    bool operator==(T *p);//等号重载


    T* get(); //得到原始指针
    myshare_ptr<T> &swap(myshare_ptr<T> &rhs);     //交换指针
    bool unique();       //如果指针引用数量为1 返回true 其他的返回false
    int use_count();    //返回该内存引用只能指针的数量
};

template <class T>
myshare_ptr<T>::myshare_ptr()
{
    ptr = NULL;
    try
    {
        count = new int(0);
    }
    catch(...)
    {
        delete ptr;
        ptr = NULL;
        delete count;
        count = NULL; 
    }
    pthread_mutex_init(&mutex,NULL);
}

template <class T>
myshare_ptr<T>::myshare_ptr(T *p)
{
    ptr = p;
    try
    {
        count = new int(1);
    }
    catch(...)
    {
        delete ptr;
        ptr = NULL;
        delete count;
        count = NULL;
    }
    pthread_mutex_init(&mutex,NULL);
}

template <class T>
myshare_ptr<T>::myshare_ptr(const myshare_ptr<T> &orig)
{
    pthread_mutex_init(&mutex,NULL);
    count = orig.count;
    this->ptr = orig.ptr;
    pthread_mutex_lock(&mutex);
    ++(*count);
    pthread_mutex_unlock(&mutex);
}

template <class T>
T& myshare_ptr<T>::operator*()
{
    return *ptr;
}

template<class T>
T* myshare_ptr<T>::operator->()
{
	return ptr;
}

template<class T>
T* myshare_ptr<T>::operator+(int i)
{
	T *temp = ptr + i;
	return temp;
}

template<class T>
int myshare_ptr<T>::operator-(myshare_ptr<T> &other)
{
	return this->ptr - other.ptr;
}

template<class T>
myshare_ptr<T>& myshare_ptr<T>::operator=(const myshare_ptr<T> &rhs)
{
	++*(rhs.count);
    pthread_mutex_lock(&mutex);
	if (--*(count) == 0)
	{
		delete ptr;
		ptr = NULL;
		delete count;
		count = NULL;
	}
	ptr = rhs.ptr;
	*count = *(rhs.count);
    pthread_mutex_unlock(&mutex);
	return *this;
}

template <class T>
bool myshare_ptr<T>::operator==(T *p)
{
    if(this->ptr == p)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class T>
T* myshare_ptr<T>::get()
{
    return ptr;
}

template <class T>
myshare_ptr<T>& myshare_ptr<T>::swap(myshare_ptr<T> &rhs)
{
    myshare_ptr<T> temp = rhs;
    rhs = *this;
    *this = temp;
}

template <class T>
bool myshare_ptr<T>::unique()
{
    if(*count == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class T>
int myshare_ptr<T>::use_count()
{
    return *count;
}

template <class T>
myshare_ptr<T>::~myshare_ptr()
{
    if(0 == *count)
    {
        delete ptr;
        delete count;
        count = NULL;
        ptr = NULL;
    }
    else
    {
        pthread_mutex_lock(&mutex);
        (*count)--;
        pthread_mutex_unlock(&mutex);
    }
    pthread_mutex_destroy(&mutex);
}


int main()
{
    myshare_ptr<int> intptr = new int(100);

    myshare_ptr<int> intptr1;
    if(intptr1 == NULL)
    {
        cout<<"it is NULL"<<endl;
    }
    cout<<*intptr<<endl;

    myshare_ptr<mytest> ohhhh = new mytest();
    ohhhh->a = 10;
    ohhhh->b = 3.0;
    cout<<ohhhh->a<<ohhhh->b<<endl;

    myshare_ptr<int> intptr2 =new int(200);
    cout<<intptr2.unique()<<endl;
    myshare_ptr<int> intptr3(intptr2);
    cout<<intptr2.use_count()<<endl;
    cout<<intptr3.use_count()<<endl;
    return 0;
}

以上为我在Linux环境下编写的share_ptr的底层实现,并在Main函数中加入了一些底层的测试,分享出来希望可以帮到大家。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值