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