00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "error.H"
00030
00031
00032
00033 namespace Foam
00034 {
00035
00036
00037
00038
00039 template<class T>
00040 inline autoPtr<T>::autoPtr(T* tPtr)
00041 :
00042 ptr_(tPtr)
00043 {}
00044
00045
00046
00047 template<class T>
00048 inline autoPtr<T>::autoPtr(const autoPtr<T>& ap)
00049 :
00050 ptr_(ap.ptr_)
00051 {
00052 ap.ptr_ = NULL;
00053 }
00054
00055
00056
00057 template<class T>
00058 inline autoPtr<T>::~autoPtr()
00059 {
00060 clear();
00061 }
00062
00063
00064
00065
00066
00067 template<class T>
00068 inline bool autoPtr<T>::valid() const
00069 {
00070 return ptr_;
00071 }
00072
00073
00074 template<class T>
00075 inline T* autoPtr<T>::ptr()
00076 {
00077 T* ptr = ptr_;
00078 ptr_ = NULL;
00079 return ptr;
00080 }
00081
00082
00083
00084
00085 template<class T>
00086 inline void autoPtr<T>::reset(T* p)
00087 {
00088 if (ptr_)
00089 {
00090 delete ptr_;
00091 }
00092
00093 ptr_ = p;
00094 }
00095
00096
00097
00098
00099 template<class T>
00100 inline void autoPtr<T>::clear()
00101 {
00102 reset(NULL);
00103 }
00104
00105
00106
00107
00108 template<class T>
00109 inline T& autoPtr<T>::operator()()
00110 {
00111 if (!ptr_)
00112 {
00113 FatalErrorIn("T& autoPtr<T>::operator()()")
00114 << "object is not allocated"
00115 << abort(FatalError);
00116 }
00117
00118 return *ptr_;
00119 }
00120
00121
00122 template<class T>
00123 inline const T& autoPtr<T>::operator()() const
00124 {
00125 if (!ptr_)
00126 {
00127 FatalErrorIn("const T& autoPtr<T>::operator()() const")
00128 << "object is not allocated"
00129 << abort(FatalError);
00130 }
00131
00132 return *ptr_;
00133 }
00134
00135
00136 template<class T>
00137 inline autoPtr<T>::operator const T&() const
00138 {
00139 return operator()();
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 template<class T>
00161 inline T* autoPtr<T>::operator->()
00162 {
00163 if (!ptr_)
00164 {
00165 FatalErrorIn("autoPtr<T>::operator->()")
00166 << "object is not allocated"
00167 << abort(FatalError);
00168 }
00169
00170 return ptr_;
00171 }
00172
00173
00174
00175 template<class T>
00176 inline const T* autoPtr<T>::operator->() const
00177 {
00178 return const_cast<autoPtr<T>&>(*this).operator->();
00179 }
00180
00181
00182 template<class T>
00183 inline void autoPtr<T>::operator=(const autoPtr<T>& ap)
00184 {
00185 if (this != &ap)
00186 {
00187 const_cast<autoPtr<T>&>(*this).reset
00188 (
00189 const_cast<autoPtr<T>&>(ap).ptr()
00190 );
00191 }
00192 }
00193
00194
00195
00196
00197 }
00198
00199