OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

autoPtrI.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-2007 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00030 
00031 namespace Foam
00032 {
00033 
00034 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00035 
00036 // Store object pointer
00037 template<class T>
00038 inline autoPtr<T>::autoPtr(T* tPtr)
00039 :
00040     ptr_(tPtr)
00041 {}
00042 
00043 
00044 // Construct copy (increment reference count if object is temporary)
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 // Delete object
00055 template<class T>
00056 inline autoPtr<T>::~autoPtr()
00057 {
00058     clear();
00059 }
00060 
00061 
00062 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00063 
00064 // Is the autoPtr valid, i.e. is the pointer set
00065 template<class T>
00066 inline bool autoPtr<T>::valid() const
00067 {
00068     return ptr_;
00069 }
00070 
00071 // Return object pointer for reuse
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 // Set pointer to that given.
00082 // If object pointer already set issue a FatalError.
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 // If object pointer already set delete object and
00098 // set pointer to that given
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 // If object pointer points to valid object:
00112 // delete object and set pointer to NULL
00113 template<class T>
00114 inline void autoPtr<T>::clear()
00115 {
00116     reset(NULL);
00117 }
00118 
00119 
00120 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
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 template<class T>
00152 inline T& autoPtr<T>::operator*()
00153 {
00154     return operator()();
00155 }
00156 
00157 
00158 template<class T>
00159 inline const T& autoPtr<T>::operator*() const
00160 {
00161     return operator()();
00162 }
00163 */
00164 
00165 
00166 template<class T>
00167 inline autoPtr<T>::operator const T&() const
00168 {
00169     return operator()();
00170 }
00171 
00172 
00173 // Return object pointer
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 // Return object pointer
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 } // End namespace Foam
00212 
00213 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd