1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright held by original author
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 inline Foam::tmp<T>::tmp(T* tPtr)
41 inline Foam::tmp<T>::tmp(const T& tRef)
50 inline Foam::tmp<T>::tmp(const tmp<T>& t)
64 FatalErrorIn("tmp<T>::tmp(const tmp<T>&)")
65 << "attempted copy of a deallocated temporary"
73 inline Foam::tmp<T>::~tmp()
77 if (ptr_->okToDelete())
90 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 inline bool Foam::tmp<T>::isTmp() const
100 inline bool Foam::tmp<T>::empty() const
102 return (isTmp_ && !ptr_);
107 inline bool Foam::tmp<T>::valid() const
109 return (!isTmp_ || (isTmp_ && ptr_));
114 inline T* Foam::tmp<T>::ptr() const
120 FatalErrorIn("tmp<T>::ptr() const")
121 << "temporary deallocated"
122 << abort(FatalError);
128 ptr->resetRefCount();
140 inline void Foam::tmp<T>::clear() const
142 if (isTmp_ && ptr_) // skip this bit: && ptr_->okToDelete())
150 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
153 inline T& Foam::tmp<T>::operator()()
159 FatalErrorIn("T& tmp<T>::operator()()")
160 << "temporary deallocated"
161 << abort(FatalError);
168 // Note: const is cast away!
169 // Perhaps there should be two refs, one for const and one for non const
170 // and if the ref is actually const then you cannot return it here.
172 // Another possibility would be to store a const ref and a flag to say
173 // whether the tmp was constructed with a const or a non-const argument.
175 // eg, enum refType { POINTER = 0, REF = 1, CONSTREF = 2 };
176 return const_cast<T&>(ref_);
182 inline const T& Foam::tmp<T>::operator()() const
188 FatalErrorIn("const T& tmp<T>::operator()() const")
189 << "temporary deallocated"
190 << abort(FatalError);
203 inline Foam::tmp<T>::operator const T&() const
210 inline T* Foam::tmp<T>::operator->()
216 FatalErrorIn("tmp<T>::operator->()")
217 << "temporary deallocated"
218 << abort(FatalError);
225 return &const_cast<T&>(ref_);
231 inline const T* Foam::tmp<T>::operator->() const
233 return const_cast<tmp<T>&>(*this).operator->();
238 inline void Foam::tmp<T>::operator=(const tmp<T>& t)
242 if (ptr_->okToDelete())
264 FatalErrorIn("tmp<T>::operator=(const tmp<T>& t)")
265 << "attempted copy of a deallocated temporary"
266 << abort(FatalError);
271 FatalErrorIn("tmp<T>::operator=(const tmp<T>& t)")
272 << "attempted to assign to a const reference to constant object"
273 << abort(FatalError);
278 // ************************************************************************* //