PtrList.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::PtrList
29 
30 Description
31  A list of pointers to objects of type <T>, with allocation/deallocation
32  management of the pointers.
33  The operator[] returns a reference to the object, not the pointer.
34 
35 See Also
36  Foam::UPtrList
37  Foam::PtrDynList
38 
39 SourceFiles
40  PtrListI.H
41  PtrList.C
42  PtrListIO.C
43 
44 \*---------------------------------------------------------------------------*/
45 
46 #ifndef Foam_PtrList_H
47 #define Foam_PtrList_H
48 
49 #include "UPtrList.H"
50 #include "SLPtrListFwd.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward Declarations
58 template<class T> class autoPtr;
59 template<class T> class refPtr;
60 template<class T> class tmp;
61 template<class T> class PtrList;
62 template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
63 
64 /*---------------------------------------------------------------------------*\
65  Class PtrList Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 template<class T>
69 class PtrList
70 :
71  public UPtrList<T>
72 {
73 protected:
74 
75  // Protected Member Functions
76 
77  //- Read from Istream using Istream constructor class
78  template<class INew>
79  void readIstream(Istream& is, const INew& inew);
80 
81 
82 public:
83 
84  // Constructors
85 
86  //- Default construct
87  inline constexpr PtrList() noexcept;
88 
89  //- Construct with specified size, each element initialized to nullptr
90  inline explicit PtrList(const label len);
91 
92  //- Copy construct using 'clone()' method on each element
93  inline PtrList(const PtrList<T>& list);
94 
95  //- Move construct
96  inline PtrList(PtrList<T>&& list) noexcept;
97 
98  //- Take ownership of pointers in the list, set old pointers to null.
99  inline explicit PtrList(UList<T*>& list);
100 
101  //- Copy construct using 'clone()' method on each element
102  template<class CloneArg>
103  inline PtrList(const PtrList<T>& list, const CloneArg& cloneArgs);
104 
105  //- Construct as copy or re-use as specified
106  PtrList(PtrList<T>& list, bool reuse);
107 
108  //- Copy construct using 'clone()' on each element of SLPtrList<T>
109  explicit PtrList(const SLPtrList<T>& list);
110 
111  //- Construct from Istream using given Istream constructor class
112  template<class INew>
113  PtrList(Istream& is, const INew& inew);
114 
115  //- Construct from Istream using default Istream constructor class
116  PtrList(Istream& is);
117 
118 
119  //- Destructor. Frees all pointers
120  ~PtrList();
121 
122 
123  // Member Functions
124 
125  //- Make a copy by cloning each of the list elements.
126  template<class... Args>
127  PtrList<T> clone(Args&&... args) const;
128 
129 
130  // Access
131 
132  //- Return const pointer to element (can be nullptr),
133  //- or nullptr for out-of-range access (ie, \em with bounds checking).
134  // The return value can be tested as a bool.
135  const T* set(const label i) const { return UPtrList<T>::set(i); }
136 
137 
138  // Edit
139 
140  //- Clear the PtrList. Delete allocated entries and set size to zero.
141  inline void clear();
142 
143  //- Free memory and nullify all entries. Does not change the list size.
144  inline void free();
145 
146  //- Adjust size of PtrList.
147  // New entries are initialized to nullptr, removed entries are deleted
148  void resize(const label newLen);
149 
150  //- Set the addressed list to the given size,
151  //- deleting all existing entries.
152  //- Afterwards the list contains all \c nullptr entries.
153  inline void resize_null(const label newLen);
154 
155  //- Construct and append an element to the end of the list,
156  //- return reference to the new list element
157  template<class... Args>
158  inline T& emplace_back(Args&&... args);
160  //- Append an element to the end of the list
161  inline void push_back(T* ptr);
162 
163  //- Move append an element to the end of the list
164  inline void push_back(std::unique_ptr<T>&& ptr);
165 
166  //- Move append an element to the end of the list
167  inline void push_back(autoPtr<T>&& ptr);
168 
169  //- Move or clone append a refPtr to the end of the list
170  inline void push_back(const refPtr<T>& ptr);
171 
172  //- Move or clone append a tmp to the end of the list
173  inline void push_back(const tmp<T>& ptr);
174 
175  //- Move append another list to the end of this list.
176  inline void push_back(PtrList<T>&& other);
177 
178  //- Construct and set a new element at given position,
179  //- (discard old element at that location).
180  // \param i - the location to set
181  // \param args arguments to forward to the constructor of the element
182  // \return reference to the new list element.
183  template<class... Args>
184  inline T& emplace_set(const label i, Args&&... args);
185 
186  //- Same as emplace_set()
187  template<class... Args>
188  inline T& emplace(const label i, Args&&... args);
189 
190  //- Like emplace_set() but will not overwrite an occupied (non-null)
191  //- location.
192  // \param i - the location to set (unless already defined)
193  // \param args arguments to forward to the constructor of the element
194  // \return reference to the existing or the new list element.
195  template<class... Args>
196  inline T& try_emplace(const label i, Args&&... args);
197 
198  //- Set element to given pointer and return old element (can be null)
199  // No-op if the new pointer value is identical to the current content.
200  inline autoPtr<T> set(const label i, T* ptr);
201 
202  //- Set element to given unique_ptr and return old element
203  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
204 
205  //- Set element to given autoPtr and return old element
206  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
207 
208  //- Set element to given refPtr and return old element
209  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
210 
211  //- Set element to given tmp and return old element
212  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
213 
214  //- Release ownership of the pointer at the given position.
215  // Out of bounds addressing is a no-op and returns nullptr.
216  inline autoPtr<T> release(const label i);
217 
218  //- Transfer into this list and annul the argument list
219  inline void transfer(PtrList<T>& list);
220 
221 
222  // Member Operators
223 
224  //- Copy assignment.
225  // For existing list entries, values are copied from the list.
226  // For new list entries, pointers are cloned from the list.
227  void operator=(const PtrList<T>& list);
228 
229  //- Move assignment
230  inline void operator=(PtrList<T>&& list);
231 
232 
233  // IOstream Operators
234 
235  //- Read from Istream, discarding contents of existing list
236  friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
237 
238 
239  // Housekeeping
240 
241  //- Disallow push_back with autoPtr without std::move
242  void push_back(autoPtr<T>& ptr) = delete;
243 
244  //- Set element to given autoPtr and return old element
245  //FOAM_DEPRECATED_FOR(2022-10, "set(autoPtr&&))")
246  autoPtr<T> set(const label i, autoPtr<T>& ptr)
247  {
248  return this->set(i, ptr.release());
249  }
250 
251  //- Same as resize()
252  void setSize(const label newLen) { this->resize(newLen); }
253 
254  //- Move append an element to the end of the list
255  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
256  void append(autoPtr<T>& ptr) { this->push_back(ptr.release()); }
257 
258  //- Append an element to the end of the list
259  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
260  void append(T* ptr) { this->push_back(ptr); }
261 
262  //- Move append an element to the end of the list
263  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
264  void append(std::unique_ptr<T>&& ptr)
265  {
266  this->push_back(ptr.release());
267  }
268 
269  //- Move append an element to the end of the list
270  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
271  void append(autoPtr<T>&& ptr) { this->push_back(ptr.release()); }
272 
273  //- Move or clone append a tmp to the end of the list
274  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
275  void append(const refPtr<T>& ptr) { this->push_back(ptr); }
276 
277  //- Move or clone append a tmp to the end of the list
278  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
279  void append(const tmp<T>& ptr) { this->push_back(ptr); }
280 
281  //- Move append another list to the end of this list.
282  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
283  void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
284 };
285 
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 
289 } // End namespace Foam
290 
291 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
292 
293 #include "PtrListI.H"
294 
295 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
296 
297 #ifdef NoRepository
298  #include "PtrList.C"
299  #include "PtrListIO.C"
300 #endif
301 
302 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
303 
304 #endif
305 
306 // ************************************************************************* //
LPtrList< SLListBase, T > SLPtrList
Definition: SLPtrListFwd.H:41
Forward declarations for SLPtrList.
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:30
A class for managing references or pointers (no reference counting)
Definition: HashPtrTable.H:49
T & emplace_back(Args &&... args)
Construct and append an element to the end of the list, return reference to the new list element...
Definition: PtrListI.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: PtrListI.H:96
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:29
Istream & operator>>(Istream &, directionInfo &)
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: PtrList.H:159
void free()
Free memory and nullify all entries. Does not change the list size.
Definition: PtrListI.H:89
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:121
T * release() noexcept
Release ownership and return the pointer.
Definition: autoPtrI.H:28
const direction noexcept
Definition: Scalar.H:258
T & emplace(const label i, Args &&... args)
Same as emplace_set()
Definition: PtrListI.H:185
T & try_emplace(const label i, Args &&... args)
Like emplace_set() but will not overwrite an occupied (non-null) location.
Definition: PtrListI.H:193
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:337
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:95
void append(autoPtr< T > &ptr)
Move append an element to the end of the list.
Definition: PtrList.H:344
T & emplace_set(const label i, Args &&... args)
Construct and set a new element at given position, (discard old element at that location).
Definition: PtrListI.H:174
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:260
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:114
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:81
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:272
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Foam::argList args(argc, argv)
Namespace for OpenFOAM.
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrList.H:366