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>
48 inline Foam::UPtrList<T>::UPtrList(const label len)
49 :
50  ptrs_(len)
51 {}
52 
53 
54 template<class T>
56 :
57  ptrs_(std::move(ptrs))
58 {}
59 
60 
61 template<class T>
63 :
64  ptrs_(list.ptrs_)
65 {}
66 
67 
68 template<class T>
70 :
71  ptrs_(std::move(list.ptrs_))
72 {}
73 
74 
75 template<class T>
76 inline Foam::UPtrList<T>::UPtrList(UPtrList<T>& list, bool reuse)
77 :
78  ptrs_(list.ptrs_, reuse)
79 {}
80 
81 
82 template<class T>
84 :
85  ptrs_(list)
86 {}
87 
88 
89 template<class T>
91 :
92  ptrs_(list.size())
93 {
94  const label len = ptrs_.size();
95 
96  for (label i = 0; i < len; ++i)
97  {
98  ptrs_[i] = &(list[i]);
99  }
100 }
101 
102 
103 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104 
105 template<class T>
107 {
108  return ptrs_.empty();
109 }
110 
111 
112 template<class T>
113 inline Foam::label Foam::UPtrList<T>::size() const noexcept
114 {
115  return ptrs_.size();
116 }
117 
118 
119 template<class T>
120 inline Foam::label Foam::UPtrList<T>::capacity() const noexcept
121 {
122  return ptrs_.capacity();
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_ = nullptr;
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>
265 inline void Foam::UPtrList<T>::resize_null(const label newLen)
266 {
267  ptrs_.resize_null(newLen);
268 }
269 
270 
271 template<class T>
273 {
274  ptrs_.push_back(ptr);
275 }
276 
277 
278 template<class T>
279 inline void Foam::UPtrList<T>::push_back(UPtrList<T>&& other)
280 {
281  ptrs_.push_back(other.ptrs_);
282  other.ptrs_.clear();
283 }
284 
285 
286 template<class T>
287 inline void Foam::UPtrList<T>::checkNonNull() const
288 {
289  ptrs_.checkNonNull();
290 }
291 
292 
293 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
294 
295 template<class T>
296 inline const T& Foam::UPtrList<T>::operator[](const label i) const
297 {
298  return this->at(i);
299 }
300 
301 
302 template<class T>
303 inline T& Foam::UPtrList<T>::operator[](const label i)
304 {
305  return this->at(i);
306 }
308 
309 // * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //
310 
311 template<class T>
312 template<bool Const>
313 inline constexpr
315 :
316  list_(nullptr),
317  pos_(-1)
318 {}
319 
320 
321 template<class T>
322 template<bool Const>
324 (
325  list_type* list
326 )
327 :
328  list_(list),
329  pos_(-1)
330 {
331  if (list_)
332  {
333  pos_ = list_->find_next(-1);
334  }
335 }
336 
337 
338 template<class T>
339 template<bool Const>
341 {
342  //TDB: extra safety? if (iter.good())
343  {
344  pos_ = list_->find_next(pos_);
345  }
346 }
347 
348 
349 // * * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * //
350 
351 template<class T>
353 {
354  return this->good() ? this->list_->get(this->pos_) : nullptr;
355 }
356 
357 
358 template<class T>
359 inline T& Foam::UPtrList<T>::iterator::val() const
360 {
361  return this->list_->at(this->pos_);
362 }
363 
364 
365 template<class T>
366 inline typename Foam::UPtrList<T>::iterator&
368 {
369  this->increment();
370  return *this;
371 }
372 
373 
374 template<class T>
375 inline typename Foam::UPtrList<T>::iterator
377 {
378  iterator iter(*this);
379  this->increment();
380  return iter;
381 }
382 
383 
384 // * * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * //
385 
386 template<class T>
388 {
389  return this->good() ? this->list_->get(this->pos_) : nullptr;
390 }
391 
392 
393 template<class T>
394 inline const T& Foam::UPtrList<T>::const_iterator::val() const
395 {
396  return this->list_->at(this->pos_);
397 }
398 
399 
400 template<class T>
401 inline typename Foam::UPtrList<T>::const_iterator&
403 {
404  this->increment();
405  return *this;
406 }
407 
408 
409 template<class T>
410 inline typename Foam::UPtrList<T>::const_iterator
412 {
413  const_iterator iter(*this);
414  this->increment();
415  return iter;
416 }
417 
418 
419 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
420 
421 template<class T>
422 inline typename Foam::UPtrList<T>::iterator
424 {
425  return iterator(Iterator<false>(this));
426 }
427 
428 
429 template<class T>
430 inline typename Foam::UPtrList<T>::const_iterator
432 {
433  return const_iterator(Iterator<true>(this));
434 }
435 
436 
437 template<class T>
438 inline typename Foam::UPtrList<T>::const_iterator
440 {
441  return const_iterator(Iterator<true>(this));
442 }
443 
444 
445 template<class T>
446 inline typename Foam::UPtrList<T>::iterator
448 {
449  return iterator();
450 }
451 
452 
453 template<class T>
454 inline typename Foam::UPtrList<T>::const_iterator
456 {
458 }
459 
460 
461 template<class T>
462 inline typename Foam::UPtrList<T>::const_iterator
464 {
466 }
467 
468 
469 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
470 
471 template<class T>
473 {
474  ptrs_ = list.ptrs_; // shallow copy
475 }
476 
477 
478 template<class T>
479 inline void Foam::UPtrList<T>::operator=(UPtrList<T>&& list)
480 {
481  ptrs_.transfer(list.ptrs_);
482 }
483 
484 
485 // ************************************************************************* //
reference val() const
Reference to the object.
Definition: UPtrListI.H:387
list_type * list_
The parent being iterated.
Definition: UPtrList.H:567
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:289
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
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:598
T & back()
Reference to the last element of the list.
Definition: UPtrListI.H:237
void swap(UPtrList< T > &list) noexcept
Swap content.
Definition: UPtrListI.H:209
iterator & operator++()
Move to the next non-nullptr entry.
Definition: UPtrListI.H:360
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:395
constexpr UPtrList() noexcept=default
Default construct.
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:711
label pos_
The position within the list.
Definition: UPtrList.H:572
Forward iterator with non-const access.
Definition: UPtrList.H:648
label capacity() const noexcept
Size of the underlying storage.
Definition: UPtrListI.H:113
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:265
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
void checkNonNull() const
Check and raise FatalError if any nullptr exists in the list.
Definition: UPtrListI.H:280
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:352
friend class Iterator< true >
Allow iterator access to internals.
Definition: UPtrList.H:521
void resize(const label newLen)
Change the size of the list. Any new entries are nullptr.
Definition: UPtrListI.H:251
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
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:105
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:345
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:465
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:99
iterator begin()
Return iterator to begin traversal of non-nullptr entries.
Definition: UPtrListI.H:416
iterator end() noexcept
Return iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:440
pointer get() const
Pointer to the referenced object (failsafe)
Definition: UPtrListI.H:380
label n
const_iterator cend() const noexcept
Return const_iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:456
bool good() const noexcept
True if iterator points to a non-null entry.
Definition: UPtrList.H:615
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:432
void resize_null(const label newLen)
Set the list to the given size and set all entries to nullptr.
Definition: UPtrListI.H:258
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:531
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:366