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-2021 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 PtrDynList_H
42 #define PtrDynList_H
43 
44 #include "PtrList.H"
45 #include <type_traits>
46 #include <memory>
47 #include <utility>
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Forward Declarations
55 template<class T, int SizeMin> class PtrDynList;
56 
57 /*---------------------------------------------------------------------------*\
58  Class PtrDynList Declaration
59 \*---------------------------------------------------------------------------*/
60 
61 template<class T, int SizeMin=64>
62 class 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 
74 public:
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
88  inline PtrDynList(PtrDynList<T, SizeMin>&& list);
89 
90  //- Take ownership of pointers in the list, set old pointers to null.
91  inline explicit PtrDynList(UList<T*>& list);
92 
93 
94  //- Destructor
95  ~PtrDynList() = default;
96 
97 
98  // Member Functions
99 
100  // Access
101 
102  //- Size of the underlying storage.
103  inline label capacity() const noexcept;
104 
105  //- Return const pointer to element (can be nullptr),
106  // with bounds checking.
107  inline const T* get(const label i) const;
108 
109  //- Return const pointer to element (if set) or nullptr,
110  // with bounds checking.
111  // The return value can be tested as a bool.
112  const T* set(const label i) const { return this->get(i); }
113 
114 
115  // Sizing
116 
117  //- Reserve allocation space for at least this size.
118  inline void reserve(const label len);
119 
120  //- Alter the addressed list size.
121  inline void resize(const label newLen);
122 
123  //- Same as resize()
124  void setSize(const label n) { this->resize(n); }
125 
126  //- Clear the addressed list, i.e. set the size to zero.
127  // Allocated size does not change
128  inline void clear();
129 
130  //- Clear the list and delete storage.
131  inline void clearStorage();
132 
133  //- Expand the addressable size to fit the allocated capacity.
134  // Returns the previous addressable size.
135  inline label expandStorage() noexcept;
136 
137  //- Shrink the allocated space to the number of elements used.
138  inline void shrink();
139 
140 
141  // Edit
142 
143  //- Delete the allocated entries, but retain the list size.
144  using PtrList<T>::free;
145 
146  //- Squeeze out intermediate nullptr entries in the list of pointers
147  //- and adjust the addressable size accordingly.
148  // \return the number of non-null entries
149  inline label squeezeNull();
150 
151  //- Swap content, independent of sizing parameter
152  template<int AnySizeMin>
153  inline void swap(PtrDynList<T, AnySizeMin>& other);
154 
155  //- Construct and append an element to the end of the list
156  template<class... Args>
157  inline void emplace_append(Args&&... args);
158 
159  //- Append an element to the end of the list
160  inline void append(T* ptr);
161 
162  //- Move append an element to the end of the list
163  inline void append(autoPtr<T>& ptr);
164 
165  //- Move append an element to the end of the list
166  inline void append(autoPtr<T>&& ptr);
167 
168  //- Move append an element to the end of the list
169  inline void append(std::unique_ptr<T>&& ptr);
170 
171  //- Move or clone append a tmp to the end of the list
172  inline void append(const refPtr<T>& ptr);
173 
174  //- Move or clone append a tmp to the end of the list
175  inline void append(const tmp<T>& ptr);
176 
177  //- Move append another list to the end of this list.
178  inline void append(PtrList<T>&& other);
179 
180  //- Move append another list to the end of this list.
181  template<int AnySizeMin>
182  inline void append(PtrDynList<T, AnySizeMin>&& other);
183 
184  //- Remove and return the top element
185  inline autoPtr<T> remove();
186 
187  //- Construct and set an element
188  template<class... Args>
189  inline autoPtr<T> emplace(const label i, Args&&... args);
190 
191  //- Set element to given pointer and return old element (can be null)
192  inline autoPtr<T> set(const label i, T* ptr);
193 
194  //- Set element to given autoPtr and return old element
195  inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
196 
197  //- Set element to given autoPtr and return old element
198  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
199 
200  //- Set element to given pointer and return old element
201  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
202 
203  //- Set element to given refPtr and return old element
204  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
205 
206  //- Set element to given tmp and return old element
207  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
208 
209  //- Reorder elements. Reordering must be unique (ie, shuffle).
210  inline void reorder(const labelUList& oldToNew);
211 
212 
213  // Member Operators
214 
215  //- Copy (clone) assignment
216  inline void operator=(const PtrList<T>& list);
217 
218  //- Copy (clone) assignment
219  inline void operator=(const PtrDynList<T, SizeMin>& list);
220 
221  //- Copy (clone) assignment with different sizing parameters
222  template<int AnySizeMin>
223  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
224 
225  //- Move assignment
226  inline void operator=(PtrList<T>&& list);
227 
228  //- Move assignment
229  inline void operator=(PtrDynList<T, SizeMin>&& list);
230 
231  //- Move assignment with different sizing parameters
232  template<int AnySizeMin>
233  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
234 };
235 
236 
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 
239 } // End namespace Foam
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #include "PtrDynListI.H"
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 #endif
248 
249 // ************************************************************************* //
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr,.
Definition: PtrDynList.H:111
Foam::PtrDynList::swap
void swap(PtrDynList< T, AnySizeMin > &other)
Swap content, independent of sizing parameter.
Definition: PtrDynListI.H:201
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::PtrDynList::expandStorage
label expandStorage() noexcept
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:163
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:87
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:412
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:228
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:54
Foam::PtrDynList::resize
void resize(const label newLen)
Alter the addressed list size.
Definition: PtrDynListI.H:118
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:175
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:310
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::PtrDynList::setSize
void setSize(const label n)
Same as resize()
Definition: PtrDynList.H:123
Foam::PtrDynList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:155
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrDynList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: PtrDynListI.H:147
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
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::PtrDynList::emplace
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
PtrList.H
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Default construct.
Definition: PtrDynListI.H:35
Foam::PtrDynList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:221
Foam::PtrDynList::reserve
void reserve(const label len)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:101
args
Foam::argList args(argc, argv)
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:190
Foam::PtrDynList::get
const T * get(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrDynListI.H:94
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60