UPtrListI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 template<class T>
33 {
34  ptrs_.setAddressableSize(n);
35 }
36 
37 
38 template<class T>
39 inline Foam::label Foam::UPtrList<T>::find_next(label pos) const
40 {
41  return ptrs_.find_next(pos);
42 }
43 
44 
45 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
46 
47 template<class T>
49 :
50  ptrs_()
51 {}
52 
53 
54 template<class T>
55 inline Foam::UPtrList<T>::UPtrList(const label len)
56 :
57  ptrs_(len)
58 {}
59 
60 
61 template<class T>
63 :
64  ptrs_(std::move(ptrs))
65 {}
66 
67 
68 template<class T>
70 :
71  ptrs_(list.ptrs_)
72 {}
73 
74 
75 template<class T>
77 :
78  ptrs_(std::move(list.ptrs_))
79 {}
80 
81 
82 template<class T>
83 inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& list, bool reuse)
84 :
85  ptrs_(list.ptrs_, reuse)
86 {}
87 
88 
89 template<class T>
91 :
92  ptrs_(list)
93 {}
94 
95 
96 template<class T>
98 :
99  ptrs_(list.size())
100 {
101  const label len = ptrs_.size();
102 
103  for (label i = 0; i < len; ++i)
104  {
105  ptrs_[i] = &(list[i]);
106  }
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
112 template<class T>
114 {
115  return ptrs_.empty();
116 }
117 
118 
119 template<class T>
120 inline Foam::label Foam::UPtrList<T>::size() const noexcept
121 {
122  return ptrs_.size();
123 }
124 
125 
126 template<class T>
127 inline Foam::label Foam::UPtrList<T>::count() const noexcept
128 {
129  return ptrs_.count();
130 }
131 
132 
133 template<class T>
134 inline const T* Foam::UPtrList<T>::test(const label i) const
135 {
136  return ptrs_.get(i);
137 }
138 
139 
140 template<class T>
141 inline const T* Foam::UPtrList<T>::get(const label i) const
142 {
143  return ptrs_.get(i);
144 }
145 
146 
147 template<class T>
148 inline T* Foam::UPtrList<T>::get(const label i)
149 {
150  return ptrs_.get(i);
151 }
152 
153 
154 template<class T>
155 inline const T& Foam::UPtrList<T>::at(const label i) const
156 {
157  const T* ptr = ptrs_.get(i);
158 
159  if (!ptr)
160  {
162  << "Cannot dereference nullptr at index " << i
163  << " in range [0," << size() << ")\n"
164  << abort(FatalError);
165  }
166 
167  return *ptr;
168 }
169 
170 
171 template<class T>
172 inline T& Foam::UPtrList<T>::at(const label i)
173 {
174  T* ptr = ptrs_.get(i);
175 
176  if (!ptr)
177  {
179  << "Cannot dereference nullptr at index " << i
180  << " in range [0," << size() << ")\n"
181  << abort(FatalError);
182  }
183 
184  return *ptr;
185 }
186 
187 
188 template<class T>
189 inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
190 {
191  T* old = ptrs_[i];
192  if (old == ptr)
193  {
194  return nullptr; // Content did not change
195  }
196  ptrs_[i] = ptr;
197  return old;
198 }
199 
200 
201 template<class T>
203 {
204  ptrs_.clear();
205 }
206 
207 
208 template<class T>
210 {
211  ptrs_.setNull();
212 }
213 
214 
215 template<class T>
217 {
218  ptrs_.swap(list.ptrs_);
219 }
220 
221 
222 template<class T>
224 {
225  ptrs_.transfer(list.ptrs_);
226 }
227 
228 
229 template<class T>
231 {
232  return this->at(0);
233 }
234 
235 
236 template<class T>
237 inline const T& Foam::UPtrList<T>::front() const
238 {
239  return this->at(0);
240 }
241 
242 
243 template<class T>
245 {
246  return this->at(this->size()-1);
247 }
248 
249 
250 template<class T>
251 inline const T& Foam::UPtrList<T>::back() const
252 {
253  return this->at(this->size()-1);
254 }
255 
256 
257 template<class T>
258 inline void Foam::UPtrList<T>::resize(const label newLen)
259 {
260  ptrs_.resize(newLen);
261 }
262 
263 
264 template<class T>
266 {
267  ptrs_.push_back(ptr);
268 }
269 
270 
271 template<class T>
272 inline void Foam::UPtrList<T>::push_back(UPtrList<T>&& other)
273 {
274  ptrs_.push_back(other.ptrs_);
275  other.ptrs_.clear();
276 }
277 
278 
279 template<class T>
280 inline void Foam::UPtrList<T>::checkNonNull() const
281 {
282  ptrs_.checkNonNull();
283 }
284 
285 
286 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
287 
288 template<class T>
289 inline const T& Foam::UPtrList<T>::operator[](const label i) const
290 {
291  return this->at(i);
292 }
293 
294 
295 template<class T>
296 inline T& Foam::UPtrList<T>::operator[](const label i)
297 {
298  return this->at(i);
299 }
301 
302 // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
303 
304 template<class T>
305 template<bool Const>
306 inline constexpr
308 :
309  list_(nullptr),
310  pos_(-1)
311 {}
312 
313 
314 template<class T>
315 template<bool Const>
317 (
318  list_type* list
319 )
320 :
321  list_(list),
322  pos_(-1)
323 {
324  if (list_)
325  {
326  pos_ = list_->find_next(-1);
327  }
328 }
329 
330 
331 template<class T>
332 template<bool Const>
334 {
335  //TDB: extra safety? if (iter.good())
336  {
337  pos_ = list_->find_next(pos_);
338  }
339 }
340 
341 
342 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
343 
344 template<class T>
346 {
347  return this->good() ? this->list_->get(this->pos_) : nullptr;
348 }
349 
350 
351 template<class T>
352 inline T& Foam::UPtrList<T>::iterator::val() const
353 {
354  return this->list_->at(this->pos_);
355 }
356 
357 
358 template<class T>
359 inline typename Foam::UPtrList<T>::iterator&
361 {
362  this->increment();
363  return *this;
364 }
365 
366 
367 template<class T>
368 inline typename Foam::UPtrList<T>::iterator
370 {
371  iterator iter(*this);
372  this->increment();
373  return iter;
374 }
375 
376 
377 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
378 
379 template<class T>
381 {
382  return this->good() ? this->list_->get(this->pos_) : nullptr;
383 }
384 
385 
386 template<class T>
387 inline const T& Foam::UPtrList<T>::const_iterator::val() const
388 {
389  return this->list_->at(this->pos_);
390 }
391 
392 
393 template<class T>
394 inline typename Foam::UPtrList<T>::const_iterator&
396 {
397  this->increment();
398  return *this;
399 }
400 
401 
402 template<class T>
403 inline typename Foam::UPtrList<T>::const_iterator
405 {
406  const_iterator iter(*this);
407  this->increment();
408  return iter;
409 }
410 
411 
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
413 
414 template<class T>
415 inline typename Foam::UPtrList<T>::iterator
417 {
418  return iterator(Iterator<false>(this));
419 }
420 
421 
422 template<class T>
423 inline typename Foam::UPtrList<T>::const_iterator
425 {
426  return const_iterator(Iterator<true>(this));
427 }
428 
429 
430 template<class T>
431 inline typename Foam::UPtrList<T>::const_iterator
433 {
434  return const_iterator(Iterator<true>(this));
435 }
436 
437 
438 template<class T>
439 inline typename Foam::UPtrList<T>::iterator
441 {
442  return iterator();
443 }
444 
445 
446 template<class T>
447 inline typename Foam::UPtrList<T>::const_iterator
449 {
451 }
452 
453 
454 template<class T>
455 inline typename Foam::UPtrList<T>::const_iterator
457 {
459 }
460 
461 
462 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
463 
464 template<class T>
466 {
467  ptrs_ = list.ptrs_; // shallow copy
468 }
469 
470 
471 template<class T>
472 inline void Foam::UPtrList<T>::operator=(UPtrList<T>&& list)
473 {
474  ptrs_.transfer(list.ptrs_);
475 }
476 
477 
478 // ************************************************************************* //
reference val() const
Reference to the object.
Definition: UPtrListI.H:380
list_type * list_
The parent being iterated.
Definition: UPtrList.H:557
const T & operator[](const label i) const
Return const reference to the element at given position. FatalError for bounds problem or nullptr...
Definition: UPtrListI.H:282
const T * test(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrListI.H:127
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
T & back()
Reference to the last element of the list.
Definition: UPtrListI.H:237
iterator & operator++()
Move to the next non-nullptr entry.
Definition: UPtrListI.H:353
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:57
const_iterator & operator++()
Move to the next non-nullptr entry.
Definition: UPtrListI.H:388
const T * get(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrListI.H:134
const T & at(const label i) const
Return const reference to the element at given position. FatalError for bounds problem or nullptr...
Definition: UPtrListI.H:148
T & front()
Reference to the first element of the list.
Definition: UPtrListI.H:223
Forward iterator with const access.
Definition: UPtrList.H:701
label pos_
The position within the list.
Definition: UPtrList.H:562
Forward iterator with non-const access.
Definition: UPtrList.H:638
dimensionedScalar pos(const dimensionedScalar &ds)
void free()
Nullify all entries. Does not change the list size.
Definition: UPtrListI.H:202
void push_back(T *ptr)
Append an element to the end of the list.
Definition: UPtrListI.H:258
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:113
void checkNonNull() const
Check and raise FatalError if any nullptr exists in the list.
Definition: UPtrListI.H:273
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition: UPtrListI.H:216
reference val() const
Reference to the object.
Definition: UPtrListI.H:345
friend class Iterator< true >
Allow iterator access to internals.
Definition: UPtrList.H:511
void resize(const label newLen)
Change the size of the list.
Definition: UPtrListI.H:251
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:209
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
errorManip< error > abort(error &err)
Definition: errorManip.H:139
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:99
const direction noexcept
Definition: Scalar.H:258
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
pointer get() const
Pointer to the referenced object (failsafe)
Definition: UPtrListI.H:338
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:458
label find_next(label pos) const
The next non-null entry after the specified position.
Definition: UPtrListI.H:32
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:106
iterator begin()
Return iterator to begin traversal of non-nullptr entries.
Definition: UPtrListI.H:409
iterator end() noexcept
Return iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:433
pointer get() const
Pointer to the referenced object (failsafe)
Definition: UPtrListI.H:373
label n
const_iterator cend() const noexcept
Return const_iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:449
bool good() const noexcept
True if iterator points to a non-null entry.
Definition: UPtrList.H:605
void clear()
Set list size to zero.
Definition: UPtrListI.H:195
const_iterator cbegin() const
Return const_iterator to begin traversal of non-nullptr entries.
Definition: UPtrListI.H:425
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:109
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition: UPtrListI.H:25
label count() const noexcept
The number of non-null entries in the list.
Definition: UPtrListI.H:120
friend class Iterator< false >
Allow iterator access to internals.
Definition: UPtrList.H:521
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrList.H:361