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-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::PtrList
29
30Description
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
35See Also
36 Foam::UPtrList
37 Foam::PtrDynList
38
39SourceFiles
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#include <memory>
52#include <utility>
53
54// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55
56namespace Foam
57{
58
59// Forward Declarations
60template<class T> class autoPtr;
61template<class T> class refPtr;
62template<class T> class tmp;
63template<class T> class PtrList;
64template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
65
66/*---------------------------------------------------------------------------*\
67 Class PtrList Declaration
68\*---------------------------------------------------------------------------*/
69
70template<class T>
71class PtrList
72:
73 public UPtrList<T>
74{
75protected:
76
77 // Protected Member Functions
78
79 //- Read from Istream using Istream constructor class
80 template<class INew>
81 void readIstream(Istream& is, const INew& inew);
82
83 //- Delete the allocated entries, but retain the list size.
84 inline void free();
85
86public:
87
88 // Constructors
89
90 //- Default construct
91 inline constexpr PtrList() noexcept;
92
93 //- Construct with specified size, each element initialized to nullptr
94 inline explicit PtrList(const label len);
95
96 //- Copy construct using 'clone()' method on each element
97 inline PtrList(const PtrList<T>& list);
98
99 //- Move construct
100 inline PtrList(PtrList<T>&& list);
101
102 //- Take ownership of pointers in the list, set old pointers to null.
103 inline explicit PtrList(UList<T*>& list);
104
105 //- Copy construct using 'clone()' method on each element
106 template<class CloneArg>
107 inline PtrList(const PtrList<T>& list, const CloneArg& cloneArgs);
108
109 //- Construct as copy or re-use as specified
110 PtrList(PtrList<T>& list, bool reuse);
111
112 //- Copy construct using 'clone()' on each element of SLPtrList<T>
113 explicit PtrList(const SLPtrList<T>& list);
114
115 //- Construct from Istream using given Istream constructor class
116 template<class INew>
117 PtrList(Istream& is, const INew& inew);
118
119 //- Construct from Istream using default Istream constructor class
120 PtrList(Istream& is);
121
122
123 //- Destructor
124 ~PtrList();
125
126
127 // Member Functions
128
129 //- Make a copy by cloning each of the list elements.
130 template<class... Args>
131 PtrList<T> clone(Args&&... args) const;
132
133
134 // Access
135
136 //- Return const pointer to element (can be nullptr),
137 //- without bounds checking - same as get().
138 // The return value can be tested as a bool.
139 const T* set(const label i) const { return this->get(i); }
140
141
142 // Edit
143
144 //- Clear the PtrList. Delete allocated entries and set size to zero.
145 inline void clear();
146
147 //- Adjust size of PtrList.
148 // New entries are initialized to nullptr, removed entries are deleted
149 void resize(const label newLen);
150
151 //- Same as resize()
152 void setSize(const label newLen) { this->resize(newLen); }
153
154 //- Construct and append an element to the end of the list
155 template<class... Args>
156 inline void emplace_append(Args&&... args);
157
158 //- Append an element to the end of the list
159 inline void append(T* ptr);
160
161 //- Move append an element to the end of the list
162 inline void append(autoPtr<T>& ptr);
163
164 //- Move append an element to the end of the list
165 inline void append(autoPtr<T>&& ptr);
166
167 //- Move append an element to the end of the list
168 inline void append(std::unique_ptr<T>&& ptr);
169
170 //- Move or clone append a refPtr to the end of the list
171 inline void append(const refPtr<T>& ptr);
172
173 //- Move or clone append a tmp to the end of the list
174 inline void append(const tmp<T>& ptr);
175
176 //- Move append another list to the end of this list.
177 inline void append(PtrList<T>&& other);
178
179 //- Construct and set an element
180 template<class... Args>
181 inline autoPtr<T> emplace(const label i, Args&&... args);
182
183 //- Set element to given pointer and return old element (can be null)
184 // No-op if the new pointer value is identical to the current content.
185 inline autoPtr<T> set(const label i, T* ptr);
186
187 //- Set element to given autoPtr and return old element
188 inline autoPtr<T> set(const label i, autoPtr<T>& ptr);
189
190 //- Set element to given autoPtr and return old element
191 inline autoPtr<T> set(const label i, autoPtr<T>&& ptr);
192
193 //- Set element to given unique_ptr and return old element
194 inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
195
196 //- Set element to given refPtr and return old element
197 inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
198
199 //- Set element to given tmp and return old element
200 inline autoPtr<T> set(const label i, const tmp<T>& ptr);
201
202 //- Release ownership of the pointer at the given position.
203 // Out of bounds addressing is a no-op and returns nullptr.
204 inline autoPtr<T> release(const label i);
205
206 //- Transfer into this list and annul the argument list
207 inline void transfer(PtrList<T>& list);
208
209
210 // Member Operators
211
212 //- Copy assignment.
213 // For existing list entries, values are copied from the list.
214 // For new list entries, pointers are cloned from the list.
215 void operator=(const PtrList<T>& list);
216
217 //- Move assignment
218 inline void operator=(PtrList<T>&& list);
219
220
221 // IOstream operator
222
223 //- Read from Istream, discarding contents of existing list
224 friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
225};
226
227
228// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229
230} // End namespace Foam
231
232// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233
234#include "PtrListI.H"
235
236// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
237
238#ifdef NoRepository
239 #include "PtrList.C"
240 #include "PtrListIO.C"
241#endif
242
243// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244
245#endif
246
247// ************************************************************************* //
Forward declarations for SLPtrList.
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:75
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:269
const T * set(const label i) const
Definition: PtrList.H:138
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:113
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:257
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:106
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:151
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:36
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
~PtrList()
Destructor.
Definition: PtrList.C:72
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:103
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
T * get(const label i)
Definition: UPtrListI.H:127
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class for managing references or pointers (no reference counting)
Definition: refPtr.H:58
A class for managing temporary objects.
Definition: tmp.H:65
const volScalarField & T
Namespace for OpenFOAM.
Istream & operator>>(Istream &, directionInfo &)
Foam::argList args(argc, argv)