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-2022 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  //- Delete the allocated entries, but retain the list size.
82  inline void free();
83 
84 public:
85 
86  // Constructors
87 
88  //- Default construct
89  inline constexpr PtrList() noexcept;
90 
91  //- Construct with specified size, each element initialized to nullptr
92  inline explicit PtrList(const label len);
93 
94  //- Copy construct using 'clone()' method on each element
95  inline PtrList(const PtrList<T>& list);
96 
97  //- Move construct
98  inline PtrList(PtrList<T>&& list);
99 
100  //- Take ownership of pointers in the list, set old pointers to null.
101  inline explicit PtrList(UList<T*>& list);
102 
103  //- Copy construct using 'clone()' method on each element
104  template<class CloneArg>
105  inline PtrList(const PtrList<T>& list, const CloneArg& cloneArgs);
106 
107  //- Construct as copy or re-use as specified
108  PtrList(PtrList<T>& list, bool reuse);
109 
110  //- Copy construct using 'clone()' on each element of SLPtrList<T>
111  explicit PtrList(const SLPtrList<T>& list);
112 
113  //- Construct from Istream using given Istream constructor class
114  template<class INew>
115  PtrList(Istream& is, const INew& inew);
116 
117  //- Construct from Istream using default Istream constructor class
118  PtrList(Istream& is);
119 
120 
121  //- Destructor. Frees all pointers
122  ~PtrList();
123 
124 
125  // Member Functions
126 
127  //- Make a copy by cloning each of the list elements.
128  template<class... Args>
129  PtrList<T> clone(Args&&... args) const;
130 
131 
132  // Access
133 
134  //- Return const pointer to element (can be nullptr),
135  //- or nullptr for out-of-range access (ie, \em with bounds checking).
136  // The return value can be tested as a bool.
137  const T* set(const label i) const { return UPtrList<T>::set(i); }
138 
139 
140  // Edit
141 
142  //- Clear the PtrList. Delete allocated entries and set size to zero.
143  inline void clear();
144 
145  //- Adjust size of PtrList.
146  // New entries are initialized to nullptr, removed entries are deleted
147  void resize(const label newLen);
148 
149  //- Same as resize()
150  void setSize(const label newLen) { this->resize(newLen); }
151 
152  //- Construct and append an element to the end of the list
153  template<class... Args>
154  inline void emplace_back(Args&&... args);
155 
156  //- Append an element to the end of the list
157  inline void push_back(T* ptr);
158 
159  //- Move append an element to the end of the list
160  inline void push_back(std::unique_ptr<T>&& ptr);
161 
162  //- Move append an element to the end of the list
163  inline void push_back(autoPtr<T>&& ptr);
164 
165  //- Move or clone append a refPtr to the end of the list
166  inline void push_back(const refPtr<T>& ptr);
167 
168  //- Move or clone append a tmp to the end of the list
169  inline void push_back(const tmp<T>& ptr);
170 
171  //- Move append another list to the end of this list.
172  inline void push_back(PtrList<T>&& other);
173 
174  //- Construct and set an element
175  template<class... Args>
176  inline autoPtr<T> emplace(const label i, Args&&... args);
177 
178  //- Set element to given pointer and return old element (can be null)
179  // No-op if the new pointer value is identical to the current content.
180  inline autoPtr<T> set(const label i, T* ptr);
181 
182  //- Set element to given unique_ptr and return old element
183  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
184 
185  //- Set element to given autoPtr and return old element
186  inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
187 
188  //- Set element to given refPtr and return old element
189  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
190 
191  //- Set element to given tmp and return old element
192  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
193 
194  //- Release ownership of the pointer at the given position.
195  // Out of bounds addressing is a no-op and returns nullptr.
196  inline autoPtr<T> release(const label i);
197 
198  //- Transfer into this list and annul the argument list
199  inline void transfer(PtrList<T>& list);
200 
201 
202  // Member Operators
203 
204  //- Copy assignment.
205  // For existing list entries, values are copied from the list.
206  // For new list entries, pointers are cloned from the list.
207  void operator=(const PtrList<T>& list);
208 
209  //- Move assignment
210  inline void operator=(PtrList<T>&& list);
211 
212 
213  // IOstream operator
214 
215  //- Read from Istream, discarding contents of existing list
216  friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
217 
218 
219  // Housekeeping
220 
221  //- Set element to given autoPtr and return old element
222  autoPtr<T> set(const label i, autoPtr<T>& ptr)
223  {
224  return this->set(i, std::move(ptr));
225  }
226 
227  //- Move append an element to the end of the list
228  void append(autoPtr<T>& ptr) { this->push_back(std::move(ptr)); }
229 
230  //- Append an element to the end of the list
231  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
232  void append(T* ptr) { this->push_back(ptr); }
233 
234  //- Move append an element to the end of the list
235  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
236  void append(std::unique_ptr<T>&& ptr)
237  {
238  this->push_back(std::move(ptr));
239  }
240 
241  //- Move append an element to the end of the list
242  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
243  void append(autoPtr<T>&& ptr) { this->push_back(std::move(ptr)); }
244 
245  //- Move or clone append a tmp to the end of the list
246  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
247  void append(const refPtr<T>& ptr) { this->push_back(ptr); }
248 
249  //- Move or clone append a tmp to the end of the list
250  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
251  void append(const tmp<T>& ptr) { this->push_back(ptr); }
252 
253  //- Move append another list to the end of this list.
254  //FOAM_DEPRECATED_FOR(2022-10, "push_back()")
255  void append(PtrList<T>&& other) { this->push_back(std::move(other)); }
256 };
257 
258 
259 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
260 
261 } // End namespace Foam
262 
263 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
264 
265 #include "PtrListI.H"
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 #ifdef NoRepository
270  #include "PtrList.C"
271  #include "PtrListIO.C"
272 #endif
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #endif
277 
278 // ************************************************************************* //
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 emplace_back(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:99
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
const T * set(const label i) const
Return const pointer to element (can be nullptr), or nullptr for out-of-range access (ie...
Definition: UPtrListI.H:134
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:38
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:163
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:29
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:125
const direction noexcept
Definition: Scalar.H:258
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:183
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:304
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:232
void push_back(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:106
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:90
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:244
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.