FreeFOAM The Cross-Platform CFD Toolkit
PtrList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include <OpenFOAM/error.H>
27 
28 #include "PtrList.H"
29 #include <OpenFOAM/SLPtrList.H>
30 
31 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
32 
33 template<class T>
35 :
36  ptrs_()
37 {}
38 
39 
40 template<class T>
42 :
43  ptrs_(s, reinterpret_cast<T*>(0))
44 {}
45 
46 
47 template<class T>
49 :
50  ptrs_(a.size())
51 {
52  forAll(*this, i)
53  {
54  ptrs_[i] = (a[i]).clone().ptr();
55  }
56 }
57 
58 
59 template<class T>
60 template<class CloneArg>
61 Foam::PtrList<T>::PtrList(const PtrList<T>& a, const CloneArg& cloneArg)
62 :
63  ptrs_(a.size())
64 {
65  forAll(*this, i)
66  {
67  ptrs_[i] = (a[i]).clone(cloneArg).ptr();
68  }
69 }
70 
71 
72 template<class T>
74 {
75  transfer(lst());
76 }
77 
78 
79 template<class T>
81 :
82  ptrs_(a.size())
83 {
84  if (reUse)
85  {
86  forAll(*this, i)
87  {
88  ptrs_[i] = a.ptrs_[i];
89  a.ptrs_[i] = NULL;
90  }
91  a.setSize(0);
92  }
93  else
94  {
95  forAll(*this, i)
96  {
97  ptrs_[i] = (a[i]).clone().ptr();
98  }
99  }
100 }
101 
102 
103 template<class T>
105 :
106  ptrs_(sll.size())
107 {
108  if (sll.size())
109  {
110  label i = 0;
111  for
112  (
113  typename SLPtrList<T>::const_iterator iter = sll.begin();
114  iter != sll.end();
115  ++iter
116  )
117  {
118  ptrs_[i++] = (iter()).clone().ptr();
119  }
120  }
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
125 
126 template<class T>
128 {
129  forAll(*this, i)
130  {
131  if (ptrs_[i])
132  {
133  delete ptrs_[i];
134  }
135  }
136 }
137 
138 
139 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
140 
141 template<class T>
142 void Foam::PtrList<T>::setSize(const label newSize)
143 {
144  if (newSize < 0)
145  {
146  FatalErrorIn("PtrList<T>::setSize(const label)")
147  << "bad set size " << newSize
148  << abort(FatalError);
149  }
150 
151  label oldSize = size();
152 
153  if (newSize == 0)
154  {
155  clear();
156  }
157  else if (newSize < oldSize)
158  {
159  register label i;
160  for (i=newSize; i<oldSize; i++)
161  {
162  if (ptrs_[i])
163  {
164  delete ptrs_[i];
165  }
166  }
167 
168  ptrs_.setSize(newSize);
169  }
170  else // newSize > oldSize
171  {
172  ptrs_.setSize(newSize);
173 
174  register label i;
175  for (i=oldSize; i<newSize; i++)
176  {
177  ptrs_[i] = NULL;
178  }
179  }
180 }
181 
182 
183 template<class T>
185 {
186  forAll(*this, i)
187  {
188  if (ptrs_[i])
189  {
190  delete ptrs_[i];
191  }
192  }
193 
194  ptrs_.clear();
195 }
196 
197 
198 template<class T>
200 {
201  clear();
202  ptrs_.transfer(a.ptrs_);
203 }
204 
205 
206 template<class T>
208 {
209  if (oldToNew.size() != size())
210  {
211  FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
212  << "Size of map (" << oldToNew.size()
213  << ") not equal to list size (" << size()
214  << ")." << abort(FatalError);
215  }
216 
217  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
218 
219  forAll(*this, i)
220  {
221  label newI = oldToNew[i];
222 
223  if (newI < 0 || newI >= size())
224  {
225  FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
226  << "Illegal index " << newI << nl
227  << "Valid indices are 0.." << size()-1
228  << abort(FatalError);
229  }
230 
231  if (newPtrs_[newI])
232  {
233  FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
234  << "reorder map is not unique; element " << newI
235  << " already set." << abort(FatalError);
236  }
237  newPtrs_[newI] = ptrs_[i];
238  }
239 
240  forAll(newPtrs_, i)
241  {
242  if (!newPtrs_[i])
243  {
244  FatalErrorIn("PtrList<T>::reorder(const UList<label>&)")
245  << "Element " << i << " not set after reordering." << nl
246  << abort(FatalError);
247  }
248  }
249 
250  ptrs_.transfer(newPtrs_);
251 }
252 
253 
254 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
255 
256 template<class T>
258 {
259  if (this == &a)
260  {
261  FatalErrorIn("PtrList<T>::operator=(const PtrList<T>&)")
262  << "attempted assignment to self"
263  << abort(FatalError);
264  }
265 
266  if (size() == 0)
267  {
268  setSize(a.size());
269 
270  forAll(*this, i)
271  {
272  ptrs_[i] = (a[i]).clone().ptr();
273  }
274  }
275  else if (a.size() == size())
276  {
277  forAll(*this, i)
278  {
279  (*this)[i] = a[i];
280  }
281  }
282  else
283  {
284  FatalErrorIn("PtrList::operator=(const PtrList<T>&)")
285  << "bad size: " << a.size()
286  << abort(FatalError);
287  }
288 
289 
290  return *this;
291 }
292 
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
296 #include "PtrListIO.C"
297 
298 // ************************ vim: set sw=4 sts=4 et: ************************ //