Loading...
Searching...
No Matches
PtrList.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-2025 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
27Class
28 Foam::PtrList
29
30Description
31 A list of pointers to objects of type <T>, with allocation/deallocation
32 management of the pointers.
33 The operator[] returns a reference to the object, not the pointer.
34
35See Also
36 Foam::UPtrList
37 Foam::PtrDynList
38
39SourceFiles
40 PtrListI.H
41 PtrList.C
42 PtrListIO.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef Foam_PtrList_H
47#define Foam_PtrList_H
48
49#include "UPtrList.H"
50#include "SLPtrListFwd.H"
51
52// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56
57// Forward Declarations
58template<class T> class autoPtr;
59template<class T> class refPtr;
60template<class T> class tmp;
61template<class T> class PtrList;
62template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
63
64/*---------------------------------------------------------------------------*\
65 Class PtrList Declaration
66\*---------------------------------------------------------------------------*/
67
68template<class T>
69class PtrList
70:
71 public UPtrList<T>
72{
73 // Private Member Functions
74
75 //- Copy assignment.
76 // For existing list entries, values are copied from the list.
77 // For new list entries, pointers are cloned from the list.
78 template<bool CheckSelf>
79 void copyPtrList(const UPtrList<T>& list);
80
81
82protected:
83
84 // Protected Member Functions
85
86 //- Read from Istream using Istream constructor class
87 template<class INew>
88 void readIstream(Istream& is, const INew& inew);
89
90
91public:
92
93 // Constructors
94
95 //- Default construct
96 inline constexpr PtrList() noexcept;
97
98 //- Construct with specified size, each element initialized to nullptr
99 inline explicit PtrList(const label len);
100
101 //- Copy construct using 'clone()' method on each element
102 inline PtrList(const PtrList<T>& list);
104 //- Move construct
105 inline PtrList(PtrList<T>&& list) noexcept;
106
107 //- Take ownership of pointers in the list, set old pointers to null.
108 inline explicit PtrList(UList<T*>& list);
109
110 //- Copy construct using 'clone()' method on each element
111 template<class CloneArg>
112 inline PtrList(const PtrList<T>& list, const CloneArg& cloneArgs);
114 //- Construct as copy or re-use as specified
115 inline PtrList(PtrList<T>& list, bool reuse);
116
117 //- Copy construct using 'clone()' on each element of SLPtrList<T>
118 explicit PtrList(const SLPtrList<T>& list);
119
120 //- Construct from Istream using given Istream constructor class
121 template<class INew>
122 PtrList(Istream& is, const INew& inew);
123
124 //- Construct from Istream using default Istream constructor class
125 PtrList(Istream& is);
126
127
128 //- Destructor. Frees all pointers
130
131
132 // Member Functions
133
134 //- Make a copy by cloning each of the list elements.
135 template<class... Args>
136 PtrList<T> clone(Args&&... args) const;
137
138
139 // Access
141 //- Return const pointer to element (can be nullptr),
142 //- or nullptr for out-of-range access (ie, \em with bounds checking).
143 // The return value can be tested as a bool.
144 const T* set(const label i) const { return UPtrList<T>::set(i); }
146
147 // Edit
148
149 //- Clear the PtrList. Delete allocated entries and set size to zero.
150 inline void clear();
152 //- Free memory and nullify all entries. Does not change the list size.
153 inline void free();
154
155 //- Adjust size of PtrList.
156 // New entries are initialized to nullptr, removed entries are deleted
157 void resize(const label newLen);
158
159 //- Set the addressed list to the given size,
160 //- deleting all existing entries.
161 //- Afterwards the list contains all \c nullptr entries.
162 inline void resize_null(const label newLen);
163
164 //- Construct and append an element to the end of the list,
165 //- return reference to the new list element
166 template<class... Args>
167 inline T& emplace_back(Args&&... args);
168
169 //- Append an element to the end of the list
170 inline void push_back(T* ptr);
172 //- Move append an element to the end of the list
173 inline void push_back(std::unique_ptr<T>&& ptr);
174
175 //- Move append an element to the end of the list
176 inline void push_back(autoPtr<T>&& ptr);
177
178 //- Move or clone append a refPtr to the end of the list
179 inline void push_back(const refPtr<T>& ptr);
180
181 //- Move or clone append a tmp to the end of the list
182 inline void push_back(const tmp<T>& ptr);
183
184 //- Move append another list to the end of this list.
185 inline void push_back(PtrList<T>&& other);
186
187 //- Construct and set a new element at given position,
188 //- (discard old element at that location).
189 // \param i - the location to set
190 // \param args arguments to forward to the constructor of the element
191 // \return reference to the new list element.
192 template<class... Args>
193 inline T& emplace_set(const label i, Args&&... args);
194
195 //- Same as emplace_set()
196 template<class... Args>
197 inline T& emplace(const label i, Args&&... args);
199 //- Like emplace_set() but will not overwrite an occupied (non-null)
200 //- location.
201 // \param i - the location to set (unless already defined)
202 // \param args arguments to forward to the constructor of the element
203 // \return reference to the existing or the new list element.
204 template<class... Args>
205 inline T& try_emplace(const label i, Args&&... args);
206
207 //- Set element to given pointer and return old element (can be null)
208 // No-op if the new pointer value is identical to the current content.
209 inline autoPtr<T> set(const label i, T* ptr);
211 //- Set element to given unique_ptr and return old element
212 inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
213
214 //- Set element to given autoPtr and return old element
215 inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
216
217 //- Set element to given refPtr and return old element
218 inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
219
220 //- Set element to given tmp and return old element
221 inline autoPtr<T> set(const label i, const tmp<T>& ptr);
222
223 //- Release ownership of the pointer at the given position.
224 // Out of bounds addressing is a no-op and returns nullptr.
225 inline autoPtr<T> release(const label i);
226
227 //- Transfer into this list and annul the argument list
228 inline void transfer(PtrList<T>& list);
229
231 // Member Operators
232
233 //- Copy assignment.
234 // For existing list entries, values are copied from the list.
235 // For new list entries, pointers are cloned from the list.
236 void operator=(const UPtrList<T>& list);
237
238 //- Copy assignment.
239 // For existing list entries, values are copied from the list.
240 // For new list entries, pointers are cloned from the list.
241 void operator=(const PtrList<T>& list);
242
243 //- Move assignment
244 inline void operator=(PtrList<T>&& list);
245
247 // IOstream Operators
248
249 //- Read from Istream, discarding contents of existing list
250 friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
251
253 // Housekeeping
254
255 //- Disallow push_back with autoPtr without std::move
256 void push_back(autoPtr<T>& ptr) = delete;
257
258 //- Set element to given autoPtr and return old element
259 //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
260 autoPtr<T> set(const label i, autoPtr<T>& ptr)
261 {
262 return this->set(i, ptr.release());
264
265 //- Same as resize()
266 void setSize(const label n) { this->resize(n); }
267
268 //- Move append an element to the end of the list
269 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
270 void append(autoPtr<T>& ptr) { this->push_back(ptr.release()); }
271
272 //- Append an element to the end of the list
273 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
274 void append(T* ptr) { this->push_back(ptr); }
276 //- Move append an element to the end of the list
277 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
278 void append(std::unique_ptr<T>&& ptr)
279 {
280 this->push_back(ptr.release());
281 }
282
283 //- Move append an element to the end of the list
284 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
285 void append(autoPtr<T>&& ptr) { this->push_back(ptr.release()); }
286
287 //- Move or clone append a tmp to the end of the list
288 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
289 void append(const refPtr<T>& ptr) { this->push_back(ptr); }
291 //- Move or clone append a tmp to the end of the list
292 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
293 void append(const tmp<T>& ptr) { this->push_back(ptr); }
294
295 //- Move append another list to the end of this list.
296 //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
297 void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
298};
299
300
301// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303} // End namespace Foam
304
305// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306
307#include "PtrListI.H"
308
309// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310
311#ifdef NoRepository
312 #include "PtrList.C"
313 #include "PtrListIO.C"
314#endif
315
316// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317
318#endif
319
320// ************************************************************************* //
Forward declarations for SLPtrList.
label n
A helper class when constructing from an Istream or dictionary.
Definition INew.H:47
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition PtrList.H:67
T & emplace(const label i, Args &&... args)
Same as emplace_set().
Definition PtrListI.H:202
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition PtrListI.H:289
void append(PtrList< T > &&other)
Move append another list to the end of this list.
Definition PtrList.H:409
const faPatch * set(const label i) const
Definition PtrList.H:171
void push_back(const tmp< T > &ptr)
Move or clone append a tmp to the end of the list.
Definition PtrListI.H:159
void append(T *ptr)
Append an element to the end of the list.
Definition PtrList.H:371
void setSize(const label n)
Same as resize().
Definition PtrList.H:357
void append(const refPtr< T > &ptr)
Move or clone append a tmp to the end of the list.
Definition PtrList.H:395
PtrList< faPatch > clone(Args &&... args) const
autoPtr< T > set(const label i, std::unique_ptr< T > &&ptr)
Set element to given unique_ptr and return old element.
Definition PtrListI.H:234
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition PtrListI.H:277
void push_back(T *ptr)
Append an element to the end of the list.
Definition PtrListI.H:131
T & emplace_back(Args &&... args)
Construct and append an element to the end of the list, return reference to the new list element.
Definition PtrListI.H:122
void operator=(PtrList< T > &&list)
Move assignment.
Definition PtrListI.H:320
void push_back(std::unique_ptr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrListI.H:138
void operator=(const UPtrList< T > &list)
Copy assignment.
Definition PtrListI.H:304
void push_back(const refPtr< T > &ptr)
Move or clone append a refPtr to the end of the list.
Definition PtrListI.H:152
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location).
Definition PtrListI.H:191
autoPtr< T > set(const label i, autoPtr< T > &ptr)
Set element to given autoPtr and return old element.
Definition PtrList.H:349
void append(const tmp< T > &ptr)
Move or clone append a tmp to the end of the list.
Definition PtrList.H:402
autoPtr< T > set(const label i, T *ptr)
Set element to given pointer and return old element (can be null).
Definition PtrListI.H:222
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition PtrList.H:364
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
Definition PtrListI.H:210
void push_back(PtrList< T > &&other)
Move append another list to the end of this list.
Definition PtrListI.H:166
void append(autoPtr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrList.H:388
void append(std::unique_ptr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrList.H:378
void free()
Free memory and nullify all entries. Does not change the list size.
Definition PtrListI.H:106
void resize_null(const label newLen)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
Definition PtrListI.H:113
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition PtrListIO.C:30
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition PtrListI.H:98
autoPtr< T > set(const label i, const tmp< T > &ptr)
Set element to given tmp and return old element.
Definition PtrListI.H:267
void push_back(autoPtr< T > &ptr)=delete
Disallow push_back with autoPtr without std::move.
void push_back(autoPtr< T > &&ptr)
Move append an element to the end of the list.
Definition PtrListI.H:145
autoPtr< T > set(const label i, const refPtr< T > &ptr)
Set element to given refPtr and return old element.
Definition PtrListI.H:256
constexpr PtrList() noexcept
Default construct.
Definition PtrListI.H:29
autoPtr< T > set(const label i, autoPtr< T > &&ptr)
Set element to given autoPtr and return old element.
Definition PtrListI.H:245
void operator=(const PtrList< T > &list)
Copy assignment.
Definition PtrListI.H:312
void resize(const label newLen)
Adjust size of PtrList.
Definition PtrList.C:124
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
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
UPtrList(Detail::PtrListDetail< T > &&ptrs) noexcept
Low-level move construct.
Definition UPtrListI.H:48
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
Namespace for OpenFOAM.
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition scalarImpl.H:265
LPtrList< SLListBase, T > SLPtrList
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)