Loading...
Searching...
No Matches
PtrListI.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
27\*---------------------------------------------------------------------------*/
28
29#include "autoPtr.H"
30#include "refPtr.H"
31#include "tmp.H"
32
33// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
34
35template<class T>
37:
38 UPtrList<T>()
39{}
40
41
42template<class T>
43inline Foam::PtrList<T>::PtrList(const label len)
44:
45 UPtrList<T>(len)
46{}
47
48
49template<class T>
51:
52 UPtrList<T>(list.ptrs_.clone())
53{}
54
55
56template<class T>
58:
59 UPtrList<T>(std::move(list))
60{}
61
62
63template<class T>
64inline Foam::PtrList<T>::PtrList(PtrList<T>& list, bool reuse)
65:
66 UPtrList<T>()
67{
68 if (reuse)
69 {
70 transfer(list);
71 }
72 else
73 {
74 // No check for self-assignment
75 copyPtrList<false>(list);
76 }
77}
78
79
80template<class T>
81inline Foam::PtrList<T>::PtrList(UList<T*>& list)
82:
83 UPtrList<T>(list)
84{
85 // Took ownership of the pointers
86 list = static_cast<T*>(nullptr);
87}
88
89
90template<class T>
91template<class CloneArg>
93(
94 const PtrList<T>& list,
95 const CloneArg& cloneArg
96)
97:
98 UPtrList<T>(list.clone(cloneArg)())
99{}
100
101
102// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103
104template<class T>
105inline void Foam::PtrList<T>::clear()
107 (this->ptrs_).free(); // Free (and nullify) old pointers
109}
110
111
112template<class T>
114{
115 (this->ptrs_).free(); // Free and nullify old pointers
116}
117
118
119template<class T>
120inline void Foam::PtrList<T>::resize_null(const label newLen)
121{
122 (this->ptrs_).free(); // Free (and nullify) old pointers
124}
125
126
127template<class T>
128template<class... Args>
129inline T& Foam::PtrList<T>::emplace_back(Args&&... args)
130{
131 T* ptr = new T(std::forward<Args>(args)...);
133 return *ptr;
134}
135
136
137template<class T>
139{
141}
142
143
144template<class T>
145inline void Foam::PtrList<T>::push_back(std::unique_ptr<T>&& ptr)
146{
147 UPtrList<T>::push_back(ptr.release());
148}
149
150
151template<class T>
153{
154 UPtrList<T>::push_back(ptr.release());
155}
156
157
158template<class T>
160{
161 UPtrList<T>::push_back(ptr.ptr()); // release or clone
162}
163
164
165template<class T>
166inline void Foam::PtrList<T>::push_back(const tmp<T>& ptr)
167{
168 UPtrList<T>::push_back(ptr.ptr()); // release or clone
169}
170
171
172template<class T>
173inline void Foam::PtrList<T>::push_back(PtrList<T>&& other)
174{
175 if (this == &other)
176 {
178 << "Attempted push_back to self"
180 }
181
182 const label idx = this->size();
183 const label len = other.size();
185 resize(idx + len);
186
187 for (label i = 0; i < len; ++i)
188 {
189 set(idx + i, other.release(i)); // Take pointer ownership
190 }
192 other.clear();
193}
194
195
196template<class T>
197template<class... Args>
198inline T& Foam::PtrList<T>::emplace_set(const label i, Args&&... args)
199{
200 (void) this->release(i); // delete old entry
201 T* ptr = new T(std::forward<Args>(args)...);
202 (void) UPtrList<T>::set(i, ptr);
203 return *ptr;
204}
206
207template<class T>
208template<class... Args>
209inline T& Foam::PtrList<T>::emplace(const label i, Args&&... args)
211 return this->emplace_set(i, std::forward<Args>(args)...);
212}
213
214
215template<class T>
216template<class... Args>
217inline T& Foam::PtrList<T>::try_emplace(const label i, Args&&... args)
218{
219 T* ptr = UPtrList<T>::get(i);
220 if (ptr)
221 {
222 return *ptr;
223 }
224 return this->emplace_set(i, std::forward<Args>(args)...);
225}
226
227
228template<class T>
229inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
230{
231 // UPtrList::set returns a nullptr if trying to set with the same
232 // pointer (again). This prevents the autoPtr from managing the
233 // memory (avoids possible double deletion).
234
235 return autoPtr<T>(UPtrList<T>::set(i, ptr));
236}
237
238
239template<class T>
241(
242 const label i,
243 std::unique_ptr<T>&& ptr
245{
246 return set(i, ptr.release());
247}
248
249
250template<class T>
253 const label i,
254 autoPtr<T>&& ptr
256{
257 return set(i, ptr.release());
258}
259
260
261template<class T>
264 const label i,
265 const refPtr<T>& ptr
267{
268 return set(i, ptr.ptr()); // release or clone
269}
270
271
272template<class T>
274(
275 const label i,
276 const tmp<T>& ptr
278{
279 return set(i, ptr.ptr()); // release or clone
280}
281
282
283template<class T>
285{
286 if (i < 0 || i >= this->size())
287 {
288 return nullptr;
290
291 return autoPtr<T>(UPtrList<T>::set(i, nullptr));
292}
293
294
295template<class T>
296inline void Foam::PtrList<T>::transfer(PtrList<T>& list)
298 if (FOAM_UNLIKELY(this == &list))
299 {
300 return; // Self-assignment is a no-op
301 }
303 (this->ptrs_).free(); // Free and nullify old pointers
305}
306
307
308// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
309
310template<class T>
311inline void Foam::PtrList<T>::operator=(const UPtrList<T>& list)
313 // With check for self-assignment
314 this->copyPtrList<true>(list);
315}
316
317
318template<class T>
319inline void Foam::PtrList<T>::operator=(const PtrList<T>& list)
321 // With check for self-assignment
322 this->copyPtrList<true>(list);
323}
324
325
326template<class T>
327inline void Foam::PtrList<T>::operator=(PtrList<T>&& list)
328{
329 this->transfer(list);
330}
331
332
333// ************************************************************************* //
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
const CloudFunctionObject< CloudType > * set(const label i) const
Definition PtrList.H:171
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
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=(const UPtrList< T > &list)
Copy assignment.
Definition PtrListI.H:304
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
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 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 clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition PtrListI.H:98
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
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
void push_back(T *ptr)
Append an element to the end of the list.
Definition UPtrListI.H:265
UPtrList(Detail::PtrListDetail< T > &&ptrs) noexcept
Low-level move construct.
Definition UPtrListI.H:48
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
label size() const noexcept
The number of entries in the list.
Definition UPtrListI.H:106
void resize_null(const label newLen)
Set the list to the given size and set all entries to nullptr.
Definition UPtrListI.H:258
void transfer(UPtrList< T > &list)
Transfer contents into this list and annul the argument.
Definition UPtrListI.H:216
Detail::PtrListDetail< T > ptrs_
The list of pointers.
Definition UPtrList.H:109
void clear()
Set list size to zero.
Definition UPtrListI.H:195
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A class for managing references or pointers (no reference counting).
Definition refPtr.H:54
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition refPtrI.H:280
A class for managing temporary objects.
Definition tmp.H:75
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition tmpI.H:256
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
errorManip< error > abort(error &err)
Definition errorManip.H:139
const direction noexcept
Definition scalarImpl.H:265
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Foam::argList args(argc, argv)
#define FOAM_UNLIKELY(cond)
Definition stdFoam.H:64