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-2022 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 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
34 
35 template<class T>
36 inline void Foam::PtrList<T>::free()
37 {
38  (this->ptrs_).free(); // free old pointers
39 }
40 
41 
42 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
43 
44 template<class T>
46 :
47  UPtrList<T>()
48 {}
49 
50 
51 template<class T>
52 inline Foam::PtrList<T>::PtrList(const label len)
53 :
54  UPtrList<T>(len)
55 {}
56 
57 
58 template<class T>
60 :
61  UPtrList<T>(list.ptrs_.clone())
62 {}
63 
64 
65 template<class T>
67 :
68  UPtrList<T>(std::move(list))
69 {}
70 
71 
72 template<class T>
74 :
75  UPtrList<T>(list)
76 {
77  // Took ownership of the pointers
78  list = reinterpret_cast<T*>(0);
79 }
80 
81 
82 template<class T>
83 template<class CloneArg>
85 (
86  const PtrList<T>& list,
87  const CloneArg& cloneArg
88 )
89 :
90  UPtrList<T>(list.clone(cloneArg)())
91 {}
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
96 template<class T>
97 inline void Foam::PtrList<T>::clear()
98 {
99  (this->ptrs_).free(); // free old pointers
101 }
102 
103 
104 template<class T>
105 template<class... Args>
106 inline void Foam::PtrList<T>::emplace_back(Args&&... args)
107 {
108  UPtrList<T>::push_back(new T(std::forward<Args>(args)...));
109 }
110 
111 
112 template<class T>
114 {
116 }
117 
118 
119 template<class T>
120 inline void Foam::PtrList<T>::push_back(std::unique_ptr<T>&& ptr)
121 {
122  UPtrList<T>::push_back(ptr.release());
123 }
124 
125 
126 template<class T>
128 {
129  UPtrList<T>::push_back(ptr.release());
130 }
131 
132 
133 template<class T>
134 inline void Foam::PtrList<T>::push_back(const refPtr<T>& ptr)
135 {
136  UPtrList<T>::push_back(ptr.ptr()); // release or clone
137 }
138 
139 
140 template<class T>
141 inline void Foam::PtrList<T>::push_back(const tmp<T>& ptr)
142 {
143  UPtrList<T>::push_back(ptr.ptr()); // release or clone
144 }
145 
146 
147 template<class T>
148 inline void Foam::PtrList<T>::push_back(PtrList<T>&& other)
149 {
150  if (this == &other)
151  {
153  << "Attempted push_back to self"
154  << abort(FatalError);
155  }
156 
157  const label idx = this->size();
158  const label len = other.size();
159 
160  resize(idx + len);
161 
162  for (label i = 0; i < len; ++i)
163  {
164  set(idx + i, other.release(i)); // Take pointer ownership
165  }
166 
167  other.clear();
168 }
169 
170 
171 template<class T>
172 template<class... Args>
174 (
175  const label i,
176  Args&&... args
177 )
178 {
179  return set(i, new T(std::forward<Args>(args)...));
180 }
181 
182 
183 template<class T>
184 inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
185 {
186  // UPtrList::set returns a nullptr if trying to set with the same
187  // pointer (again). This prevents the autoPtr from managing the
188  // memory (avoids possible double deletion).
190  return autoPtr<T>(UPtrList<T>::set(i, ptr));
191 }
192 
193 
194 template<class T>
196 (
197  const label i,
198  std::unique_ptr<T>&& ptr
199 )
200 {
201  return set(i, ptr.release());
202 }
203 
204 
205 template<class T>
207 (
208  const label i,
209  autoPtr<T>&& ptr
210 )
211 {
212  return set(i, ptr.release());
213 }
214 
215 
216 template<class T>
218 (
219  const label i,
220  const refPtr<T>& ptr
221 )
222 {
223  return set(i, ptr.ptr()); // release or clone
224 }
225 
226 
227 template<class T>
229 (
230  const label i,
231  const tmp<T>& ptr
232 )
233 {
234  return set(i, ptr.ptr()); // release or clone
235 }
236 
237 
238 template<class T>
239 inline Foam::autoPtr<T> Foam::PtrList<T>::release(const label i)
240 {
241  if (i < 0 || i >= this->size())
242  {
243  return nullptr;
244  }
245 
246  return autoPtr<T>(UPtrList<T>::set(i, nullptr));
247 }
248 
249 
250 template<class T>
251 inline void Foam::PtrList<T>::transfer(PtrList<T>& list)
252 {
253  (this->ptrs_).free(); // free old pointers
255 }
256 
257 
258 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
259 
260 template<class T>
261 inline void Foam::PtrList<T>::operator=(PtrList<T>&& list)
262 {
263  this->transfer(list);
264 }
265 
266 
267 // ************************************************************************* //
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:578
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign &#39;true&#39;).
Definition: BitOps.C:30
void emplace_back(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:99
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:258
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:38
patchWriters clear()
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
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:163
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:99
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:29
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:125
const direction noexcept
Definition: Scalar.H:258
const volScalarField & T
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:232
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:106
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:90
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:244
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:231
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)