initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / PtrList / PtrList.C
blob11d997dd3f2fe6047b0ad9734c0de207404a302d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
29 #include "PtrList.H"
30 #include "SLPtrList.H"
32 // * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * * //
34 template<class T>
35 Foam::PtrList<T>::PtrList()
37     ptrs_()
41 template<class T>
42 Foam::PtrList<T>::PtrList(const label s)
44     ptrs_(s, reinterpret_cast<T*>(0))
48 template<class T>
49 Foam::PtrList<T>::PtrList(const PtrList<T>& a)
51     ptrs_(a.size())
53     forAll(*this, i)
54     {
55         ptrs_[i] = (a[i]).clone().ptr();
56     }
60 template<class T>
61 template<class CloneArg>
62 Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
64     ptrs_(a.size())
66     forAll(*this, i)
67     {
68         ptrs_[i] = (a[i]).clone(cloneArg).ptr();
69     }
73 template<class T>
74 Foam::PtrList<T>::PtrList(const Xfer<PtrList<T> >& lst)
76     transfer(lst());
80 template<class T>
81 Foam::PtrList<T>::PtrList(PtrList<T>& a, bool reUse)
83     ptrs_(a.size())
85     if (reUse)
86     {
87         forAll(*this, i)
88         {
89             ptrs_[i] = a.ptrs_[i];
90             a.ptrs_[i] = NULL;
91         }
92         a.setSize(0);
93     }
94     else
95     {
96         forAll(*this, i)
97         {
98             ptrs_[i] = (a[i]).clone().ptr();
99         }
100     }
104 template<class T>
105 Foam::PtrList<T>::PtrList(const SLPtrList<T>& sll)
107     ptrs_(sll.size())
109     if (sll.size())
110     {
111         label i = 0;
112         for
113         (
114             typename SLPtrList<T>::const_iterator iter = sll.begin();
115             iter != sll.end();
116             ++iter
117         )
118         {
119             ptrs_[i++] = (iter()).clone().ptr();
120         }
121     }
125 // * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * * //
127 template<class T>
128 Foam::PtrList<T>::~PtrList()
130     forAll(*this, i)
131     {
132         if (ptrs_[i])
133         {
134             delete ptrs_[i];
135         }
136     }
140 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
142 template<class T>
143 void Foam::PtrList<T>::setSize(const label newSize)
145     if (newSize < 0)
146     {
147         FatalErrorIn("PtrList<T>::setSize(const label)")
148             << "bad set size " << newSize
149             << abort(FatalError);
150     }
152     label oldSize = size();
154     if (newSize == 0)
155     {
156         clear();
157     }
158     else if (newSize < oldSize)
159     {
160         register label i;
161         for (i=newSize; i<oldSize; i++)
162         {
163             if (ptrs_[i])
164             {
165                 delete ptrs_[i];
166             }
167         }
169         ptrs_.setSize(newSize);
170     }
171     else // newSize > oldSize
172     {
173         ptrs_.setSize(newSize);
175         register label i;
176         for (i=oldSize; i<newSize; i++)
177         {
178             ptrs_[i] = NULL;
179         }
180     }
184 template<class T>
185 void Foam::PtrList<T>::clear()
187     forAll(*this, i)
188     {
189         if (ptrs_[i])
190         {
191             delete ptrs_[i];
192         }
193     }
195     ptrs_.clear();
199 template<class T>
200 void Foam::PtrList<T>::transfer(PtrList<T>& a)
202     clear();
203     ptrs_.transfer(a.ptrs_);
207 template<class T>
208 void Foam::PtrList<T>::reorder(const UList<label>& oldToNew)
210     if (oldToNew.size() != size())
211     {
212         FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
213             << "Size of map (" << oldToNew.size()
214             << ") not equal to list size (" << size()
215             << ")." << abort(FatalError);
216     }
218     List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
220     forAll(*this, i)
221     {
222         label newI = oldToNew[i];
224         if (newI < 0 || newI >= size())
225         {
226             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
227                 << "Illegal index " << newI << nl
228                 << "Valid indices are 0.." << size()-1
229                 << abort(FatalError);
230         }
232         if (newPtrs_[newI])
233         {
234             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
235                 << "reorder map is not unique; element " << newI
236                 << " already set." << abort(FatalError);
237         }
238         newPtrs_[newI] = ptrs_[i];
239     }
241     forAll(newPtrs_, i)
242     {
243         if (!newPtrs_[i])
244         {
245             FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
246                 << "Element " << i << " not set after reordering." << nl
247                 << abort(FatalError);
248         }
249     }
251     ptrs_.transfer(newPtrs_);
255 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
257 template<class T>
258 Foam::PtrList<T>& Foam::PtrList<T>::operator=(const PtrList<T>& a)
260     if (this == &a)
261     {
262         FatalErrorIn("PtrList<T>::operator=(const PtrList<T>&)")
263             << "attempted assignment to self"
264             << abort(FatalError);
265     }
267     if (size() == 0)
268     {
269         setSize(a.size());
271         forAll(*this, i)
272         {
273             ptrs_[i] = (a[i]).clone().ptr();
274         }
275     }
276     else if (a.size() == size())
277     {
278         forAll(*this, i)
279         {
280             (*this)[i] = a[i];
281         }
282     }
283     else
284     {
285         FatalErrorIn("PtrList::operator=(const PtrList<T>&)")
286             << "bad size: " << a.size()
287             << abort(FatalError);
288     }
291     return *this;
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297 #include "PtrListIO.C"
299 // ************************************************************************* //