ScopedPtr.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_ScopedPtr_hxx
00024 #define __ccxx_ScopedPtr_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/NullPointerException.h++>
00028
00029 namespace ccxx {
00030
00037 template<class T> class ScopedPtr
00038 {
00039 public:
00040
00042 ScopedPtr()
00043 : _object(NULL)
00044 { }
00045
00047 explicit ScopedPtr(T *object)
00048 : _object(object)
00049 { }
00050
00052 ~ScopedPtr()
00053 { delete _object; }
00054
00059 T& operator*() const throw(NullPointerException)
00060 {
00061 if(! _object)
00062 throw NullPointerException();
00063
00064 return(*_object);
00065 }
00066
00071 T* operator->() const throw(NullPointerException)
00072 {
00073 if(! _object)
00074 throw NullPointerException();
00075
00076 return(_object);
00077 }
00078
00080 void reset(T *object = NULL)
00081 {
00082 if(_object != object)
00083 {
00084 delete _object;
00085 _object = object;
00086 }
00087 }
00088
00090 T* get() const
00091 { return(_object); }
00092
00094 T* release() throw()
00095 {
00096 T *tmp = _object;
00097 _object = NULL;
00098 return(tmp);
00099 }
00100
00102 bool operator!() const
00103 { return(isNull()); }
00104
00106 operator const void *() const
00107 { return(_object ? this : NULL); }
00108
00110 bool isNull() const throw()
00111 { return(_object == NULL); }
00112
00114 void swap(ScopedPtr &other) throw()
00115 {
00116 T *tmp = other._object;
00117 other._object = _object;
00118 _object = tmp;
00119 }
00120
00121 private:
00122
00123 T *_object;
00124
00125 CCXX_COPY_DECLS(ScopedPtr);
00126 };
00127
00130 template<class T> inline void swap(ScopedPtr<T> &a, ScopedPtr<T> &b)
00131 throw()
00132 { a.swap(b); }
00133
00134 };
00135
00136 #endif // __ccxx_ScopedPtr_hxx
00137
00138