|
|
|
autoPtrI.HGo 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::autoPtr<T>::autoPtr(T* p) 00033 : 00034 ptr_(p) 00035 {} 00036 00037 00038 template<class T> 00039 inline Foam::autoPtr<T>::autoPtr(const autoPtr<T>& ap) 00040 : 00041 ptr_(ap.ptr_) 00042 { 00043 ap.ptr_ = 0; 00044 } 00045 00046 00047 template<class T> 00048 inline Foam::autoPtr<T>::~autoPtr() 00049 { 00050 clear(); 00051 } 00052 00053 00054 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // 00055 00056 template<class T> 00057 inline bool Foam::autoPtr<T>::empty() const 00058 { 00059 return !ptr_; 00060 } 00061 00062 00063 template<class T> 00064 inline bool Foam::autoPtr<T>::valid() const 00065 { 00066 return ptr_; 00067 } 00068 00069 00070 template<class T> 00071 inline T* Foam::autoPtr<T>::ptr() 00072 { 00073 T* ptr = ptr_; 00074 ptr_ = 0; 00075 return ptr; 00076 } 00077 00078 00079 template<class T> 00080 inline void Foam::autoPtr<T>::set(T* p) 00081 { 00082 if (ptr_) 00083 { 00084 FatalErrorIn("void autoPtr<T>::set(T*)") 00085 << "object already allocated" 00086 << abort(FatalError); 00087 } 00088 00089 ptr_ = p; 00090 } 00091 00092 00093 template<class T> 00094 inline void Foam::autoPtr<T>::reset(T* p) 00095 { 00096 if (ptr_) 00097 { 00098 delete ptr_; 00099 } 00100 00101 ptr_ = p; 00102 } 00103 00104 00105 template<class T> 00106 inline void Foam::autoPtr<T>::clear() 00107 { 00108 reset(0); 00109 } 00110 00111 00112 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // 00113 00114 template<class T> 00115 inline T& Foam::autoPtr<T>::operator()() 00116 { 00117 if (!ptr_) 00118 { 00119 FatalErrorIn("T& autoPtr<T>::operator()()") 00120 << "object is not allocated" 00121 << abort(FatalError); 00122 } 00123 00124 return *ptr_; 00125 } 00126 00127 00128 template<class T> 00129 inline const T& Foam::autoPtr<T>::operator()() const 00130 { 00131 if (!ptr_) 00132 { 00133 FatalErrorIn("const T& autoPtr<T>::operator()() const") 00134 << "object is not allocated" 00135 << abort(FatalError); 00136 } 00137 00138 return *ptr_; 00139 } 00140 00141 00142 /* 00143 template<class T> 00144 inline T& Foam::autoPtr<T>::operator*() 00145 { 00146 return operator()(); 00147 } 00148 00149 00150 template<class T> 00151 inline const T& Foam::autoPtr<T>::operator*() const 00152 { 00153 return operator()(); 00154 } 00155 */ 00156 00157 00158 template<class T> 00159 inline Foam::autoPtr<T>::operator const T&() const 00160 { 00161 return operator()(); 00162 } 00163 00164 00165 template<class T> 00166 inline T* Foam::autoPtr<T>::operator->() 00167 { 00168 if (!ptr_) 00169 { 00170 FatalErrorIn("autoPtr<T>::operator->()") 00171 << "object is not allocated" 00172 << abort(FatalError); 00173 } 00174 00175 return ptr_; 00176 } 00177 00178 00179 template<class T> 00180 inline const T* Foam::autoPtr<T>::operator->() const 00181 { 00182 return const_cast<autoPtr<T>&>(*this).operator->(); 00183 } 00184 00185 00186 template<class T> 00187 inline void Foam::autoPtr<T>::operator=(const autoPtr<T>& ap) 00188 { 00189 if (this != &ap) 00190 { 00191 reset(const_cast<autoPtr<T>&>(ap).ptr()); 00192 } 00193 } 00194 00195 00196 // ************************************************************************* // |