OpenFOAM logo
Open Source CFD Toolkit

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-2005 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 Description
00026     
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include "error.H"
00030 
00031 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00032 
00033 namespace Foam
00034 {
00035 
00036 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00037 
00038 // Store object pointer
00039 template<class T>
00040 inline autoPtr<T>::autoPtr(T* tPtr)
00041 :
00042     ptr_(tPtr)
00043 {}
00044 
00045 
00046 // Construct copy (increment reference count if object is temporary)
00047 template<class T>
00048 inline autoPtr<T>::autoPtr(const autoPtr<T>& ap)
00049 :
00050     ptr_(ap.ptr_)
00051 {
00052     ap.ptr_ = NULL;
00053 }
00054 
00055 
00056 // Delete object
00057 template<class T>
00058 inline autoPtr<T>::~autoPtr()
00059 {
00060     clear();
00061 }
00062 
00063 
00064 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00065 
00066 // Is the autoPtr valid, i.e. is the pointer set
00067 template<class T>
00068 inline bool autoPtr<T>::valid() const
00069 {
00070     return ptr_;
00071 }
00072 
00073 // Return object pointer for reuse
00074 template<class T>
00075 inline T* autoPtr<T>::ptr()
00076 {
00077     T* ptr = ptr_;
00078     ptr_ = NULL;
00079     return ptr;
00080 }
00081 
00082 
00083 // If object pointer points to valid object:
00084 // delete object and set pointer to NULL
00085 template<class T>
00086 inline void autoPtr<T>::reset(T* p)
00087 {
00088     if (ptr_)
00089     {
00090         delete ptr_;
00091     }
00092 
00093     ptr_ = p;
00094 }
00095 
00096 
00097 // If object pointer points to valid object:
00098 // delete object and set pointer to NULL
00099 template<class T>
00100 inline void autoPtr<T>::clear()
00101 {
00102     reset(NULL);
00103 }
00104 
00105 
00106 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00107 
00108 template<class T>
00109 inline T& autoPtr<T>::operator()()
00110 {
00111     if (!ptr_)
00112     {
00113         FatalErrorIn("T& autoPtr<T>::operator()()")
00114             << "object is not allocated"
00115             << abort(FatalError);
00116     }
00117 
00118     return *ptr_;
00119 }
00120 
00121 
00122 template<class T>
00123 inline const T& autoPtr<T>::operator()() const
00124 {
00125     if (!ptr_)
00126     {
00127         FatalErrorIn("const T& autoPtr<T>::operator()() const")
00128             << "object is not allocated"
00129             << abort(FatalError);
00130     }
00131 
00132     return *ptr_;
00133 }
00134 
00135 
00136 template<class T>
00137 inline autoPtr<T>::operator const T&() const
00138 {
00139     return operator()();
00140 }
00141 
00142 
00143 /*
00144 template<class T>
00145 inline T& autoPtr<T>::operator*()
00146 {
00147     return operator()();
00148 }
00149 
00150 
00151 template<class T>
00152 inline const T& autoPtr<T>::operator*() const
00153 {
00154     return operator()();
00155 }
00156 */
00157 
00158 
00159 // Return object pointer
00160 template<class T>
00161 inline T* autoPtr<T>::operator->()
00162 {
00163     if (!ptr_)
00164     {
00165         FatalErrorIn("autoPtr<T>::operator->()")
00166             << "object is not allocated"
00167             << abort(FatalError);
00168     }
00169 
00170     return ptr_;
00171 }
00172 
00173 
00174 // Return object pointer
00175 template<class T>
00176 inline const T* autoPtr<T>::operator->() const
00177 {
00178     return const_cast<autoPtr<T>&>(*this).operator->();
00179 }
00180 
00181 
00182 template<class T>
00183 inline void autoPtr<T>::operator=(const autoPtr<T>& ap)
00184 {
00185     if (this != &ap)
00186     {
00187         const_cast<autoPtr<T>&>(*this).reset
00188         (
00189             const_cast<autoPtr<T>&>(ap).ptr()
00190         );
00191     }
00192 }
00193 
00194 
00195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00196 
00197 } // End namespace Foam
00198 
00199 // ************************************************************************* //
For further information go to www.openfoam.org