OpenFOAM logo
Open Source CFD Toolkit

PtrListI.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     A PtrList<class T> is a 1D array of pointers to objects
00027     of T 'T', where the size of the array is known and used for
00028     subscript bounds checking, etc.
00029     The element operator [] returns a reference to the object
00030     rather than a pointer.
00031 
00032 \*---------------------------------------------------------------------------*/
00033 
00034 #include "error.H"
00035 
00036 #include "autoPtr.H"
00037 #include "tmp.H"
00038 
00039 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00040 
00041 namespace Foam
00042 {
00043 
00044 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00045 
00046 template<class T>
00047 inline label PtrList<T>::size() const
00048 {
00049     return ptrs_.size();
00050 }
00051 
00052 
00053 template<class T>
00054 inline void PtrList<T>::hook(const autoPtr<T>& aptr)
00055 {
00056     hook(const_cast<autoPtr<T>&>(aptr).ptr());
00057 }
00058 
00059 
00060 template<class T>
00061 inline void PtrList<T>::hook(const tmp<T>& t)
00062 {
00063     hook(const_cast<tmp<T>&>(t).ptr());
00064 }
00065 
00066 
00067 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00068 
00069 // Return element const reference
00070 
00071 template<class T>
00072 const T& PtrList<T>::operator[](const label i) const
00073 {
00074     if (!ptrs_[i])
00075     {
00076         FatalErrorIn("PtrList::operator[] const")
00077             << "hanging pointer, cannot dereference"
00078             << abort(FatalError);
00079     }
00080 
00081     return *(ptrs_[i]);
00082 }
00083 
00084 
00085 // Return element reference
00086 
00087 template<class T>
00088 T& PtrList<T>::operator[](const label i)
00089 {
00090     if (!ptrs_[i])
00091     {
00092         FatalErrorIn("PtrList::operator[]")
00093             << "hanging pointer, cannot dereference"
00094             << abort(FatalError);
00095     }
00096 
00097     return *(ptrs_[i]);
00098 }
00099 
00100 
00101 template<class T>
00102 const T* PtrList<T>::operator()(const label i) const
00103 {
00104     return ptrs_[i];
00105 }
00106 
00107 
00108 // * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //
00109 
00110 template<class T>
00111 inline PtrList<T>::iterator::iterator(T** ptr)
00112 :
00113     ptr_(ptr)
00114 {}
00115 
00116 template<class T>
00117 inline bool PtrList<T>::iterator::operator==(const iterator& iter) const
00118 {
00119     return ptr_ == iter.ptr_;
00120 }
00121 
00122 template<class T>
00123 inline bool PtrList<T>::iterator::operator!=(const iterator& iter) const
00124 {
00125     return ptr_ != iter.ptr_;
00126 }
00127 
00128 template<class T>
00129 inline T& PtrList<T>::iterator::operator*()
00130 {
00131     return **ptr_;
00132 }
00133 
00134 template<class T>
00135 inline T& PtrList<T>::iterator::operator()()
00136 {
00137     return operator*();
00138 }
00139 
00140 template<class T>
00141 inline typename PtrList<T>::iterator
00142 PtrList<T>::iterator::operator++()
00143 {
00144     ++ptr_;
00145     return *this;
00146 }
00147 
00148 template<class T>
00149 inline typename PtrList<T>::iterator
00150 PtrList<T>::iterator::operator++(int)
00151 {
00152     iterator tmp = *this;
00153     ++ptr_;
00154     return tmp;
00155 }
00156 
00157 template<class T>
00158 inline typename PtrList<T>::iterator
00159 PtrList<T>::iterator::operator--()
00160 {
00161     --ptr_;
00162     return *this;
00163 }
00164 
00165 template<class T>
00166 inline typename PtrList<T>::iterator
00167 PtrList<T>::iterator::operator--(int)
00168 {
00169     iterator tmp = *this;
00170     --ptr_;
00171     return tmp;
00172 }
00173 
00174 template<class T>
00175 inline typename PtrList<T>::iterator
00176 PtrList<T>::iterator::operator+=(label n)
00177 {
00178     ptr_ += n;
00179     return *this;
00180 }
00181 
00182 template<class T>
00183 inline typename PtrList<T>::iterator
00184 operator+(const typename PtrList<T>::iterator& iter, label n)
00185 {
00186     typename PtrList<T>::iterator tmp = iter;
00187     return tmp += n;
00188 }
00189 
00190 template<class T>
00191 inline typename PtrList<T>::iterator
00192 operator+(label n, const typename PtrList<T>::iterator& iter)
00193 {
00194     typename PtrList<T>::iterator tmp = iter;
00195     return tmp += n;
00196 }
00197 
00198 template<class T>
00199 inline typename PtrList<T>::iterator
00200 PtrList<T>::iterator::operator-=(label n)
00201 {
00202     ptr_ -= n;
00203     return *this;
00204 }
00205 
00206 template<class T>
00207 inline typename PtrList<T>::iterator
00208 operator-(const typename PtrList<T>::iterator& iter, label n)
00209 {
00210     typename PtrList<T>::iterator tmp = iter;
00211     return tmp -= n;
00212 }
00213 
00214 template<class T>
00215 inline label operator-
00216 (
00217     const typename PtrList<T>::iterator& iter1,
00218     const typename PtrList<T>::iterator& iter2
00219 )
00220 {
00221     return (iter1.ptr_ - iter2.ptr_)/sizeof(T*);
00222 }
00223 
00224 template<class T>
00225 inline T& PtrList<T>::iterator::operator[](label n)
00226 {
00227     return *(*this + n);
00228 }
00229 
00230 template<class T>
00231 inline bool PtrList<T>::iterator::operator<(const iterator& iter) const
00232 {
00233     return ptr_ < iter.ptr_;
00234 }
00235 
00236 template<class T>
00237 inline bool PtrList<T>::iterator::operator>(const iterator& iter) const
00238 {
00239     return ptr_ > iter.ptr_;
00240 }
00241 
00242 template<class T>
00243 inline bool PtrList<T>::iterator::operator<=(const iterator& iter) const
00244 {
00245     return ptr_ <= iter.ptr_;
00246 }
00247 
00248 template<class T>
00249 inline bool PtrList<T>::iterator::operator>=(const iterator& iter) const
00250 {
00251     return ptr_ >= iter.ptr_;
00252 }
00253 
00254 template<class T>
00255 inline typename PtrList<T>::iterator PtrList<T>::begin()
00256 {
00257     return ptrs_.begin();
00258 }
00259 
00260 template<class T>
00261 inline typename PtrList<T>::iterator PtrList<T>::end()
00262 {
00263     return ptrs_.end();
00264 }
00265 
00266 
00267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00268 
00269 } // End namespace Foam
00270 
00271 // ************************************************************************* //
For further information go to www.openfoam.org