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-2019 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 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 
57 // Forward Declarations
58 
59 template<class T> class autoPtr;
60 template<class T> class tmp;
61 template<class T> class PtrList;
62 
63 template<class T> Istream& operator>>(Istream& is, PtrList<T>& list);
64 
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  //- Construct null
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 ownerskip 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  //- Clear the PtrList. Delete allocated entries and set size to zero.
134  inline void clear();
135 
136  //- Adjust size of PtrList.
137  // New entries are initialized to nullptr, removed entries are deleted
138  void resize(const label newLen);
139 
140  //- Same as resize()
141  inline void setSize(const label newLen);
142 
143  //- Append an element to the end of the list
144  inline void append(T* ptr);
145 
146  //- Move append an element to the end of the list
147  inline void append(autoPtr<T>& aptr);
148 
149  //- Move append an element to the end of the list
150  inline void append(autoPtr<T>&& aptr);
151 
152  //- Move or clone append a tmp to the end of the list
153  inline void append(const tmp<T>& tptr);
154 
155  //- Transfer into this list and annul the argument list
156  inline void transfer(PtrList<T>& list);
157 
158  //- Return const pointer to element (if set) or nullptr.
159  // The return value can be tested as a bool.
160  inline const T* set(const label i) const;
161 
162  //- Set element to given pointer and return old element (can be null)
163  // No-op if the new pointer value is identical to the current content.
164  inline autoPtr<T> set(const label i, T* ptr);
165 
166  //- Set element to given autoPtr and return old element
167  inline autoPtr<T> set(const label i, autoPtr<T>& aptr);
168 
169  //- Set element to given autoPtr and return old element
170  inline autoPtr<T> set(const label i, autoPtr<T>&& aptr);
171 
172  //- Set element to given tmp and return old element
173  inline autoPtr<T> set(const label i, const tmp<T>& tptr);
174 
175  //- Release ownership of the pointer at the given position.
176  // Out of bounds addressing is a no-op and returns nullptr.
177  inline autoPtr<T> release(const label i);
178 
179 
180  // Member Operators
181 
182  //- Copy assignment.
183  // For existing list entries, values are copied from the list.
184  // For new list entries, pointers are cloned from the list.
185  void operator=(const PtrList<T>& list);
186 
187  //- Move assignment
188  inline void operator=(PtrList<T>&& list);
189 
190 
191  // IOstream operator
192 
193  //- Read from Istream, discarding contents of existing list
194  friend Istream& operator>> <T>(Istream& is, PtrList<T>& list);
195 
196 };
197 
198 
199 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
200 
201 } // End namespace Foam
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 #include "PtrListI.H"
206 
207 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 
209 #ifdef NoRepository
210  #include "PtrList.C"
211  #include "PtrListIO.C"
212 #endif
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 #endif
217 
218 // ************************************************************************* //
UPtrList.H
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::PtrList::release
autoPtr< T > release(const label i)
Release ownership of the pointer at the given position.
Definition: PtrListI.H:178
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
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: PtrListI.H:108
Foam::PtrList::PtrList
constexpr PtrList() noexcept
Construct null.
Definition: PtrListI.H:44
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:63
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:62
Foam::PtrList::append
void append(T *ptr)
Append an element to the end of the list.
Definition: PtrListI.H:115
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:100
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (if set) or nullptr.
Definition: PtrListI.H:143
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:35
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
args
Foam::argList args(argc, argv)
Foam::PtrList::transfer
void transfer(PtrList< T > &list)
Transfer into this list and annul the argument list.
Definition: PtrListI.H:190