FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/error.H>
00027 
00028 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00029 
00030 template<class T>
00031 inline Foam::autoPtr<T>::autoPtr(T* p)
00032 :
00033     ptr_(p)
00034 {}
00035 
00036 
00037 template<class T>
00038 inline Foam::autoPtr<T>::autoPtr(const autoPtr<T>& ap)
00039 :
00040     ptr_(ap.ptr_)
00041 {
00042     ap.ptr_ = 0;
00043 }
00044 
00045 
00046 template<class T>
00047 inline Foam::autoPtr<T>::~autoPtr()
00048 {
00049     clear();
00050 }
00051 
00052 
00053 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00054 
00055 template<class T>
00056 inline bool Foam::autoPtr<T>::empty() const
00057 {
00058     return !ptr_;
00059 }
00060 
00061 
00062 template<class T>
00063 inline bool Foam::autoPtr<T>::valid() const
00064 {
00065     return ptr_;
00066 }
00067 
00068 
00069 template<class T>
00070 inline T* Foam::autoPtr<T>::ptr()
00071 {
00072     T* ptr = ptr_;
00073     ptr_ = 0;
00074     return ptr;
00075 }
00076 
00077 
00078 template<class T>
00079 inline void Foam::autoPtr<T>::set(T* p)
00080 {
00081     if (ptr_)
00082     {
00083         FatalErrorIn("void autoPtr<T>::set(T*)")
00084             << "object already allocated"
00085             << abort(FatalError);
00086     }
00087 
00088     ptr_ = p;
00089 }
00090 
00091 
00092 template<class T>
00093 inline void Foam::autoPtr<T>::reset(T* p)
00094 {
00095     if (ptr_)
00096     {
00097         delete ptr_;
00098     }
00099 
00100     ptr_ = p;
00101 }
00102 
00103 
00104 template<class T>
00105 inline void Foam::autoPtr<T>::clear()
00106 {
00107     reset(0);
00108 }
00109 
00110 
00111 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00112 
00113 template<class T>
00114 inline T& Foam::autoPtr<T>::operator()()
00115 {
00116     if (!ptr_)
00117     {
00118         FatalErrorIn("T& autoPtr<T>::operator()()")
00119             << "object is not allocated"
00120             << abort(FatalError);
00121     }
00122 
00123     return *ptr_;
00124 }
00125 
00126 
00127 template<class T>
00128 inline const T& Foam::autoPtr<T>::operator()() const
00129 {
00130     if (!ptr_)
00131     {
00132         FatalErrorIn("const T& autoPtr<T>::operator()() const")
00133             << "object is not allocated"
00134             << abort(FatalError);
00135     }
00136 
00137     return *ptr_;
00138 }
00139 
00140 
00141 /*
00142 template<class T>
00143 inline T& Foam::autoPtr<T>::operator*()
00144 {
00145     return operator()();
00146 }
00147 
00148 
00149 template<class T>
00150 inline const T& Foam::autoPtr<T>::operator*() const
00151 {
00152     return operator()();
00153 }
00154 */
00155 
00156 
00157 template<class T>
00158 inline Foam::autoPtr<T>::operator const T&() const
00159 {
00160     return operator()();
00161 }
00162 
00163 
00164 template<class T>
00165 inline T* Foam::autoPtr<T>::operator->()
00166 {
00167     if (!ptr_)
00168     {
00169         FatalErrorIn("autoPtr<T>::operator->()")
00170             << "object is not allocated"
00171             << abort(FatalError);
00172     }
00173 
00174     return ptr_;
00175 }
00176 
00177 
00178 template<class T>
00179 inline const T* Foam::autoPtr<T>::operator->() const
00180 {
00181     return const_cast<autoPtr<T>&>(*this).operator->();
00182 }
00183 
00184 
00185 template<class T>
00186 inline void Foam::autoPtr<T>::operator=(const autoPtr<T>& ap)
00187 {
00188     if (this != &ap)
00189     {
00190         reset(const_cast<autoPtr<T>&>(ap).ptr());
00191     }
00192 }
00193 
00194 
00195 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines