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) 2011-2015 OpenFOAM Foundation
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 "error.H"
27 
28 #include "PtrList.H"
29 #include "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  {
147  << "bad set size " << newSize
148  << " for type " << typeid(T).name()
149  << abort(FatalError);
150  }
151 
152  label oldSize = size();
153 
154  if (newSize == 0)
155  {
156  clear();
157  }
158  else if (newSize < oldSize)
159  {
160  label i;
161  for (i=newSize; i<oldSize; i++)
162  {
163  if (ptrs_[i])
164  {
165  delete ptrs_[i];
166  }
167  }
168 
169  ptrs_.setSize(newSize);
170  }
171  else // newSize > oldSize
172  {
173  ptrs_.setSize(newSize);
174 
175  label i;
176  for (i=oldSize; i<newSize; i++)
177  {
178  ptrs_[i] = NULL;
179  }
180  }
181 }
182 
183 
184 template<class T>
186 {
187  forAll(*this, i)
188  {
189  if (ptrs_[i])
190  {
191  delete ptrs_[i];
192  }
193  }
194 
195  ptrs_.clear();
196 }
197 
198 
199 template<class T>
201 {
202  clear();
203  ptrs_.transfer(a.ptrs_);
204 }
205 
206 
207 template<class T>
209 {
210  if (oldToNew.size() != size())
211  {
213  << "Size of map (" << oldToNew.size()
214  << ") not equal to list size (" << size()
215  << ") for type " << typeid(T).name()
216  << abort(FatalError);
217  }
218 
219  List<T*> newPtrs_(ptrs_.size(), reinterpret_cast<T*>(0));
220 
221  forAll(*this, i)
222  {
223  label newI = oldToNew[i];
224 
225  if (newI < 0 || newI >= size())
226  {
228  << "Illegal index " << newI << nl
229  << "Valid indices are 0.." << size()-1
230  << " for type " << typeid(T).name()
231  << abort(FatalError);
232  }
233 
234  if (newPtrs_[newI])
235  {
237  << "reorder map is not unique; element " << newI
238  << " already set for type " << typeid(T).name()
239  << abort(FatalError);
240  }
241  newPtrs_[newI] = ptrs_[i];
242  }
243 
244  forAll(newPtrs_, i)
245  {
246  if (!newPtrs_[i])
247  {
249  << "Element " << i << " not set after reordering with type "
250  << typeid(T).name() << nl << abort(FatalError);
251  }
252  }
253 
254  ptrs_.transfer(newPtrs_);
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
259 
260 template<class T>
262 {
263  if (this == &a)
264  {
266  << "attempted assignment to self for type " << typeid(T).name()
267  << abort(FatalError);
268  }
269 
270  if (size() == 0)
271  {
272  setSize(a.size());
273 
274  forAll(*this, i)
275  {
276  ptrs_[i] = (a[i]).clone().ptr();
277  }
278  }
279  else if (a.size() == size())
280  {
281  forAll(*this, i)
282  {
283  (*this)[i] = a[i];
284  }
285  }
286  else
287  {
289  << "bad size: " << a.size()
290  << " for type " << typeid(T).name()
291  << abort(FatalError);
292  }
293 
294 
295  return *this;
296 }
297 
298 
299 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300 
301 #include "PtrListIO.C"
302 
303 // ************************************************************************* //
reinterpret_cast
const Foam::edgeFaceCirculator Foam::edgeFaceCirculator::endConstIter * reinterpret_cast(0), -1, false, -1, false
setSize
points setSize(newPointi)
clear
UEqn clear()
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
SLPtrList.H
Foam::PtrList::PtrList
PtrList()
Null Constructor.
Foam::PtrList::~PtrList
~PtrList()
Destructor.
Definition: PtrList.C:127
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::SLPtrList
Non-intrusive singly-linked pointer list.
Definition: SLPtrList.H:47
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
error.H
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::PtrList< T >
Foam::PtrList::ptrs_
List< T * > ptrs_
Definition: PtrList.H:130
Foam::PtrList::transfer
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
PtrListIO.C
Foam::PtrList::reorder
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:208
Foam::FatalError
error FatalError
Foam::PtrList::clear
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
s
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
T
const volScalarField & T
Definition: createFields.H:25
Foam::List< T * >
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
PtrList.H
Foam::UList::size
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::PtrList::operator=
PtrList< T > & operator=(const PtrList< T > &)
Assignment.
Definition: PtrList.C:261