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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
31template<class T>
32inline void Foam::UPtrList<T>::setAddressableSize(const label n) noexcept
33{
34 ptrs_.setAddressableSize(n);
35}
36
37
38// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
39
40template<class T>
41inline constexpr Foam::UPtrList<T>::UPtrList() noexcept
42:
43 ptrs_()
44{}
45
46
47template<class T>
48inline Foam::UPtrList<T>::UPtrList(const label len)
49:
50 ptrs_(len)
51{}
52
53
54template<class T>
56:
57 ptrs_(std::move(ptrs))
58{}
59
60
61template<class T>
63:
64 ptrs_(list.ptrs_)
65{}
66
67
68template<class T>
70:
71 ptrs_(std::move(list.ptrs_))
72{}
73
74
75template<class T>
77:
78 ptrs_(list.ptrs_, reuse)
79{}
80
81
82template<class T>
84:
85 ptrs_(list)
86{}
87
88
89template<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
105template<class T>
106inline Foam::label Foam::UPtrList<T>::size() const noexcept
107{
108 return ptrs_.size();
109}
110
111
112template<class T>
114{
115 return ptrs_.empty();
116}
117
118
119template<class T>
120inline const T* Foam::UPtrList<T>::test(const label i) const
121{
122 return (i >= 0 && i < ptrs_.size()) ? ptrs_[i] : nullptr;
123}
124
125
126template<class T>
127inline T* Foam::UPtrList<T>::get(const label i)
128{
129 return ptrs_[i];
130}
131
132
133template<class T>
134inline const T* Foam::UPtrList<T>::get(const label i) const
135{
136 return ptrs_[i];
137}
138
139
140template<class T>
142{
143 ptrs_.clear();
144}
145
146
147template<class T>
149{
150 ptrs_.swap(list.ptrs_);
151}
152
153
154template<class T>
156{
157 ptrs_.transfer(list.ptrs_);
158}
159
160
161template<class T>
163{
164 return this->operator[](0);
165}
166
167
168template<class T>
169inline const T& Foam::UPtrList<T>::first() const
170{
171 return this->operator[](0);
172}
173
174
175template<class T>
177{
178 return this->operator[](this->size()-1);
179}
180
182template<class T>
183inline const T& Foam::UPtrList<T>::last() const
185 return this->operator[](this->size()-1);
186}
188
189template<class T>
190inline void Foam::UPtrList<T>::resize(const label newLen)
191{
192 ptrs_.resize(newLen);
194
195
196template<class T>
198{
199 ptrs_.append(ptr);
200}
202
203template<class T>
205{
206 ptrs_.append(other.ptrs_);
207 other.ptrs_.clear();
208}
209
210
211template<class T>
212inline T* Foam::UPtrList<T>::set(const label i, T* ptr)
213{
214 T* old = ptrs_[i];
215 if (old == ptr)
216 {
217 return nullptr; // Content did not change
218 }
219 ptrs_[i] = ptr;
220 return old;
221}
222
224template<class T>
227 ptrs_.checkNonNull();
228}
230
231// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
232
233template<class T>
234inline const T& Foam::UPtrList<T>::operator[](const label i) const
235{
236 const T* ptr = ptrs_[i];
237
238 if (!ptr)
241 << "Cannot dereference nullptr at index " << i
242 << " in range [0," << size() << ")\n"
244 }
245
246 return *ptr;
247}
248
249
250template<class T>
251inline T& Foam::UPtrList<T>::operator[](const label i)
252{
253 T* ptr = ptrs_[i];
255 if (!ptr)
256 {
258 << "Cannot dereference nullptr at index " << i
259 << " in range [0," << size() << ")\n"
260 << abort(FatalError);
261 }
262
263 return *ptr;
264}
265
266
267template<class T>
268inline const T* Foam::UPtrList<T>::operator()(const label i) const
269{
270 return ptrs_[i];
272
273
274// * * * * * * * * * * * * * * * * iterator * * * * * * * * * * * * * * * * //
275
276template<class T>
278iterator(T** ptr) noexcept
279:
280 ptr_(ptr)
281{}
283
284template<class T>
286{
287 return *ptr_;
288}
289
290
291template<class T>
293{
294 return *ptr_;
295}
296
297
298template<class T>
300{
301 return **ptr_;
302}
303
305template<class T>
308 return **(ptr_ + n);
309}
311template<class T>
312inline typename Foam::UPtrList<T>::iterator&
314{
315 ++ptr_;
316 return *this;
317}
318
319
320template<class T>
321inline typename Foam::UPtrList<T>::iterator
323{
324 iterator iter(*this);
325 ++ptr_;
326 return iter;
327}
328
329
330template<class T>
331inline typename Foam::UPtrList<T>::iterator&
333{
334 --ptr_;
335 return *this;
336}
338
339template<class T>
340inline typename Foam::UPtrList<T>::iterator
343 iterator iter(*this);
344 --ptr_;
345 return iter;
348
349template<class T>
352{
353 ptr_ += n;
354 return *this;
355}
358template<class T>
359inline typename Foam::UPtrList<T>::iterator&
362 ptr_ -= n;
363 return *this;
364}
366
367template<class T>
370{
371 return iterator(ptr_ + n);
373
375template<class T>
376inline typename Foam::UPtrList<T>::iterator
378{
379 return iterator(ptr_ - n);
380}
381
382
383template<class T>
384inline Foam::label
386operator-(const iterator& iter) const noexcept
387{
388 return (ptr_ - iter.ptr_);
389}
390
391
392template<class T>
394operator==(const iterator& iter) const noexcept
395{
396 return ptr_ == iter.ptr_;
398
399
400template<class T>
402operator!=(const iterator& iter) const noexcept
403{
404 return ptr_ != iter.ptr_;
405}
408template<class T>
410operator<(const iterator& iter) const noexcept
411{
412 return ptr_ < iter.ptr_;
415
416template<class T>
418operator>(const iterator& iter) const noexcept
419{
420 return ptr_ > iter.ptr_;
424template<class T>
426operator<=(const iterator& iter) const noexcept
427{
428 return ptr_ <= iter.ptr_;
430
432template<class T>
434operator>=(const iterator& iter) const noexcept
436 return ptr_ >= iter.ptr_;
437}
438
439
440// * * * * * * * * * * * * * * * const_iterator * * * * * * * * * * * * * * //
441
442template<class T>
444const_iterator(const T* const* ptr) noexcept
445:
446 ptr_(ptr)
448
449
450template<class T>
454 ptr_(iter.ptr_)
455{}
457
458template<class T>
460{
461 return *ptr_;
463
464
465template<class T>
467{
468 return *ptr_;
469}
470
471
472template<class T>
474{
475 return **ptr_;
476}
477
478
479template<class T>
480inline const T&
482{
483 return **(ptr_ + n);
484}
485
486
487template<class T>
490{
491 ++ptr_;
492 return *this;
493}
494
495
496template<class T>
499{
500 const_iterator iter(*this);
501 ++ptr_;
502 return iter;
503}
504
505
506template<class T>
509{
510 --ptr_;
511 return *this;
512}
513
514
515template<class T>
518{
519 const_iterator iter(*this);
520 --ptr_;
521 return iter;
522}
523
524
525template<class T>
528{
529 ptr_ += n;
530 return *this;
531}
532
533
534template<class T>
537{
538 ptr_ -= n;
539 return *this;
540}
541
542
543template<class T>
546{
547 return const_iterator(ptr_ + n);
548}
549
550
551template<class T>
554{
555 return const_iterator(ptr_ - n);
556}
557
558
559template<class T>
560inline Foam::label
562operator-(const const_iterator& iter) const noexcept
563{
564 return (ptr_ - iter.ptr_);
565}
566
567
568template<class T>
570operator==(const const_iterator& iter) const noexcept
571{
572 return ptr_ == iter.ptr_;
573}
574
575
576template<class T>
578operator!=(const const_iterator& iter) const noexcept
579{
580 return ptr_ != iter.ptr_;
581}
582
583
584template<class T>
586operator<(const const_iterator& iter) const noexcept
587{
588 return ptr_ < iter.ptr_;
589}
590
591
592template<class T>
594operator>(const const_iterator& iter) const noexcept
595{
596 return ptr_ > iter.ptr_;
597}
598
599
600template<class T>
602operator<=(const const_iterator& iter) const noexcept
603{
604 return ptr_ <= iter.ptr_;
605}
606
607
608template<class T>
610operator>=(const const_iterator& iter) const noexcept
611{
612 return ptr_ >= iter.ptr_;
613}
614
615
616// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
617
618template<class T>
619inline typename Foam::UPtrList<T>::iterator
621{
622 return ptrs_.begin();
623}
624
625
626template<class T>
627inline typename Foam::UPtrList<T>::iterator
629{
630 return ptrs_.end();
631}
632
633
634template<class T>
637{
638 return ptrs_.cbegin();
639}
640
641
642template<class T>
645{
646 return ptrs_.cend();
647}
648
649
650template<class T>
653{
654 return ptrs_.begin();
655}
656
657
658template<class T>
661{
662 return ptrs_.end();
663}
664
665
666// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
667
668template<class T>
670{
671 ptrs_ = list.ptrs_; // shallow copy
672}
673
674
675template<class T>
677{
678 ptrs_.transfer(list.ptrs_);
679}
680
681
682// ************************************************************************* //
label n
A rudimentary list of pointers used for PtrList, UPtrList, etc. This class is considered implementati...
Definition: PtrListDetail.H:64
virtual bool resize()
Resize the ODE solver.
Definition: Euler.C:53
unsigned int get() const
Get value as unsigned, no range-checking.
Definition: PackedListI.H:302
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Random-access iterator with const access.
Definition: UPtrList.H:381
reference operator*() const
Definition: UPtrListI.H:473
const_iterator & operator+=(difference_type n) noexcept
Definition: UPtrListI.H:527
bool operator<=(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:602
bool operator>=(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:610
const_iterator & operator++() noexcept
Definition: UPtrListI.H:489
const_iterator & operator--() noexcept
Definition: UPtrListI.H:508
bool operator>(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:594
const_iterator & operator-=(difference_type n) noexcept
Definition: UPtrListI.H:536
pointer operator->() const
Definition: UPtrListI.H:466
bool operator<(const const_iterator &iter) const noexcept
Definition: UPtrListI.H:586
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:459
reference operator[](difference_type n) const
Definition: UPtrListI.H:481
Random-access iterator with non-const access.
Definition: UPtrList.H:323
reference operator*() const
Definition: UPtrListI.H:299
iterator & operator--() noexcept
Definition: UPtrListI.H:332
iterator & operator+=(difference_type n) noexcept
Definition: UPtrListI.H:351
bool operator>(const iterator &iter) const noexcept
Definition: UPtrListI.H:418
pointer operator->() const
Definition: UPtrListI.H:292
bool operator<=(const iterator &iter) const noexcept
Definition: UPtrListI.H:426
iterator & operator-=(difference_type n) noexcept
Definition: UPtrListI.H:360
bool operator>=(const iterator &iter) const noexcept
Definition: UPtrListI.H:434
bool operator<(const iterator &iter) const noexcept
Definition: UPtrListI.H:410
reference operator[](difference_type n) const
Definition: UPtrListI.H:306
pointer get() const
Return pointer, can be nullptr.
Definition: UPtrListI.H:285
iterator & operator++() noexcept
Definition: UPtrListI.H:313
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
const T & operator[](const label i) const
Return const reference to the element.
Definition: UPtrListI.H:234
void swap(UPtrList< T > &list)
Swap content.
Definition: UPtrListI.H:148
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:162
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:113
void operator=(const UPtrList< T > &list)
Copy assignment (shallow copies addresses)
Definition: UPtrListI.H:669
const_iterator cbegin() const noexcept
Return const_iterator to begin of UPtrList traversal.
Definition: UPtrListI.H:636
constexpr UPtrList() noexcept
Default construct.
Definition: UPtrListI.H:41
const T * test(const label i) const
Definition: UPtrListI.H:120
const_iterator cend() const noexcept
Return const_iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:644
label size() const noexcept
The number of elements in the list.
Definition: UPtrListI.H:106
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition: UPtrList.H:77
void clear()
Set list size to zero.
Definition: UPtrListI.H:141
void checkNonNull() const
Check and raise FatalError if any nullptr exists in the list.
Definition: UPtrListI.H:225
iterator begin() noexcept
Return an iterator to begin of UPtrList traversal.
Definition: UPtrListI.H:620
iterator end() noexcept
Return iterator beyond end of UPtrList traversal.
Definition: UPtrListI.H:628
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition: UPtrListI.H:32
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:176
friend complex operator+(const complex &c1, const complex &c2)
Definition: complexI.H:291
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
friend bool operator!=(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:106
friend bool operator==(const refineCell &rc1, const refineCell &rc2)
Definition: refineCell.H:97
bool set() const
Are all the vector set.
Definition: triadI.H:76
bool append() const noexcept
True if output format uses an append mode.
zeroField operator-() const noexcept
Definition: zeroField.H:85
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError