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-2020 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// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
34
35template<class T>
37{
38 (this->ptrs_).free(); // free old pointers
39}
40
41
42// * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
43
44template<class T>
45inline constexpr Foam::PtrList<T>::PtrList() noexcept
46:
47 UPtrList<T>()
48{}
49
50
51template<class T>
52inline Foam::PtrList<T>::PtrList(const label len)
53:
54 UPtrList<T>(len)
55{}
56
57
58template<class T>
60:
61 UPtrList<T>(list.ptrs_.clone())
62{}
63
64
65template<class T>
67:
68 UPtrList<T>(std::move(list))
69{}
70
71
72template<class T>
74:
75 UPtrList<T>(list)
76{
77 // Took ownership of the pointers
78 list = reinterpret_cast<T*>(0);
79}
80
81
82template<class T>
83template<class CloneArg>
85(
86 const PtrList<T>& list,
87 const CloneArg& cloneArg
88)
89:
90 UPtrList<T>(list.clone(cloneArg)())
91{}
92
94// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95
96template<class T>
98{
99 (this->ptrs_).free(); // free old pointers
101}
103
104template<class T>
105template<class... Args>
107{
108 UPtrList<T>::append(new T(std::forward<Args>(args)...));
109}
110
111
112template<class T>
114{
116}
117
118
119template<class T>
121{
123}
124
125
126template<class T>
128{
129 UPtrList<T>::append(ptr.release());
130}
131
132
133template<class T>
134inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
135{
136 UPtrList<T>::append(ptr.release());
137}
138
139
140template<class T>
141inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
142{
145
146
147template<class T>
148inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
149{
151}
152
153
154template<class T>
156{
157 if (this == &other)
160 << "Attempted append to self"
162 }
163
164 const label idx = this->size();
165 const label len = other.size();
166
167 resize(idx + len);
168
169 for (label i = 0; i < len; ++i)
171 set(idx + i, other.release(i)); // Take pointer ownership
172 }
174 other.clear();
175}
177
178template<class T>
179template<class... Args>
181(
182 const label i,
183 Args&&... args
185{
186 return set(i, new T(std::forward<Args>(args)...));
188
189
190template<class T>
191inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
192{
193 // UPtrList::set returns a nullptr if trying to set with the same
194 // pointer (again). This prevents the autoPtr from managing the
195 // memory (avoids possible double deletion).
197 return autoPtr<T>(UPtrList<T>::set(i, ptr));
198}
200
201template<class T>
204 const label i,
205 autoPtr<T>& ptr
207{
208 return set(i, ptr.release());
209}
210
211
212template<class T>
214(
215 const label i,
216 autoPtr<T>&& ptr
218{
219 return set(i, ptr.release());
220}
221
222
223template<class T>
225(
226 const label i,
227 std::unique_ptr<T>&& ptr
228)
229{
230 return set(i, ptr.release());
231}
232
233
234template<class T>
236(
237 const label i,
238 const refPtr<T>& ptr
239)
240{
241 return set(i, ptr.ptr());
242}
243
244
245template<class T>
247(
248 const label i,
249 const tmp<T>& ptr
250)
251{
252 return set(i, ptr.ptr());
253}
254
255
256template<class T>
258{
259 if (i < 0 || i >= this->size())
260 {
261 return nullptr;
262 }
263
264 return autoPtr<T>(UPtrList<T>::set(i, nullptr));
265}
266
267
268template<class T>
270{
271 (this->ptrs_).free(); // free old pointers
273}
274
275
276// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
277
278template<class T>
280{
281 this->transfer(list);
282}
283
284
285// ************************************************************************* //
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:106
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:36
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
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:94
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
void clear()
Set list size to zero.
Definition: UPtrListI.H:141
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
std::istream * release() noexcept
Return managed pointer and release ownership.
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:260
A class for managing temporary objects.
Definition: tmp.H:65
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
bool set() const
Are all the vector set.
Definition: triadI.H:76
bool append() const noexcept
True if output format uses an append mode.
const volScalarField & T
patchWriters resize(patchIds.size())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
Foam::argList args(argc, argv)