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-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 #include "autoPtr.H"
30 #include "refPtr.H"
31 #include "tmp.H"
32 
33 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
34 
35 template<class T>
37 :
38  UPtrList<T>()
39 {}
40 
41 
42 template<class T>
43 inline Foam::PtrList<T>::PtrList(const label len)
44 :
45  UPtrList<T>(len)
46 {}
47 
48 
49 template<class T>
51 :
52  UPtrList<T>(list.ptrs_.clone())
53 {}
54 
55 
56 template<class T>
58 :
59  UPtrList<T>(std::move(list))
60 {}
61 
62 
63 template<class T>
65 :
66  UPtrList<T>(list)
67 {
68  // Took ownership of the pointers
69  list = static_cast<T*>(nullptr);
70 }
71 
72 
73 template<class T>
74 template<class CloneArg>
76 (
77  const PtrList<T>& list,
78  const CloneArg& cloneArg
79 )
80 :
81  UPtrList<T>(list.clone(cloneArg)())
82 {}
83 
84 
85 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
86 
87 template<class T>
88 inline void Foam::PtrList<T>::clear()
89 {
90  (this->ptrs_).free(); // Free (and nullify) old pointers
92 }
93 
94 
95 template<class T>
97 {
98  (this->ptrs_).free(); // Free and nullify old pointers
99 }
100 
101 
102 template<class T>
103 inline void Foam::PtrList<T>::resize_null(const label newLen)
104 {
105  (this->ptrs_).free(); // Free (and nullify) old pointers
106  UPtrList<T>::resize_null(newLen);
107 }
108 
109 
110 template<class T>
111 template<class... Args>
112 inline T& Foam::PtrList<T>::emplace_back(Args&&... args)
113 {
114  T* ptr = new T(std::forward<Args>(args)...);
116  return *ptr;
117 }
118 
119 
120 template<class T>
122 {
124 }
125 
126 
127 template<class T>
128 inline void Foam::PtrList<T>::push_back(std::unique_ptr<T>&& ptr)
129 {
130  UPtrList<T>::push_back(ptr.release());
131 }
132 
133 
134 template<class T>
136 {
137  UPtrList<T>::push_back(ptr.release());
138 }
139 
140 
141 template<class T>
142 inline void Foam::PtrList<T>::push_back(const refPtr<T>& ptr)
143 {
144  UPtrList<T>::push_back(ptr.ptr()); // release or clone
145 }
146 
147 
148 template<class T>
149 inline void Foam::PtrList<T>::push_back(const tmp<T>& ptr)
150 {
151  UPtrList<T>::push_back(ptr.ptr()); // release or clone
152 }
153 
154 
155 template<class T>
156 inline void Foam::PtrList<T>::push_back(PtrList<T>&& other)
157 {
158  if (this == &other)
159  {
161  << "Attempted push_back to self"
162  << abort(FatalError);
163  }
164 
165  const label idx = this->size();
166  const label len = other.size();
167 
168  resize(idx + len);
169 
170  for (label i = 0; i < len; ++i)
171  {
172  set(idx + i, other.release(i)); // Take pointer ownership
173  }
175  other.clear();
176 }
177 
178 
179 template<class T>
180 template<class... Args>
181 inline T& Foam::PtrList<T>::emplace_set(const label i, Args&&... args)
182 {
183  (void) this->release(i); // delete old entry
184  T* ptr = new T(std::forward<Args>(args)...);
185  (void) UPtrList<T>::set(i, ptr);
186  return *ptr;
187 }
188 
189 
190 template<class T>
191 template<class... Args>
192 inline T& Foam::PtrList<T>::emplace(const label i, Args&&... args)
193 {
194  return this->emplace_set(i, std::forward<Args>(args)...);
195 }
196 
197 
198 template<class T>
199 template<class... Args>
200 inline T& Foam::PtrList<T>::try_emplace(const label i, Args&&... args)
201 {
202  T* ptr = UPtrList<T>::get(i);
203  if (ptr)
204  {
205  return *ptr;
206  }
207  return this->emplace_set(i, std::forward<Args>(args)...);
208 }
209 
210 
211 template<class T>
212 inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
213 {
214  // UPtrList::set returns a nullptr if trying to set with the same
215  // pointer (again). This prevents the autoPtr from managing the
216  // memory (avoids possible double deletion).
218  return autoPtr<T>(UPtrList<T>::set(i, ptr));
219 }
220 
221 
222 template<class T>
224 (
225  const label i,
226  std::unique_ptr<T>&& ptr
227 )
228 {
229  return set(i, ptr.release());
230 }
231 
232 
233 template<class T>
235 (
236  const label i,
237  autoPtr<T>&& ptr
238 )
239 {
240  return set(i, ptr.release());
241 }
242 
243 
244 template<class T>
246 (
247  const label i,
248  const refPtr<T>& ptr
249 )
250 {
251  return set(i, ptr.ptr()); // release or clone
252 }
253 
254 
255 template<class T>
257 (
258  const label i,
259  const tmp<T>& ptr
260 )
261 {
262  return set(i, ptr.ptr()); // release or clone
263 }
264 
265 
266 template<class T>
267 inline Foam::autoPtr<T> Foam::PtrList<T>::release(const label i)
268 {
269  if (i < 0 || i >= this->size())
270  {
271  return nullptr;
272  }
273 
274  return autoPtr<T>(UPtrList<T>::set(i, nullptr));
275 }
276 
277 
278 template<class T>
279 inline void Foam::PtrList<T>::transfer(PtrList<T>& list)
280 {
281  if (this == &list)
282  {
283  return; // Self-assignment is a no-op
284  }
285 
286  (this->ptrs_).free(); // Free and nullify old pointers
288 }
289 
290 
291 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
292 
293 template<class T>
294 inline void Foam::PtrList<T>::operator=(PtrList<T>&& list)
295 {
296  this->transfer(list);
297 }
298 
299 
300 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
patchWriters resize(patchIds.size())
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
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign &#39;true&#39;).
Definition: BitOps.C:30
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:280
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
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:105
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:96
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:29
patchWriters clear()
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
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: PtrList.H:159
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
void free()
Free memory and nullify all entries. Does not change the list size.
Definition: PtrListI.H:89
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:121
const direction noexcept
Definition: Scalar.H:258
T & emplace(const label i, Args &&... args)
Same as emplace_set()
Definition: PtrListI.H:185
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
Definition: PtrListI.H:193
const volScalarField & T
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:174
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:260
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:114
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:81
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:272
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:256
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)