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函数中加入了一些底层的测试,分享出来希望可以帮到大家。