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

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