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 with different sizing parameters
89  template<int AnySizeMin>
91 
92  //- Move construct from PtrList
93  inline PtrDynList(PtrList<T>&& list) noexcept;
94 
95  //- Take ownership of pointers in the list, set old pointers to null.
96  inline explicit PtrDynList(UList<T*>& list);
97 
98 
99  //- Destructor
100  ~PtrDynList() = default;
101 
102 
103  // Member Functions
104 
105  // Sizing
106 
107  //- Size of the underlying storage.
108  label capacity() const noexcept { return capacity_; }
109 
110  //- Reserve allocation space for at least this size.
111  inline void reserve(const label len);
112 
113  //- Alter the addressed list size.
114  inline void resize(const label newLen);
115 
116  //- Set the addressed list to the given size,
117  //- deleting all existing entries.
118  //- Afterwards the list contains all \c nullptr entries.
119  inline void resize_null(const label newLen);
120 
121  //- Clear the addressed list, i.e. set the size to zero.
122  // Allocated size does not change
123  inline void clear();
124 
125  //- Clear the list and delete storage.
126  inline void clearStorage();
127 
128  //- Shrink the allocated space to the number of elements used.
129  inline void shrink_to_fit();
130 
131  //- Shrink the internal bookkeeping of the allocated space to the
132  //- number of addressed elements without affecting allocation.
133  // \note when empty() it will delete any allocated memory.
134  inline void shrink_unsafe();
135 
136  //- Calls shrink_to_fit()
137  void shrink() { shrink_to_fit(); }
138 
139 
140  // Edit
141 
142  //- Squeeze out intermediate nullptr entries in the list of pointers
143  //- and adjust the addressable size accordingly.
144  // \return the number of non-null entries
145  inline label squeezeNull();
146 
147  //- Swap with plain PtrList content. Implies shrink_to_fit().
148  inline void swap(PtrList<T>& list);
149 
150  //- Swap content, independent of sizing parameter
151  template<int AnySizeMin>
152  inline void swap(PtrDynList<T, AnySizeMin>& other) noexcept;
153 
154  //- Transfer contents of the argument PtrList into this.
155  inline void transfer(PtrList<T>& list);
156 
157  //- Transfer contents of any sized PtrDynList into this.
158  template<int AnySizeMin>
159  inline void transfer(PtrDynList<T, AnySizeMin>& list);
160 
161  //- Construct an element at the end of the list,
162  //- return reference to the new list element
163  template<class... Args>
164  inline T& emplace_back(Args&&... args);
165 
166  //- Append an element to the end of the list
167  inline void push_back(T* ptr);
168 
169  //- Move append an element to the end of the list
170  inline void push_back(std::unique_ptr<T>&& ptr);
171 
172  //- Move append an element to the end of the list
173  inline void push_back(autoPtr<T>&& ptr);
174 
175  //- Move or clone append a tmp to the end of the list
176  inline void push_back(const refPtr<T>& ptr);
177 
178  //- Move or clone append a tmp to the end of the list
179  inline void push_back(const tmp<T>& ptr);
180 
181  //- Move append another list to the end of this list.
182  inline void push_back(PtrList<T>&& other);
183 
184  //- Move append another list to the end of this list.
185  template<int AnySizeMin>
186  inline void push_back(PtrDynList<T, AnySizeMin>&& other);
187 
188  //- Reduce size by 1 or more elements. Can be called on an empty list.
189  inline void pop_back(label n = 1);
190 
191  //- Construct and set a new element at given position,
192  //- (discard old element at that location).
193  //- Auto-sizes list as required.
194  // \param i - the location to set
195  // \param args arguments to forward to the constructor of the element
196  // \return reference to the new list element.
197  template<class... Args>
198  inline T& emplace_set(const label i, Args&&... args);
199 
200  //- Same as emplace_set()
201  template<class... Args>
202  inline T& emplace(const label i, Args&&... args);
203 
204  //- Like emplace_set() but will not overwrite an occupied location.
205  // \param i - the location to set (unless already defined)
206  // \param args arguments to forward to the constructor of the element
207  // \return reference to the existing or the new list element.
208  template<class... Args>
209  inline T& try_emplace(const label i, Args&&... args);
210 
211  //- Set element to given pointer and return old element (can be null).
212  //- Auto-sizes list as required.
213  inline autoPtr<T> set(const label i, T* ptr);
214 
215  //- Set element to given pointer and return old element
216  //- Auto-sizes list as required.
217  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
218 
219  //- Set element to given autoPtr and return old element
220  //- Auto-sizes list as required.
221  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
222 
223  //- Set element to given refPtr and return old element
224  //- Auto-sizes list as required.
225  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
226 
227  //- Set element to given tmp and return old element
228  //- Auto-sizes list as required.
229  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
230 
231  //- Reorder elements. Reordering must be unique (ie, shuffle).
232  inline void reorder(const labelUList& oldToNew);
233 
234 
235  // Member Operators
236 
237  //- Copy (clone) assignment
238  inline void operator=(const PtrList<T>& list);
239 
240  //- Copy (clone) assignment
241  inline void operator=(const PtrDynList<T, SizeMin>& list);
242 
243  //- Copy (clone) assignment with different sizing parameters
244  template<int AnySizeMin>
245  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
246 
247  //- Move assignment
248  inline void operator=(PtrList<T>&& list);
249 
250  //- Move assignment
251  inline void operator=(PtrDynList<T, SizeMin>&& list);
252 
253  //- Move assignment with different sizing parameters
254  template<int AnySizeMin>
255  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
256 
257 
258  // Housekeeping
259 
260  //- Disallow push_back with autoPtr without std::move
261  void push_back(autoPtr<T>& ptr) = delete;
262 
263  //- Set element to given autoPtr and return old element
264  //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
265  autoPtr<T> set(const label i, autoPtr<T>& ptr)
266  {
267  return this->set(i, ptr.release());
268  }
269 
270  //- Same as resize()
271  void setSize(const label n) { this->resize(n); }
272 
273  //- Move append an element to the end of the list
274  void append(autoPtr<T>& ptr) { this->push_back(std::move(ptr)); }
275 
276  //- Append an element to the end of the list
277  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
278  void append(T* ptr) { this->push_back(ptr); }
279 
280  //- Move append an element to the end of the list
281  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
282  void append(std::unique_ptr<T>&& ptr)
283  {
284  this->push_back(std::move(ptr));
285  }
286 
287  //- Move append an element to the end of the list
288  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
289  void append(autoPtr<T>&& ptr) { this->push_back(std::move(ptr)); }
290 
291  //- Move or clone append a tmp to the end of the list
292  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
293  void append(const refPtr<T>& ptr) { this->push_back(ptr); }
294 
295  //- Move or clone append a tmp to the end of the list
296  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
297  void append(const tmp<T>& ptr) { this->push_back(ptr); }
298 
299  //- Move append another list to the end of this list.
300  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
301  void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
302 
303  //- Move append another list to the end of this list.
304  template<int AnySizeMin>
305  void append(PtrDynList<T, AnySizeMin>&& other)
306  {
307  this->push_back(std::move(other));
308  }
309 };
310 
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 } // End namespace Foam
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 
318 #include "PtrDynListI.H"
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 #endif
323 
324 // ************************************************************************* //
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:28
void shrink_to_fit()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:193
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
void shrink()
Calls shrink_to_fit()
Definition: PtrDynList.H:170
void operator=(const PtrList< T > &list)
Copy (clone) assignment.
Definition: PtrDynListI.H:543
label squeezeNull()
Squeeze out intermediate nullptr entries in the list of pointers and adjust the addressable size acco...
Definition: PtrDynListI.H:219
void transfer(PtrList< T > &list)
Transfer contents of the argument PtrList into this.
Definition: PtrDynListI.H:275
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location). Auto-sizes list as required.
Definition: PtrDynListI.H:428
T & emplace_back(Args &&... args)
Construct an element at the end of the list, return reference to the new list element.
Definition: PtrDynListI.H:317
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynList.H:123
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:531
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:105
void resize_null(const label newLen)
Set the addressed list to the given size, deleting all existing entries. Afterwards the list contains...
Definition: PtrDynListI.H:157
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:28
~PtrDynList()=default
Destructor.
void setSize(const label n)
Same as resize()
Definition: PtrDynList.H:372
const direction noexcept
Definition: Scalar.H:258
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:185
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:412
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied location.
Definition: PtrDynListI.H:456
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:326
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:48
void swap(PtrList< T > &list)
Swap with plain PtrList content. Implies shrink_to_fit().
Definition: PtrDynListI.H:228
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
void shrink_unsafe()
Shrink the internal bookkeeping of the allocated space to the number of addressed elements without af...
Definition: PtrDynListI.H:207
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:128
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:111
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
T & emplace(const label i, Args &&... args)
Same as emplace_set()
Definition: PtrDynListI.H:444
Namespace for OpenFOAM.
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:177
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition: PtrDynList.H:377