PtrDynList.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) 2018-2022 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::PtrDynList
28
29Description
30 A dynamically resizable PtrList with allocation management.
31
32See Also
33 Foam::UPtrList
34 Foam::PtrList
35
36SourceFiles
37 PtrDynListI.H
38
39\*---------------------------------------------------------------------------*/
40
41#ifndef Foam_PtrDynList_H
42#define Foam_PtrDynList_H
43
44#include "PtrList.H"
45#include <type_traits>
46#include <memory>
47#include <utility>
48
49// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50
51namespace Foam
52{
53
54// Forward Declarations
55template<class T, int SizeMin> class PtrDynList;
56
57/*---------------------------------------------------------------------------*\
58 Class PtrDynList Declaration
59\*---------------------------------------------------------------------------*/
60
61template<class T, int SizeMin=64>
62class PtrDynList
63:
64 public PtrList<T>
65{
66 static_assert(SizeMin > 0, "Invalid min size parameter");
67
68 // Private Data
69
70 //- The capacity (allocated size) of the list.
71 label capacity_;
72
73
74public:
75
76 // Constructors
77
78 //- Default construct
79 inline constexpr PtrDynList() noexcept;
80
81 //- Construct with given capacity.
82 inline explicit PtrDynList(const label len);
83
84 //- Copy construct using 'clone()' method on each element
85 inline PtrDynList(const PtrDynList<T, SizeMin>& list);
86
87 //- Move construct
89
90 //- Move construct from PtrList
91 inline PtrDynList(PtrList<T>&& list);
92
93 //- Take ownership of pointers in the list, set old pointers to null.
94 inline explicit PtrDynList(UList<T*>& list);
95
96
97 //- Destructor
98 ~PtrDynList() = default;
99
100
101 // Member Functions
102
103 // Access
104
105 //- Size of the underlying storage.
106 inline label capacity() const noexcept;
107
108 //- Return const pointer to element (can be nullptr),
109 //- with bounds checking.
110 // The return value can be tested as a bool.
111 const T* get(const label i) const { return this->test(i); }
112
113 //- Return const pointer to element (if set) or nullptr,
114 //- with bounds checking.
115 // The return value can be tested as a bool.
116 const T* set(const label i) const { return this->test(i); }
117
118
119 // Sizing
120
121 //- Reserve allocation space for at least this size.
122 inline void reserve(const label len);
123
124 //- Alter the addressed list size.
125 inline void resize(const label newLen);
126
127 //- Same as resize()
128 void setSize(const label n) { this->resize(n); }
129
130 //- Clear the addressed list, i.e. set the size to zero.
131 // Allocated size does not change
132 inline void clear();
133
134 //- Clear the list and delete storage.
135 inline void clearStorage();
136
137 //- Expand the addressable size to fit the allocated capacity.
138 // Returns the previous addressable size.
139 inline label expandStorage() noexcept;
140
141 //- Shrink the allocated space to the number of elements used.
142 inline void shrink();
143
144
145 // Edit
146
147 //- Delete the allocated entries, but retain the list size.
148 using PtrList<T>::free;
149
150 //- Squeeze out intermediate nullptr entries in the list of pointers
151 //- and adjust the addressable size accordingly.
152 // \return the number of non-null entries
153 inline label squeezeNull();
154
155 //- Swap content, independent of sizing parameter
156 template<int AnySizeMin>
157 inline void swap(PtrDynList<T, AnySizeMin>& other);
158
159 //- Construct and append an element to the end of the list
160 template<class... Args>
161 inline void emplace_append(Args&&... args);
162
163 //- Append an element to the end of the list
164 inline void append(T* ptr);
165
166 //- Move append an element to the end of the list
167 inline void append(autoPtr<T>& ptr);
168
169 //- Move append an element to the end of the list
170 inline void append(autoPtr<T>&& ptr);
171
172 //- Move append an element to the end of the list
173 inline void append(std::unique_ptr<T>&& ptr);
174
175 //- Move or clone append a tmp to the end of the list
176 inline void append(const refPtr<T>& ptr);
177
178 //- Move or clone append a tmp to the end of the list
179 inline void append(const tmp<T>& ptr);
180
181 //- Move append another list to the end of this list.
182 inline void append(PtrList<T>&& other);
183
184 //- Move append another list to the end of this list.
185 template<int AnySizeMin>
186 inline void append(PtrDynList<T, AnySizeMin>&& other);
187
188 //- Remove and return the top element
189 inline autoPtr<T> remove();
190
191 //- Construct and set an element
192 template<class... Args>
193 inline autoPtr<T> emplace(const label i, Args&&... args);
194
195 //- Set element to given pointer and return old element (can be null)
196 inline autoPtr<T> set(const label i, T* ptr);
197
198 //- Set element to given autoPtr and return old element
199 inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
200
201 //- Set element to given autoPtr and return old element
202 inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
203
204 //- Set element to given pointer and return old element
205 inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
206
207 //- Set element to given refPtr and return old element
208 inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
209
210 //- Set element to given tmp and return old element
211 inline autoPtr<T> set(const label i, const tmp<T>& ptr);
212
213 //- Reorder elements. Reordering must be unique (ie, shuffle).
214 inline void reorder(const labelUList& oldToNew);
215
216
217 // Member Operators
218
219 //- Copy (clone) assignment
220 inline void operator=(const PtrList<T>& list);
221
222 //- Copy (clone) assignment
223 inline void operator=(const PtrDynList<T, SizeMin>& list);
224
225 //- Copy (clone) assignment with different sizing parameters
226 template<int AnySizeMin>
227 inline void operator=(const PtrDynList<T, AnySizeMin>& list);
228
229 //- Move assignment
230 inline void operator=(PtrList<T>&& list);
231
232 //- Move assignment
233 inline void operator=(PtrDynList<T, SizeMin>&& list);
234
235 //- Move assignment with different sizing parameters
236 template<int AnySizeMin>
237 inline void operator=(PtrDynList<T, AnySizeMin>&& list);
238};
239
240
241// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242
243} // End namespace Foam
244
245// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246
247#include "PtrDynListI.H"
248
249// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250
251#endif
252
253// ************************************************************************* //
label n
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:64
const T * set(const label i) const
Definition: PtrDynList.H:115
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:179
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:35
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:235
void setSize(const label n)
Same as resize()
Definition: PtrDynList.H:127
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:228
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:328
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:159
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:98
const T * get(const label i) const
Definition: PtrDynList.H:110
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:167
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:430
label squeezeNull()
Definition: PtrDynListI.H:194
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:105
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:151
void swap(PtrDynList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: PtrDynListI.H:205
~PtrDynList()=default
Destructor.
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:122
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:36
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
const T * test(const label i) const
Definition: UPtrListI.H:120
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
A class for managing temporary objects.
Definition: tmp.H:65
const volScalarField & T
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223
Foam::argList args(argc, argv)