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 -------------------------------------------------------------------------------
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>
37 {
38  (this->ptrs_).free(); // free old pointers
39 }
40 
41 
42 // * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * * //
43 
44 template<class T>
45 inline constexpr Foam::PtrList<T>::PtrList() noexcept
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>
98 {
99  (this->ptrs_).free(); // free old pointers
101 }
102 
103 
104 template<class T>
105 inline void Foam::PtrList<T>::setSize(const label newLen)
106 {
107  this->resize(newLen);
108 }
109 
110 
111 template<class T>
112 template<class... Args>
114 {
115  UPtrList<T>::append(new T(std::forward<Args>(args)...));
116 }
117 
118 
119 template<class T>
120 inline void Foam::PtrList<T>::append(T* ptr)
121 {
122  UPtrList<T>::append(ptr);
123 }
124 
125 
126 template<class T>
128 {
130 }
131 
132 
133 template<class T>
135 {
136  UPtrList<T>::append(ptr.release());
137 }
138 
139 
140 template<class T>
141 inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
142 {
143  UPtrList<T>::append(ptr.release());
144 }
145 
146 
147 template<class T>
148 inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
149 {
150  UPtrList<T>::append(ptr.ptr());
151 }
152 
153 
154 template<class T>
155 inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
156 {
157  UPtrList<T>::append(ptr.ptr());
158 }
159 
160 
161 template<class T>
162 template<class... Args>
164 (
165  const label i,
166  Args&&... args
167 )
168 {
169  return set(i, new T(std::forward<Args>(args)...));
170 }
171 
172 
173 template<class T>
174 inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
175 {
176  // UPtrList::set returns a nullptr if trying to set with the same
177  // pointer (again). This prevents the autoPtr from managing the
178  // memory (avoids possible double deletion).
179 
180  return autoPtr<T>(UPtrList<T>::set(i, ptr));
181 }
182 
183 
184 template<class T>
186 (
187  const label i,
188  autoPtr<T>& ptr
189 )
190 {
191  return set(i, ptr.release());
192 }
193 
194 
195 template<class T>
197 (
198  const label i,
199  autoPtr<T>&& ptr
200 )
201 {
202  return set(i, ptr.release());
203 }
204 
205 
206 template<class T>
208 (
209  const label i,
210  std::unique_ptr<T>&& ptr
211 )
212 {
213  return set(i, ptr.release());
214 }
215 
216 
217 template<class T>
219 (
220  const label i,
221  const refPtr<T>& ptr
222 )
223 {
224  return set(i, ptr.ptr());
225 }
226 
227 
228 template<class T>
230 (
231  const label i,
232  const tmp<T>& ptr
233 )
234 {
235  return set(i, ptr.ptr());
236 }
237 
238 
239 template<class T>
241 {
242  if (i < 0 || i >= this->size())
243  {
244  return nullptr;
245  }
246 
247  return autoPtr<T>(UPtrList<T>::set(i, nullptr));
248 }
249 
250 
251 template<class T>
253 {
254  (this->ptrs_).free(); // free old pointers
255  UPtrList<T>::transfer(list);
256 }
257 
258 
259 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
260 
261 template<class T>
263 {
264  this->transfer(list);
265 }
266 
267 
268 // ************************************************************************* //
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
refPtr.H
Foam::PtrList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:113
Foam::PtrList::operator=
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
Foam::PtrList::release
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:240
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrList.H:138
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
resize
patchWriters resize(patchIds.size())
Foam::autoPtr::release
T * release() noexcept
Return pointer to the managed object and release ownership.
Definition: autoPtrI.H:100
Foam::PtrList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrListI.H:105
Foam::PtrList::PtrList
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::PtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:120
Foam::PtrList::clone
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
Foam::PtrList::clear
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
Foam::tmp::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:259
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::PtrList::free
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:36
Foam::refPtr::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: refPtrI.H:235
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
tmp.H
clear
patchWriters clear()
Foam::UList
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:103
Foam::PtrList::emplace
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
args
Foam::argList args(argc, argv)
Foam::PtrList::transfer
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:252
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
autoPtr.H