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-2020 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 PtrList_H
47 #define PtrList_H
48 
49 #include "UPtrList.H"
50 #include "SLPtrListFwd.H"
51 #include <memory>
52 #include <utility>
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward Declarations
60 template<class T> class autoPtr;
61 template<class T> class refPtr;
62 template<class T> class tmp;
63 template<class T> class PtrList;
64 template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
65 
66 /*---------------------------------------------------------------------------*\
67  Class PtrList Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 template<class T>
71 class PtrList
72 :
73  public UPtrList<T>
74 {
75 protected:
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 
86 public:
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 also 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  //- Construct and set an element
177  template<class... Args>
178  inline autoPtr<T> emplace(const label i, Args&&... args);
179 
180  //- Set element to given pointer and return old element (can be null)
181  // No-op if the new pointer value is identical to the current content.
182  inline autoPtr<T> set(const label i, T* ptr);
183 
184  //- Set element to given autoPtr and return old element
185  inline autoPtr<T> set(const label i, autoPtr<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 unique_ptr and return old element
191  inline autoPtr<T> set(const label i, std::unique_ptr<T>&& ptr);
192 
193  //- Set element to given refPtr and return old element
194  inline autoPtr<T> set(const label i, const refPtr<T>& ptr);
195 
196  //- Set element to given tmp and return old element
197  inline autoPtr<T> set(const label i, const tmp<T>& ptr);
198 
199  //- Release ownership of the pointer at the given position.
200  // Out of bounds addressing is a no-op and returns nullptr.
201  inline autoPtr<T> release(const label i);
202 
203  //- Transfer into this list and annul the argument list
204  inline void transfer(PtrList<T>& list);
205 
206 
207  // Member Operators
208 
209  //- Copy assignment.
210  // For existing list entries, values are copied from the list.
211  // For new list entries, pointers are cloned from the list.
212  void operator=(const PtrList<T>& list);
213 
214  //- Move assignment
215  inline void operator=(PtrList<T>&& list);
216 
217 
218  // IOstream operator
219 
220  //- Read from Istream, discarding contents of existing list
221  friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
222 };
223 
224 
225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 
227 } // End namespace Foam
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
231 #include "PtrListI.H"
232 
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 
235 #ifdef NoRepository
236  #include "PtrList.C"
237  #include "PtrListIO.C"
238 #endif
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
242 #endif
243 
244 // ************************************************************************* //
UPtrList.H
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::PtrList::emplace_append
void emplace_append(Args &&... args)
Construct and append an element to the end of the list.
Definition: PtrListI.H:106
Foam::PtrList::operator=
void operator=(const PtrList< T > &list)
Copy assignment.
Definition: PtrList.C:133
Foam::PtrList::release
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:233
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrList.H:138
SLPtrListFwd.H
Forward declarations for SLPtrList.
Foam::LPtrList
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:50
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:51
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
PtrList.C
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::PtrList::setSize
void setSize(const label newLen)
Same as resize()
Definition: PtrList.H:151
Foam::PtrList::PtrList
constexpr PtrList() noexcept
Default construct.
Definition: PtrListI.H:45
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:62
Foam::PtrList::readIstream
void readIstream(Istream &is, const INew &inew)
Read from Istream using Istream constructor class.
Definition: PtrListIO.C:38
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::PtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:113
PtrListIO.C
Foam::PtrList::clone
PtrList< T > clone(Args &&... args) const
Make a copy by cloning each of the list elements.
Foam::PtrList::clear
void clear()
Clear the PtrList. Delete allocated entries and set size to zero.
Definition: PtrListI.H:97
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrList::resize
void resize(const label newLen)
Adjust size of PtrList.
Definition: PtrList.C:103
Foam::PtrList::free
void free()
Delete the allocated entries, but retain the list size.
Definition: PtrListI.H:36
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::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::PtrList::emplace
autoPtr< T > emplace(const label i, Args &&... args)
Construct and set an element.
args
Foam::argList args(argc, argv)
PtrListI.H
Foam::PtrList::transfer
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:245
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60