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 template<class... Args>
107 {
108  UPtrList<T>::append(new T(std::forward<Args>(args)...));
109 }
110 
111 
112 template<class T>
113 inline void Foam::PtrList<T>::append(T* ptr)
114 {
115  UPtrList<T>::append(ptr);
116 }
117 
118 
119 template<class T>
121 {
123 }
124 
125 
126 template<class T>
128 {
129  UPtrList<T>::append(ptr.release());
130 }
131 
132 
133 template<class T>
134 inline void Foam::PtrList<T>::append(std::unique_ptr<T>&& ptr)
135 {
136  UPtrList<T>::append(ptr.release());
137 }
138 
139 
140 template<class T>
141 inline void Foam::PtrList<T>::append(const refPtr<T>& ptr)
142 {
143  UPtrList<T>::append(ptr.ptr());
144 }
145 
146 
147 template<class T>
148 inline void Foam::PtrList<T>::append(const tmp<T>& ptr)
149 {
150  UPtrList<T>::append(ptr.ptr());
151 }
152 
153 
154 template<class T>
155 template<class... Args>
157 (
158  const label i,
159  Args&&... args
160 )
161 {
162  return set(i, new T(std::forward<Args>(args)...));
163 }
164 
165 
166 template<class T>
167 inline Foam::autoPtr<T> Foam::PtrList<T>::set(const label i, T* ptr)
168 {
169  // UPtrList::set returns a nullptr if trying to set with the same
170  // pointer (again). This prevents the autoPtr from managing the
171  // memory (avoids possible double deletion).
172 
173  return autoPtr<T>(UPtrList<T>::set(i, ptr));
174 }
175 
176 
177 template<class T>
179 (
180  const label i,
181  autoPtr<T>& ptr
182 )
183 {
184  return set(i, ptr.release());
185 }
186 
187 
188 template<class T>
190 (
191  const label i,
192  autoPtr<T>&& ptr
193 )
194 {
195  return set(i, ptr.release());
196 }
197 
198 
199 template<class T>
201 (
202  const label i,
203  std::unique_ptr<T>&& ptr
204 )
205 {
206  return set(i, ptr.release());
207 }
208 
209 
210 template<class T>
212 (
213  const label i,
214  const refPtr<T>& ptr
215 )
216 {
217  return set(i, ptr.ptr());
218 }
219 
220 
221 template<class T>
223 (
224  const label i,
225  const tmp<T>& ptr
226 )
227 {
228  return set(i, ptr.ptr());
229 }
230 
231 
232 template<class T>
234 {
235  if (i < 0 || i >= this->size())
236  {
237  return nullptr;
238  }
239 
240  return autoPtr<T>(UPtrList<T>::set(i, nullptr));
241 }
242 
243 
244 template<class T>
246 {
247  (this->ptrs_).free(); // free old pointers
248  UPtrList<T>::transfer(list);
249 }
250 
251 
252 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
253 
254 template<class T>
256 {
257  this->transfer(list);
258 }
259 
260 
261 // ************************************************************************* //
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:106
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:233
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))))
Foam::autoPtr::release
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:100
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:59
Foam::PtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:113
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:255
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:260
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::set
autoPtr< T > set(const label i, autoPtr< T > &ptr)
Set element to given autoPtr and return old element.
Definition: PtrListI.H:179
Foam::PtrList::transfer
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:245
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60
autoPtr.H