Loading...
Searching...
No Matches
PtrDynList.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) 2018-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::PtrDynList
28
29Description
30 A dynamically resizable PtrList with allocation management.
31
32See Also
33 Foam::UPtrList
34 Foam::PtrList
35
36SourceFiles
37 PtrDynListI.H
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_PtrDynList_H
42#define Foam_PtrDynList_H
43
44#include "PtrList.H"
45#include <type_traits>
46
47// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48
49namespace Foam
50{
51
52// Forward Declarations
53template<class T, int SizeMin> class PtrDynList;
54
55/*---------------------------------------------------------------------------*\
56 Class PtrDynList Declaration
57\*---------------------------------------------------------------------------*/
58
59template<class T, int SizeMin=64>
60class PtrDynList
61:
62 public PtrList<T>
63{
64 static_assert(SizeMin > 0, "Invalid min size parameter");
65
66 // Private Data
67
68 //- The capacity (allocated size) of the list.
69 label capacity_;
70
71
72public:
73
74 // Constructors
75
76 //- Default construct
77 inline constexpr PtrDynList() noexcept;
78
79 //- Construct with given initial capacity
80 inline explicit PtrDynList(const label len);
81
82 //- Construct with given size and capacity
83 inline explicit PtrDynList(std::pair<label,label> sizing);
84
85 //- Copy construct using 'clone()' method on each element
86 inline PtrDynList(const PtrDynList<T, SizeMin>& list);
87
88 //- Move construct
90
91 //- Move construct with different sizing parameters
92 template<int AnySizeMin>
94
95 //- Move construct from PtrList
96 inline PtrDynList(PtrList<T>&& list) noexcept;
97
98 //- Take ownership of pointers in the list, set old pointers to null.
99 inline explicit PtrDynList(UList<T*>& list);
100
101
102 //- Destructor, sync allocated size before list destruction
104
105
106 // Member Functions
107
108 // Sizing
109
110 //- Size of the underlying storage.
111 label capacity() const noexcept { return capacity_; }
112
113 //- Change the value for the list capacity directly (ADVANCED, UNSAFE)
114 //- Does not perform any memory management or resizing.
115 void setCapacity_unsafe(label len) noexcept { capacity_ = len; }
116
117 //- Reserve allocation space for at least this size.
118 // New entries are initialized to nullptr.
119 inline void reserve(const label len);
120
121 //- Reserve allocation space for at least this size.
122 //- If allocation is required, uses the specified size
123 //- without any other resizing logic.
124 // New entries are initialized to nullptr.
125 inline void reserve_exact(const label len);
126
127 //- Alter the addressed list size.
128 // New entries are initialized to nullptr.
129 inline void resize(const label len);
130
131 //- Set the addressed list to the given size,
132 //- deleting all existing entries.
133 //- Afterwards the list contains all \c nullptr entries.
134 inline void resize_null(const label len);
135
136 //- Clear the addressed list, i.e. set the size to zero.
137 // Allocated size does not change
138 inline void clear();
139
140 //- Clear the list and delete storage.
141 inline void clearStorage();
142
143 //- Shrink the allocated space to the number of elements used.
144 inline void shrink_to_fit();
145
146 //- Alias for shrink_to_fit()
147 void shrink() { this->shrink_to_fit(); }
148
149
150 // Edit
151
152 //- Squeeze out intermediate nullptr entries in the list of pointers
153 //- and adjust the addressable size accordingly.
154 // \return the number of non-null entries
155 inline label squeezeNull();
156
157 //- Swap with plain PtrList content. Implies shrink_to_fit().
158 inline void swap(PtrList<T>& list);
159
160 //- Swap content, independent of sizing parameter
161 template<int AnySizeMin>
162 inline void swap(PtrDynList<T, AnySizeMin>& other) noexcept;
163
164 //- Transfer contents of the argument PtrList into this.
165 inline void transfer(PtrList<T>& list);
166
167 //- Transfer contents of any sized PtrDynList into this.
168 template<int AnySizeMin>
169 inline void transfer(PtrDynList<T, AnySizeMin>& list);
170
171 //- Construct an element at the end of the list,
172 //- return reference to the new list element
173 template<class... Args>
174 inline T& emplace_back(Args&&... args);
175
176 //- Append an element to the end of the list
177 inline void push_back(T* ptr);
178
179 //- Move append an element to the end of the list
180 inline void push_back(std::unique_ptr<T>&& ptr);
181
182 //- Move append an element to the end of the list
183 inline void push_back(autoPtr<T>&& ptr);
184
185 //- Move or clone append a tmp to the end of the list
186 inline void push_back(const refPtr<T>& ptr);
187
188 //- Move or clone append a tmp to the end of the list
189 inline void push_back(const tmp<T>& ptr);
190
191 //- Move append another list to the end of this list.
192 inline void push_back(PtrList<T>&& other);
193
194 //- Move append another list to the end of this list.
195 template<int AnySizeMin>
196 inline void push_back(PtrDynList<T, AnySizeMin>&& other);
197
198 //- Reduce size by 1 or more elements. Can be called on an empty list.
199 inline void pop_back(label n = 1);
200
201 //- Construct and set a new element at given position,
202 //- (discard old element at that location).
203 //- Auto-sizes list as required.
204 // \param i - the location to set
205 // \param args arguments to forward to the constructor of the element
206 // \return reference to the new list element.
207 template<class... Args>
208 inline T& emplace_set(const label i, Args&&... args);
209
210 //- Same as emplace_set()
211 template<class... Args>
212 inline T& emplace(const label i, Args&&... args);
213
214 //- Like emplace_set() but will not overwrite an occupied location.
215 // \param i - the location to set (unless already defined)
216 // \param args arguments to forward to the constructor of the element
217 // \return reference to the existing or the new list element.
218 template<class... Args>
219 inline T& try_emplace(const label i, Args&&... args);
220
221 //- Set element to given pointer and return old element (can be null).
222 //- Auto-sizes list as required.
223 inline autoPtr<T> set(const label i, T* ptr);
224
225 //- Set element to given pointer and return old element
226 //- Auto-sizes list as required.
227 inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
228
229 //- Set element to given autoPtr and return old element
230 //- Auto-sizes list as required.
231 inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
232
233 //- Set element to given refPtr and return old element
234 //- Auto-sizes list as required.
235 inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
236
237 //- Set element to given tmp and return old element
238 //- Auto-sizes list as required.
239 inline autoPtr<T> set(const label i, const tmp<T>& ptr);
240
241
242 // Writing
243
244 //- Print pointer addresses to Ostream (debugging only).
245 // Optionally print addresses within the upper (capacity) region
246 Ostream& printAddresses(Ostream& os, const bool full=false) const;
247
248
249 // Member Operators
250
251 //- Copy (clone) assignment
252 inline void operator=(const UPtrList<T>& list);
253
254 //- Copy (clone) assignment
255 inline void operator=(const PtrDynList<T, SizeMin>& list);
256
257 //- Copy (clone) assignment with different sizing parameters
258 template<int AnySizeMin>
259 inline void operator=(const PtrDynList<T, AnySizeMin>& list);
260
261 //- Move assignment
262 inline void operator=(PtrList<T>&& list);
263
264 //- Move assignment
265 inline void operator=(PtrDynList<T, SizeMin>&& list);
266
267 //- Move assignment with different sizing parameters
268 template<int AnySizeMin>
269 inline void operator=(PtrDynList<T, AnySizeMin>&& list);
270
271
272 // Housekeeping
273
274 //- Disallow push_back with autoPtr without std::move
275 void push_back(autoPtr<T>& ptr) = delete;
276
277 //- Set element to given autoPtr and return old element
278 //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
279 autoPtr<T> set(const label i, autoPtr<T>& ptr)
280 {
281 return this->set(i, ptr.release());
282 }
283
284 //- Same as resize()
285 void setSize(const label n) { this->resize(n); }
286
287 //- Move append an element to the end of the list
288 void append(autoPtr<T>& ptr) { this->push_back(std::move(ptr)); }
289
290 //- Append an element to the end of the list
291 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
292 void append(T* ptr) { this->push_back(ptr); }
293
294 //- Move append an element to the end of the list
295 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
296 void append(std::unique_ptr<T>&& ptr)
297 {
298 this->push_back(std::move(ptr));
299 }
300
301 //- Move append an element to the end of the list
302 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
303 void append(autoPtr<T>&& ptr) { this->push_back(std::move(ptr)); }
304
305 //- Move or clone append a tmp to the end of the list
306 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
307 void append(const refPtr<T>& ptr) { this->push_back(ptr); }
308
309 //- Move or clone append a tmp to the end of the list
310 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
311 void append(const tmp<T>& ptr) { this->push_back(ptr); }
312
313 //- Move append another list to the end of this list.
314 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
315 void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
316
317 //- Move append another list to the end of this list.
318 template<int AnySizeMin>
320 {
321 this->push_back(std::move(other));
322 }
323};
324
325
326// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
327
328} // End namespace Foam
329
330// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331
332#include "PtrDynListI.H"
333
334// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335
336#endif
337
338// ************************************************************************* //
label n
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A dynamically resizable PtrList with allocation management.
Definition PtrDynList.H:58
T & emplace(const label i, Args &&... args)
Same as emplace_set().
void transfer(PtrList< T > &list)
Transfer contents of the argument PtrList into this.
void append(PtrList< T > &&other)
Move append another list to the end of this list.
Definition PtrDynList.H:443
void shrink()
Alias for shrink_to_fit().
Definition PtrDynList.H:186
constexpr PtrDynList() noexcept
Default construct.
Definition PtrDynListI.H:28
void append(T *ptr)
Append an element to the end of the list.
Definition PtrDynList.H:405
void setSize(const label n)
Same as resize().
Definition PtrDynList.H:393
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
void append(const refPtr< T > &ptr)
Move or clone append a tmp to the end of the list.
Definition PtrDynList.H:429
Ostream & printAddresses(Ostream &os, const bool full=false) const
Print pointer addresses to Ostream (debugging only).
void push_back(T *ptr)
Append an element to the end of the list.
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
void setCapacity_unsafe(label len) noexcept
Change the value for the list capacity directly (ADVANCED, UNSAFE) Does not perform any memory manage...
Definition PtrDynList.H:134
void operator=(const UPtrList< T > &list)
Copy (clone) assignment.
void swap(PtrList< T > &list)
Swap with plain PtrList content. Implies shrink_to_fit().
void reserve_exact(const label len)
Reserve allocation space for at least this size. If allocation is required, uses the specified size w...
void shrink_to_fit()
Shrink the allocated space to the number of elements used.
void append(PtrDynList< T, AnySizeMin > &&other)
Move append another list to the end of this list.
Definition PtrDynList.H:449
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location)....
void clearStorage()
Clear the list and delete storage.
void resize_null(const label len)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
autoPtr< T > set(const label i, autoPtr< T > &ptr)
Set element to given autoPtr and return old element.
Definition PtrDynList.H:385
void append(const tmp< T > &ptr)
Move or clone append a tmp to the end of the list.
Definition PtrDynList.H:436
autoPtr< T > set(const label i, T *ptr)
Set element to given pointer and return old element (can be null). Auto-sizes list as required.
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition PtrDynList.H:398
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied location.
void append(autoPtr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrDynList.H:422
label capacity() const noexcept
Size of the underlying storage.
Definition PtrDynList.H:128
void append(std::unique_ptr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrDynList.H:412
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers and adjust the addressable size acco...
void resize(const label len)
Alter the addressed list size.
void reserve(const label len)
Reserve allocation space for at least this size.
void clear()
Clear the addressed list, i.e. set the size to zero.
~PtrDynList()
Destructor, sync allocated size before list destruction.
Definition PtrDynList.H:118
void push_back(autoPtr< T > &ptr)=delete
Disallow push_back with autoPtr without std::move.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
constexpr PtrList() noexcept
Default construct.
Definition PtrListI.H:29
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:89
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
void setAddressableSize(const label n) noexcept
Adjust addressable size.
Definition UPtrListI.H:25
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
T * release() noexcept
Release ownership and return the pointer.
Definition autoPtrI.H:28
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
A class for managing temporary objects.
Definition tmp.H:75
const volScalarField & T
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
const direction noexcept
Definition scalarImpl.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)