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 template<class T>
00037 inline tmp<T>::tmp(T* tPtr)
00038 :
00039 isTmp_(true),
00040 ptr_(tPtr),
00041 ref_(*tPtr)
00042 {}
00043
00044
00045 template<class T>
00046 inline tmp<T>::tmp(const T& tRef)
00047 :
00048 isTmp_(false),
00049 ptr_(NULL),
00050 ref_(tRef)
00051 {}
00052
00053
00054 template<class T>
00055 inline tmp<T>::tmp(const tmp<T>& t)
00056 :
00057 isTmp_(t.isTmp_),
00058 ptr_(t.ptr_),
00059 ref_(t.ref_)
00060 {
00061 if (isTmp_)
00062 {
00063 if (ptr_)
00064 {
00065 ptr_->operator++();
00066 }
00067 else
00068 {
00069 FatalErrorIn("tmp<T>::tmp(const tmp<T>&)")
00070 << "attempted copy of a deallocated temporary"
00071 << abort(FatalError);
00072 }
00073 }
00074 }
00075
00076
00077 template<class T>
00078 inline tmp<T>::~tmp()
00079 {
00080 if (isTmp_ && ptr_)
00081 {
00082 if (ptr_->okToDelete())
00083 {
00084 delete ptr_;
00085 ptr_ = NULL;
00086 }
00087 else
00088 {
00089 ptr_->operator--();
00090 }
00091 }
00092 }
00093
00094
00095
00096
00097 template<class T>
00098 inline bool tmp<T>::isTmp() const
00099 {
00100 return isTmp_;
00101 }
00102
00103
00104 template<class T>
00105 inline bool tmp<T>::valid() const
00106 {
00107 return (!isTmp_ || (isTmp_ && ptr_));
00108 }
00109
00110
00111 template<class T>
00112 inline T* tmp<T>::ptr() const
00113 {
00114 if (isTmp_)
00115 {
00116 if (!ptr_)
00117 {
00118 FatalErrorIn("tmp<T>::ptr() const")
00119 << "temporary deallocated"
00120 << abort(FatalError);
00121 }
00122
00123 T* ptr = ptr_;
00124 ptr_ = NULL;
00125
00126 ptr->resetRefCount();
00127
00128 return ptr;
00129 }
00130 else
00131 {
00132 return new T(ref_);
00133 }
00134 }
00135
00136
00137 template<class T>
00138 inline void tmp<T>::clear()
00139 {
00140 if (isTmp_ && ptr_)
00141 {
00142 delete ptr_;
00143 ptr_ = NULL;
00144 }
00145 }
00146
00147
00148 template<class T>
00149 inline void tmp<T>::clear() const
00150 {
00151 const_cast<tmp<T>&>(*this).clear();
00152 }
00153
00154
00155
00156
00157 template<class T>
00158 inline T& tmp<T>::operator()()
00159 {
00160 if (isTmp_)
00161 {
00162 if (!ptr_)
00163 {
00164 FatalErrorIn("T& tmp<T>::operator()()")
00165 << "temporary deallocated"
00166 << abort(FatalError);
00167 }
00168
00169 return *ptr_;
00170 }
00171 else
00172 {
00173
00174
00175
00176
00177
00178 return const_cast<T&>(ref_);
00179 }
00180 }
00181
00182
00183 template<class T>
00184 inline const T& tmp<T>::operator()() const
00185 {
00186 if (isTmp_)
00187 {
00188 if (!ptr_)
00189 {
00190 FatalErrorIn("const T& tmp<T>::operator()() const")
00191 << "temporary deallocated"
00192 << abort(FatalError);
00193 }
00194
00195 return *ptr_;
00196 }
00197 else
00198 {
00199 return ref_;
00200 }
00201 }
00202
00203
00204 template<class T>
00205 inline tmp<T>::operator const T&() const
00206 {
00207 return operator()();
00208 }
00209
00210
00211 template<class T>
00212 inline T* tmp<T>::operator->()
00213 {
00214 if (isTmp_)
00215 {
00216 if (!ptr_)
00217 {
00218 FatalErrorIn("tmp<T>::operator->()")
00219 << "temporary deallocated"
00220 << abort(FatalError);
00221 }
00222
00223 return ptr_;
00224 }
00225 else
00226 {
00227 return &const_cast<T&>(ref_);
00228 }
00229 }
00230
00231
00232 template<class T>
00233 inline const T* tmp<T>::operator->() const
00234 {
00235 return const_cast<tmp<T>&>(*this).operator->();
00236 }
00237
00238
00239 template<class T>
00240 inline void tmp<T>::operator=(const tmp<T>& t)
00241 {
00242 if (isTmp_ && ptr_)
00243 {
00244 if (ptr_->okToDelete())
00245 {
00246 delete ptr_;
00247 ptr_ = NULL;
00248 }
00249 else
00250 {
00251 ptr_->operator--();
00252 }
00253 }
00254
00255 if (t.isTmp_)
00256 {
00257 isTmp_ = true;
00258 ptr_ = t.ptr_;
00259
00260 if (ptr_)
00261 {
00262 ptr_->operator++();
00263 }
00264 else
00265 {
00266 FatalErrorIn("tmp<T>::operator=(const tmp<T>& t)")
00267 << "attempted copy of a deallocated temporary"
00268 << abort(FatalError);
00269 }
00270 }
00271 else
00272 {
00273 FatalErrorIn("tmp<T>::operator=(const tmp<T>& t)")
00274 << "attempted to assign to a const reference to constant object"
00275 << abort(FatalError);
00276 }
00277 }
00278
00279
00280
00281
00282 }
00283
00284
Copyright © 2000-2007 OpenCFD Ltd