OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

PtrList.C

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-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 #include "PtrList.H"
00030 #include "SLPtrList.H"
00031 
00032 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
00033 
00034 template<class T>
00035 Foam::PtrList<T>::PtrList()
00036 :
00037     ptrs_()
00038 {}
00039 
00040 
00041 template<class T>
00042 Foam::PtrList<T>::PtrList(const label s)
00043 :
00044     ptrs_(s, reinterpret_cast<T*>(0))
00045 {}
00046 
00047 
00048 template<class T>
00049 Foam::PtrList<T>::PtrList(const PtrList<T>& a)
00050 :
00051     ptrs_(a.size())
00052 {
00053     forAll(*this, i)
00054     {
00055         ptrs_[i] = (a[i]).clone().ptr();
00056     }
00057 }
00058 
00059 
00060 template<class T>
00061 template<class CloneArg>
00062 Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
00063 :
00064     ptrs_(a.size())
00065 {
00066     forAll(*this, i)
00067     {
00068         ptrs_[i] = (a[i]).clone(cloneArg).ptr();
00069     }
00070 }
00071 
00072 
00073 template<class T>
00074 Foam::PtrList<T>::PtrList(const Xfer<PtrList<T> >& lst)
00075 {
00076     transfer(lst());
00077 }
00078 
00079 
00080 template<class T>
00081 Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reUse)
00082 :
00083     ptrs_(a.size())
00084 {
00085     if (reUse)
00086     {
00087         forAll(*this, i)
00088         {
00089             ptrs_[i] = a.ptrs_[i];
00090             a.ptrs_[i] = NULL;
00091         }
00092         a.setSize(0);
00093     }
00094     else
00095     {
00096         forAll(*this, i)
00097         {
00098             ptrs_[i] = (a[i]).clone().ptr();
00099         }
00100     }
00101 }
00102 
00103 
00104 template<class T>
00105 Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll)
00106 :
00107     ptrs_(sll.size())
00108 {
00109     if (sll.size())
00110     {
00111         label i = 0;
00112         for
00113         (
00114             typename SLPtrList<T>::const_iterator iter = sll.begin();
00115             iter != sll.end();
00116             ++iter
00117         )
00118         {
00119             ptrs_[i++] = (iter()).clone().ptr();
00120         }
00121     }
00122 }
00123 
00124 
00125 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
00126 
00127 template<class T>
00128 Foam::PtrList<T>::~PtrList()
00129 {
00130     forAll(*this, i)
00131     {
00132         if (ptrs_[i])
00133         {
00134             delete ptrs_[i];
00135         }
00136     }
00137 }
00138 
00139 
00140 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00141 
00142 template<class T>
00143 void Foam::PtrList<T>::setSize(const label newSize)
00144 {
00145     if (newSize < 0)
00146     {
00147         FatalErrorIn("PtrList<T>::setSize(const label)")
00148             << "bad set size " << newSize
00149             << abort(FatalError);
00150     }
00151 
00152     label oldSize = size();
00153 
00154     if (newSize == 0)
00155     {
00156         clear();
00157     }
00158     else if (newSize < oldSize)
00159     {
00160         register label i;
00161         for (i=newSize; i<oldSize; i++)
00162         {
00163             if (ptrs_[i])
00164             {
00165                 delete ptrs_[i];
00166             }
00167         }
00168 
00169         ptrs_.setSize(newSize);
00170     }
00171     else // newSize > oldSize
00172     {
00173         ptrs_.setSize(newSize);
00174 
00175         register label i;
00176         for (i=oldSize; i<newSize; i++)
00177         {
00178             ptrs_[i] = NULL;
00179         }
00180     }
00181 }
00182 
00183 
00184 template<class T>
00185 void Foam::PtrList<T>::clear()
00186 {
00187     forAll(*this, i)
00188     {
00189         if (ptrs_[i])
00190         {
00191             delete ptrs_[i];
00192         }
00193     }
00194 
00195     ptrs_.clear();
00196 }
00197 
00198 
00199 template<class T>
00200 void Foam::PtrList<T>::transfer(PtrList<T>& a)
00201 {
00202     clear();
00203     ptrs_.transfer(a.ptrs_);
00204 }
00205 
00206 
00207 template<class T>
00208 void Foam::PtrList<T>::reorder(const UList<label>& oldToNew)
00209 {
00210     if (oldToNew.size() != size())
00211     {
00212         FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
00213             << "Size of map (" << oldToNew.size()
00214             << ") not equal to list size (" << size()
00215             << ")." << abort(FatalError);
00216     }
00217 
00218     List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
00219 
00220     forAll(*this, i)
00221     {
00222         label newI = oldToNew[i];
00223 
00224         if (newI < 0 || newI >= size())
00225         {
00226             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
00227                 << "Illegal index " << newI << nl
00228                 << "Valid indices are 0.." << size()-1
00229                 << abort(FatalError);
00230         }
00231 
00232         if (newPtrs_[newI])
00233         {
00234             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
00235                 << "reorder map is not unique; element " << newI
00236                 << " already set." << abort(FatalError);
00237         }
00238         newPtrs_[newI] = ptrs_[i];
00239     }
00240 
00241     forAll(newPtrs_, i)
00242     {
00243         if (!newPtrs_[i])
00244         {
00245             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
00246                 << "Element " << i << " not set after reordering." << nl
00247                 << abort(FatalError);
00248         }
00249     }
00250 
00251     ptrs_.transfer(newPtrs_);
00252 }
00253 
00254 
00255 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00256 
00257 template<class T>
00258 Foam::PtrList<T>& Foam::PtrList<T>::operator=(const PtrList<T>& a)
00259 {
00260     if (this == &a)
00261     {
00262         FatalErrorIn("PtrList<T>::operator=(const PtrList<T>&)")
00263             << "attempted assignment to self"
00264             << abort(FatalError);
00265     }
00266 
00267     if (size() == 0)
00268     {
00269         setSize(a.size());
00270 
00271         forAll(*this, i)
00272         {
00273             ptrs_[i] = (a[i]).clone().ptr();
00274         }
00275     }
00276     else if (a.size() == size())
00277     {
00278         forAll(*this, i)
00279         {
00280             (*this)[i] = a[i];
00281         }
00282     }
00283     else
00284     {
00285         FatalErrorIn("PtrList::operator=(const PtrList<T>&)")
00286             << "bad size: " << a.size()
00287             << abort(FatalError);
00288     }
00289 
00290 
00291     return *this;
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00296 
00297 #include "PtrListIO.C"
00298 
00299 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd