OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

tmpI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "error.H"
00028 
00029 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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_)  // skip this bit:  && ptr_->okToDelete())
00143     {
00144         delete ptr_;
00145         ptr_ = 0;
00146     }
00147 }
00148 
00149 
00150 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
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         // Note: const is cast away!
00169         // Perhaps there should be two refs, one for const and one for non const
00170         // and if the ref is actually const then you cannot return it here.
00171         //
00172         // Another possibility would be to store a const ref and a flag to say
00173         // whether the tmp was constructed with a const or a non-const argument.
00174         //
00175         // eg, enum refType { POINTER = 0, REF = 1, CONSTREF = 2 };
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