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