P
Protoman
Is this a good reference-counting smart pointer class?
// begin code
-------------------------------------------------------------------------------------------------------------------
class SmrtPtrDB
{
public:
SmrtPtrDB(int status=1):num(status){}
SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--;}
int status(){return num;}
private:
int num;
};
class NullPtr{};
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr<T>(T* obj=0)
tr(obj),DataBase(new SmrtPtrDB){}
SmrtPtr<T>(const SmrtPtr<T>& rhs)
tr(rhs.ptr),DataBase(new
SmrtPtrDB(rhs.DataBase->status())) {DataBase->add();}
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase;}
else {delete DataBase;}
}
void operator=(T* val)
{
SmrtPtr<T> temp(val);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
SmrtPtr<T> temp(rhs);
swap(temp);
return *this;
}
bool operator==(const SmrtPtr<T>& rhs)const {if(ptr==rhs.ptr)return
true;else return false;}
bool operator!=(const SmrtPtr<T>& rhs)const {if(ptr!=rhs.ptr)return
true;else return false;}
bool operator<=(const SmrtPtr<T>& rhs)const {if(ptr<=rhs.ptr)return
true;else return false;}
bool operator>=(const SmrtPtr<T>& rhs)const {if(ptr>=rhs.ptr)return
true;else return false;}
int status(){return DataBase->status();}
T& operator*()const {if(ptr==0)throw NullPtr();else return *ptr;}
T* operator->()const {if(ptr==0)throw NullPtr();else return ptr;}
operator T*()const {if(ptr==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T>& rhs)
{
std::swap(DataBase,rhs.DataBase);
std::swap(ptr,rhs.ptr);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};
//end of code
// begin code
-------------------------------------------------------------------------------------------------------------------
class SmrtPtrDB
{
public:
SmrtPtrDB(int status=1):num(status){}
SmrtPtrDB(const SmrtPtrDB& rhs):num(rhs.num){}
~SmrtPtrDB(){}
void add(){num++;}
void sub(){num--;}
int status(){return num;}
private:
int num;
};
class NullPtr{};
template<class T>
class SmrtPtr
{
public:
explicit SmrtPtr<T>(T* obj=0)
SmrtPtr<T>(const SmrtPtr<T>& rhs)
SmrtPtrDB(rhs.DataBase->status())) {DataBase->add();}
~SmrtPtr<T>()
{
DataBase->sub();
if(DataBase->status()==0)
{delete ptr; delete DataBase;}
else {delete DataBase;}
}
void operator=(T* val)
{
SmrtPtr<T> temp(val);
swap(temp);
}
SmrtPtr<T>& operator=(const SmrtPtr<T>& rhs)
{
SmrtPtr<T> temp(rhs);
swap(temp);
return *this;
}
bool operator==(const SmrtPtr<T>& rhs)const {if(ptr==rhs.ptr)return
true;else return false;}
bool operator!=(const SmrtPtr<T>& rhs)const {if(ptr!=rhs.ptr)return
true;else return false;}
bool operator<=(const SmrtPtr<T>& rhs)const {if(ptr<=rhs.ptr)return
true;else return false;}
bool operator>=(const SmrtPtr<T>& rhs)const {if(ptr>=rhs.ptr)return
true;else return false;}
int status(){return DataBase->status();}
T& operator*()const {if(ptr==0)throw NullPtr();else return *ptr;}
T* operator->()const {if(ptr==0)throw NullPtr();else return ptr;}
operator T*()const {if(ptr==0)throw NullPtr();else return ptr;}
private:
void swap(SmrtPtr<T>& rhs)
{
std::swap(DataBase,rhs.DataBase);
std::swap(ptr,rhs.ptr);
}
mutable SmrtPtrDB* DataBase;
T* ptr;
};
//end of code