OptppSmartPtr.h
00001 #ifndef SMARTPTR_H
00002 #define SMARTPTR_H
00003
00004 #include "OptppFatalError.h"
00005
00006 namespace OPTPP {
00007
00022 template <class T>
00023 class SmartPtr {
00024 public:
00025 inline SmartPtr(T* ptr = 0);
00026 inline SmartPtr(const SmartPtr<T>& other);
00027 inline ~SmartPtr();
00028 const SmartPtr<T>& operator =(const SmartPtr<T>& rhs);
00029 inline T* operator ->();
00030 inline bool isNull() const;
00031 inline const T* operator ->() const;
00032 inline T& operator *();
00033 inline const T& operator *() const;
00034 inline operator const T* () const;
00035 inline bool isNonUnique() const;
00036
00037 inline int refCount() const;
00038
00039 protected:
00040 T* ptr_;
00041 int* refCount_;
00042 };
00043
00044 template <class T> inline
00045 SmartPtr<T>::SmartPtr(T* ptr)
00046 : ptr_(ptr),
00047 refCount_(0)
00048 {
00049 if (ptr_) {
00050 refCount_ = new int;
00051 if (refCount_ == 0)
00052 OptppmemoryError("SmartPtr::SmartPtr(T* ptr) out of memory");
00053 *refCount_ = 1;
00054 }
00055 }
00056
00057 template <class T> inline
00058 SmartPtr<T>::SmartPtr(const SmartPtr<T>& other)
00059 : ptr_(other.ptr_),
00060 refCount_(other.refCount_)
00061 {
00062 if (refCount_ != 0)
00063 ++(*refCount_);
00064 }
00065
00066 template <class T>
00067 inline SmartPtr<T>::~SmartPtr() {
00068 if (refCount_ != 0 && --(*refCount_) == 0) {
00069 delete ptr_;
00070 ptr_ = 0;
00071 delete refCount_;
00072 refCount_ = 0;
00073 }
00074 }
00075
00076 template <class T> inline
00077 bool SmartPtr<T>::isNull() const {
00078 return (ptr_ == 0);
00079 }
00080
00081 template <class T> inline
00082 T* SmartPtr<T>::operator ->() {
00083 if (ptr_ == 0)
00084 OptppfatalError("SmartPtr<T>::operator ->() on null pointer");
00085 return ptr_;
00086 }
00087
00088 template <class T> inline
00089 const T* SmartPtr<T>::operator ->() const {
00090 if (ptr_ == 0)
00091 OptppfatalError("SmartPtr<T>::operator ->() on null pointer");
00092 return ptr_;
00093 }
00094
00095 template <class T> inline
00096 T& SmartPtr<T>::operator *() {
00097 if (ptr_ == 0)
00098 OptppfatalError("SmartPtr<T>::operator *() on null pointer");
00099 return *ptr_;
00100 }
00101
00102 template <class T> inline
00103 const T& SmartPtr<T>::operator *() const {
00104 if (ptr_ == 0)
00105 OptppfatalError("SmartPtr<T>::operator *() on null pointer");
00106 return *ptr_;
00107 }
00108
00109 template <class T> inline
00110 SmartPtr<T>::operator const T* () const {
00111 return ptr_;
00112 }
00113
00114 template <class T> inline
00115 bool SmartPtr<T>::isNonUnique() const {
00116 return refCount_ == 0 ? false : *refCount_ != 1;
00117 }
00118
00119 template <class T> inline
00120 int SmartPtr<T>::refCount() const {
00121 return refCount_ == 0 ? 0 : *refCount_;
00122 }
00123
00124 template <class T>
00125 inline const SmartPtr<T>& SmartPtr<T>::operator =(const SmartPtr<T>& rhs) {
00126 if (ptr_ != rhs.ptr_) {
00127 if (refCount_ != 0 && --(*refCount_) == 0) {
00128 delete ptr_;
00129 delete refCount_;
00130 }
00131 ptr_ = rhs.ptr_;
00132 refCount_ = rhs.refCount_;
00133 if (refCount_ != 0)
00134 ++(*refCount_);
00135 }
00136 return *this;
00137 }
00138
00139 }
00140 #endif