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-2020 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  // Private Member Functions
75 
76  //- Adjust addressable size
77  void setAddressableSize(const label len);
78 
79 
80 public:
81 
82  // Constructors
83 
84  //- Construct null
85  inline constexpr PtrDynList() noexcept;
86 
87  //- Construct with given capacity.
88  inline explicit PtrDynList(const label len);
89 
90  //- Copy construct using 'clone()' method on each element
91  inline PtrDynList(const PtrDynList<T, SizeMin>& list);
92 
93  //- Move construct
94  inline PtrDynList(PtrDynList<T, SizeMin>&& list);
95 
96  //- Take ownership of pointers in the list, set old pointers to null.
97  inline explicit PtrDynList(UList<T*>& list);
98 
99 
100  //- Destructor
101  ~PtrDynList() = default;
102 
103 
104  // Member Functions
105 
106  // Access
107 
108  //- Size of the underlying storage.
109  inline label capacity() const noexcept;
110 
111  //- Return const pointer to element (can be nullptr),
112  // with bounds checking.
113  inline const T* get(const label i) const;
114 
115  //- Return const pointer to element (if set) or nullptr,
116  // with bounds checking.
117  // The return value can be tested as a bool.
118  const T* set(const label i) const { return this->get(i); }
119 
120 
121  // Edit
122 
123  //- Delete the allocated entries, but retain the list size.
124  using PtrList<T>::free;
125 
126  //- Alter the size of the underlying storage.
127  inline void setCapacity(const label nElem);
128 
129  //- Alter the addressed list size.
130  inline void resize(const label newLen);
131 
132  //- Same as resize()
133  inline void setSize(const label newLen);
134 
135  //- Reserve allocation space for at least this size.
136  // Never shrinks the allocated size, use setCapacity() for that.
137  inline void reserve(const label nElem);
138 
139  //- Clear the addressed list, i.e. set the size to zero.
140  // Allocated size does not change
141  inline void clear();
142 
143  //- Clear the list and delete storage.
144  inline void clearStorage();
145 
146  //- Expand the addressable size to fit the allocated capacity.
147  // Returns the previous addressable size.
148  inline label expandStorage();
149 
150  //- Shrink the allocated space to the number of elements used.
151  inline void shrink();
152 
153  //- Squeeze out intermediate nullptr entries in the list of pointers
154  //- and adjust the addressable size accordingly.
155  // \return the number of non-null entries
156  inline label squeezeNull();
157 
158  //- Construct and append an element to the end of the list
159  template<class... Args>
160  inline void emplace_append(Args&&... args);
161 
162  //- Append an element to the end of the list
163  inline void append(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(autoPtr<T>&& ptr);
170 
171  //- Move append an element to the end of the list
172  inline void append(std::unique_ptr<T>&& ptr);
173 
174  //- Move or clone append a tmp to the end of the list
175  inline void append(const refPtr<T>& ptr);
176 
177  //- Move or clone append a tmp to the end of the list
178  inline void append(const tmp<T>& ptr);
179 
180  //- Move append another list to the end of this list.
181  inline void append(PtrList<T>&& other);
182 
183  //- Move append another list to the end of this list.
184  template<int AnySizeMin>
185  inline void append(PtrDynList<T, AnySizeMin>&& other);
186 
187  //- Remove and return the top element
188  inline autoPtr<T> remove();
189 
190  //- Construct and set an element
191  template<class... Args>
192  inline autoPtr<T> emplace(const label i, Args&&... args);
193 
194  //- Set element to given pointer and return old element (can be null)
195  inline autoPtr<T> set(const label i, 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 autoPtr and return old element
201  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
202 
203  //- Set element to given pointer and return old element
204  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
205 
206  //- Set element to given refPtr and return old element
207  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
208 
209  //- Set element to given tmp and return old element
210  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
211 
212  //- Reorder elements. Reordering must be unique (ie, shuffle).
213  inline void reorder(const labelUList& oldToNew);
214 
215 
216  // Member Operators
217 
218  //- Copy (clone) assignment
219  inline void operator=(const PtrList<T>& list);
220 
221  //- Copy (clone) assignment
222  inline void operator=(const PtrDynList<T, SizeMin>& list);
223 
224  //- Copy (clone) assignment with different sizing parameters
225  template<int AnySizeMin>
226  inline void operator=(const PtrDynList<T, AnySizeMin>& list);
227 
228  //- Move assignment
229  inline void operator=(PtrList<T>&& list);
230 
231  //- Move assignment
232  inline void operator=(PtrDynList<T, SizeMin>&& list);
233 
234  //- Move assignment with different sizing parameters
235  template<int AnySizeMin>
236  inline void operator=(PtrDynList<T, AnySizeMin>&& list);
237 
238 };
239 
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 } // End namespace Foam
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 #include "PtrDynListI.H"
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
Foam::PtrDynList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: PtrDynListI.H:127
Foam::PtrDynList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr,.
Definition: PtrDynList.H:117
Foam::PtrDynList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrDynListI.H:172
Foam::PtrDynList::expandStorage
label expandStorage()
Expand the addressable size to fit the allocated capacity.
Definition: PtrDynListI.H:195
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::PtrDynList::capacity
label capacity() const noexcept
Size of the underlying storage.
Definition: PtrDynListI.H:96
PtrDynListI.H
Foam::PtrDynList::reorder
void reorder(const labelUList &oldToNew)
Reorder elements. Reordering must be unique (ie, shuffle).
Definition: PtrDynListI.H:425
Foam::PtrDynList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrDynListI.H:241
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:144
Foam::PtrDynList::shrink
void shrink()
Shrink the allocated space to the number of elements used.
Definition: PtrDynListI.H:207
Foam::PtrDynList::remove
autoPtr< T > remove()
Remove and return the top element.
Definition: PtrDynListI.H:323
Foam::PtrDynList::setCapacity
void setCapacity(const label nElem)
Alter the size of the underlying storage.
Definition: PtrDynListI.H:110
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:62
Foam::PtrDynList::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: PtrDynListI.H:187
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:179
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.
Foam::PtrDynList::operator=
void operator=(const PtrList< T > &list)
Copy (clone) assignment.
Definition: PtrDynListI.H:437
PtrList.H
Foam::PtrDynList::PtrDynList
constexpr PtrDynList() noexcept
Construct null.
Definition: PtrDynListI.H:44
Foam::PtrDynList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrDynListI.H:234
args
Foam::argList args(argc, argv)
Foam::PtrDynList::squeezeNull
label squeezeNull()
Definition: PtrDynListI.H:224
Foam::PtrDynList::get
const T * get(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrDynListI.H:103
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60