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-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
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 
26 Class
27  Foam::PtrDynList
28 
29 Description
30  A dynamically resizable PtrList with allocation management.
31 
32 See Also
33  Foam::UPtrList
34  Foam::PtrList
35 
36 SourceFiles
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 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward Declarations
53 template<class T, int SizeMin> class PtrDynList;
54 
55 /*---------------------------------------------------------------------------*\
56  Class PtrDynList Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 template<class T, int SizeMin=64>
60 class PtrDynList
61 :
62  public PtrList<T>
63 {
64  static_assert(SizeMin > 0, "Invalid min size parameter");
65 
66  // Private Data
67 
68  //- The capacity (allocated size) of the list.
69  label capacity_;
70 
71 
72 public:
73 
74  // Constructors
75 
76  //- Default construct
77  inline constexpr PtrDynList() noexcept;
78 
79  //- Construct with given capacity.
80  inline explicit PtrDynList(const label len);
81 
82  //- Copy construct using 'clone()' method on each element
83  inline PtrDynList(const PtrDynList<T, SizeMin>& list);
84 
85  //- Move construct
86  inline PtrDynList(PtrDynList<T, SizeMin>&& list);
87 
88  //- Move construct from PtrList
89  inline PtrDynList(PtrList<T>&& list);
90 
91  //- Take ownership of pointers in the list, set old pointers to null.
92  inline explicit PtrDynList(UList<T*>& list);
93 
94 
95  //- Destructor
96  ~PtrDynList() = default;
97 
98 
99  // Member Functions
100 
101  // Sizing
102 
103  //- Size of the underlying storage.
104  label capacity() const noexcept { return capacity_; }
105 
106  //- Reserve allocation space for at least this size.
107  inline void reserve(const label len);
108 
109  //- Alter the addressed list size.
110  inline void resize(const label newLen);
111 
112  //- Clear the addressed list, i.e. set the size to zero.
113  // Allocated size does not change
114  inline void clear();
115 
116  //- Clear the list and delete storage.
117  inline void clearStorage();
118 
119  //- Expand the addressable size to fit the allocated capacity.
120  // Returns the previous addressable size.
121  inline label expandStorage() noexcept;
122 
123  //- Shrink the allocated space to the number of elements used.
124  inline void shrink();
125 
126 
127  // Edit
128 
129  //- Squeeze out intermediate nullptr entries in the list of pointers
130  //- and adjust the addressable size accordingly.
131  // \return the number of non-null entries
132  inline label squeezeNull();
133 
134  //- Swap content, independent of sizing parameter
135  template<int AnySizeMin>
136  inline void swap(PtrDynList<T, AnySizeMin>& other);
137 
138  //- Transfer contents of the argument PtrList into this.
139  inline void transfer(PtrList<T>& list);
140 
141  //- Transfer contents of any sized PtrDynList into this.
142  template<int AnySizeMin>
143  inline void transfer(PtrDynList<T, AnySizeMin>& list);
144 
145  //- Construct an element at the end of the list,
146  //- return reference to the new list element
147  template<class... Args>
148  inline T& emplace_back(Args&&... args);
149 
150  //- Append an element to the end of the list
151  inline void push_back(T* ptr);
152 
153  //- Move append an element to the end of the list
154  inline void push_back(std::unique_ptr<T>&& ptr);
155 
156  //- Move append an element to the end of the list
157  inline void push_back(autoPtr<T>&& ptr);
158 
159  //- Move or clone append a tmp to the end of the list
160  inline void push_back(const refPtr<T>& ptr);
161 
162  //- Move or clone append a tmp to the end of the list
163  inline void push_back(const tmp<T>& ptr);
164 
165  //- Move append another list to the end of this list.
166  inline void push_back(PtrList<T>&& other);
167 
168  //- Move append another list to the end of this list.
169  template<int AnySizeMin>
170  inline void push_back(PtrDynList<T, AnySizeMin>&& other);
171 
172  //- Reduce size by 1 or more elements. Can be called on an empty list.
173  inline void pop_back(label n = 1);
174 
175  //- Construct and set a new element at given position,
176  //- (discard old element at that location).
177  //- Return reference to the new list element.
178  template<class... Args>
179  inline T& emplace(const label i, Args&&... args);
180 
181  //- Set element to given pointer and return old element (can be null).
182  //- Auto-sizes list as required.
183  inline autoPtr<T> set(const label i, T* ptr);
184 
185  //- Set element to given pointer and return old element
186  //- Auto-sizes list as required.
187  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
188 
189  //- Set element to given autoPtr and return old element
190  //- Auto-sizes list as required.
191  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
192 
193  //- Set element to given refPtr and return old element
194  //- Auto-sizes list as required.
195  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
196 
197  //- Set element to given tmp and return old element
198  //- Auto-sizes list as required.
199  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
200 
201  //- Reorder elements. Reordering must be unique (ie, shuffle).
202  inline void reorder(const labelUList& oldToNew);
203 
204 
205  // Member Operators
206 
207  //- Copy (clone) assignment
208  inline void operator=(const PtrList<T>& list);
209 
210  //- Copy (clone) assignment
211  inline void operator=(const PtrDynList<T, SizeMin>& list);
212 
213  //- Copy (clone) assignment with different sizing parameters
214  template<int AnySizeMin>
215  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
216 
217  //- Move assignment
218  inline void operator=(PtrList<T>&& list);
219 
220  //- Move assignment
221  inline void operator=(PtrDynList<T, SizeMin>&& list);
222 
223  //- Move assignment with different sizing parameters
224  template<int AnySizeMin>
225  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
226 
227 
228  // Housekeeping
229 
230  //- Disallow push_back with autoPtr without std::move
231  void push_back(autoPtr<T>& ptr) = delete;
232 
233  //- Set element to given autoPtr and return old element
234  //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
235  autoPtr<T> set(const label i, autoPtr<T>& ptr)
236  {
237  return this->set(i, ptr.release());
238  }
239 
240  //- Same as resize()
241  void setSize(const label n) { this->resize(n); }
242 
243  //- Move append an element to the end of the list
244  void append(autoPtr<T>& ptr) { this->push_back(std::move(ptr)); }
245 
246  //- Append an element to the end of the list
247  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
248  void append(T* ptr) { this->push_back(ptr); }
249 
250  //- Move append an element to the end of the list
251  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
252  void append(std::unique_ptr<T>&& ptr)
253  {
254  this->push_back(std::move(ptr));
255  }
256 
257  //- Move append an element to the end of the list
258  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
259  void append(autoPtr<T>&& ptr) { this->push_back(std::move(ptr)); }
260 
261  //- Move or clone append a tmp to the end of the list
262  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
263  void append(const refPtr<T>& ptr) { this->push_back(ptr); }
264 
265  //- Move or clone append a tmp to the end of the list
266  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
267  void append(const tmp<T>& ptr) { this->push_back(ptr); }
268 
269  //- Move append another list to the end of this list.
270  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
271  void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
272 
273  //- Move append another list to the end of this list.
274  template<int AnySizeMin>
275  void append(PtrDynList<T, AnySizeMin>&& other)
276  {
277  this->push_back(std::move(other));
278  }
279 };
280 
281 
282 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
283 
284 } // End namespace Foam
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
287 
288 #include "PtrDynListI.H"
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 
292 #endif
293 
294 // ************************************************************************* //
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:28
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
autoPtr< T > set(const label i, T *ptr)
Set element to given pointer and return old element (can be null). Auto-sizes list as required...
Definition: PtrDynListI.H:379
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:165
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers and adjust the addressable size acco...
Definition: PtrDynListI.H:180
void transfer(PtrList< T > &list)
Transfer contents of the argument PtrList into this.
Definition: PtrDynListI.H:213
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition: PtrDynListI.H:255
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynList.H:117
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:439
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
~PtrDynList()=default
Destructor.
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:153
void setSize(const label n)
Same as resize()
Definition: PtrDynList.H:328
const direction noexcept
Definition: Scalar.H:258
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:145
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void pop_back(label n=1)
Reduce size by 1 or more elements. Can be called on an empty list.
Definition: PtrDynListI.H:350
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:264
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:48
void swap(PtrDynList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: PtrDynListI.H:191
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:108
label n
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:91
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
T & emplace(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location). Return reference to the new list element.
Definition: PtrDynListI.H:366
Namespace for OpenFOAM.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:137
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition: PtrDynList.H:333