UPtrList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::UPtrList
26 
27 Description
28  A templated 1D list of pointers to objects of type <T>, where the
29  size of the array is known and used for subscript bounds checking, etc.
30 
31  The element operator [] returns a reference to the object rather than a
32  pointer. Storage is not allocated during construction or use but is
33  supplied to the constructor as an argument.
34 
35 SourceFiles
36  UPtrList.C
37  UPtrListIO.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef UPtrList_H
42 #define UPtrList_H
43 
44 #include "List.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward declaration of friend classes
52 template<class T> class PtrList;
53 
54 // Forward declaration of friend functions and operators
55 template<class T> class UPtrList;
56 template<class T> void writeEntry(Ostream& os, const UPtrList<T>&);
57 template<class T> Istream& operator>>(Istream&, UPtrList<T>&);
58 template<class T> Ostream& operator<<(Ostream&, const UPtrList<T>&);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class UPtrList Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 template<class T>
66 class UPtrList
67 {
68  // Private Data
69 
70  List<T*> ptrs_;
71 
72 
73 public:
74 
75  // Related types
76 
77  //- Declare friendship with the PtrList class
78  friend class PtrList<T>;
79 
80 
81  // Constructors
82 
83  //- Null Constructor
84  UPtrList();
85 
86  //- Construct with size specified
87  explicit UPtrList(const label);
88 
89  //- Construct as copy or reuse as specified
90  UPtrList(UPtrList<T>&, bool reuse);
91 
92  //- Construct from an initialiser list of pointers
93  UPtrList(std::initializer_list<T*>);
94 
95 
96  // Member Functions
97 
98  // Access
99 
100  //- Return the number of elements in the UPtrList
101  inline label size() const;
102 
103  //- Return true if the UPtrList is empty (ie, size() is zero)
104  inline bool empty() const;
105 
106  //- Return reference to the first element of the list
107  inline T& first();
108 
109  //- Return reference to first element of the list
110  inline const T& first() const;
111 
112  //- Return reference to the last element of the list
113  inline T& last();
114 
115  //- Return reference to the last element of the list
116  inline const T& last() const;
117 
118 
119  // Edit
120 
121  //- Reset size of UPtrList. This can only be used to set the size
122  // of an empty UPtrList, extend a UPtrList, remove entries from
123  // the end of a UPtrList
124  void setSize(const label);
125 
126  //- Reset size of UPtrList. This can only be used to set the size
127  // of an empty UPtrList, extend a UPtrList, remove entries from
128  // the end of a UPtrList
129  inline void resize(const label);
130 
131  //- Clear the UPtrList, i.e. set size to zero
132  void clear();
133 
134  //- Append an element at the end of the list
135  inline void append(T*);
136 
137  //- Transfer the contents of the argument UPtrList into this
138  // UPtrList and annul the argument list
139  void transfer(UPtrList<T>&);
140 
141  //- Is element set
142  inline bool set(const label) const;
143 
144  //- Set element. Return old element (can be nullptr).
145  // No checks on new element
146  inline T* set(const label, T*);
147 
148  //- Reorders elements. Ordering does not have to be done in
149  // ascending or descending order. Reordering has to be unique.
150  // (is shuffle)
151  void reorder(const labelUList& oldToNew);
152 
153  //- Reorders elements. Ordering does not have to be done in
154  // ascending or descending order. Reordering has to be unique.
155  // Note: can create unset elements
156  void shuffle(const labelUList& newToOld);
157 
158 
159  // Conversion
160 
161  //- Convert to list of different pointer type
162  template<class T2>
164 
165  //- Convert to list of different pointer type
166  template<class T2>
167  UPtrList<const T2> convert() const;
168 
169 
170  // Member Operators
171 
172  //- Return element const reference
173  inline const T& operator[](const label) const;
174 
175  //- Return element reference
176  inline T& operator[](const label);
177 
178  //- Return element const pointer
179  inline const T* operator()(const label) const;
180 
181  //- Return element const pointer
182  inline T* operator()(const label);
183 
184 
185  // STL type definitions
186 
187  //- Type of values the UPtrList contains
188  typedef T value_type;
189 
190  //- Type that can be used for storing into UPtrList::value_type objects
191  typedef T& reference;
192 
193  //- Type that can be used for storing into constant UPtrList::value_type
194  // objects
195  typedef const T& const_reference;
196 
197 
198  // STL iterator
199  // Random access iterator for traversing UPtrList
200 
201  class iterator;
203  friend class iterator;
204 
205  //- An STL iterator
206  class iterator
207  {
208  T** ptr_;
209 
210  public:
211 
212  friend class const_iterator;
213 
214  //- Construct for a given UPtrList entry
215  inline iterator(T**);
216 
217  // Member Operators
218 
219  inline bool operator==(const iterator&) const;
220  inline bool operator!=(const iterator&) const;
221 
222  inline T& operator*();
223  inline T& operator()();
224 
225  inline iterator operator++();
226  inline iterator operator++(const int);
227 
228  inline iterator operator--();
229  inline iterator operator--(const int);
230 
231  inline iterator operator+=(const label);
232  inline iterator operator-=(const label);
233 
234  inline iterator operator+(const label) const;
235  inline iterator operator-(const label) const;
236 
237  inline label operator-(const iterator&) const;
238 
239  inline T& operator[](const label);
240 
241  inline bool operator<(const iterator&) const;
242  inline bool operator>(const iterator&) const;
243 
244  inline bool operator<=(const iterator&) const;
245  inline bool operator>=(const iterator&) const;
246  };
247 
248  //- Return an iterator to begin traversing the UPtrList
249  inline iterator begin();
250 
251  //- Return an iterator to end traversing the UPtrList
252  inline iterator end();
253 
254 
255  // STL const_iterator
256  // Random access iterator for traversing UPtrList
257 
258  //- An STL-conforming const_iterator
259  class const_iterator
260  {
261  const T* const* ptr_;
262 
263  public:
264 
265  //- Construct for a given UPtrList entry
266  inline const_iterator(const T* const*);
267 
268  //- Construct from an iterator
269  inline const_iterator(const iterator&);
270 
271 
272  // Member Operators
273 
274  inline bool operator==(const const_iterator&) const;
275  inline bool operator!=(const const_iterator&) const;
276 
277  typedef const T& Tref;
278  inline Tref operator*();
279  inline Tref operator()();
280 
281  inline const_iterator operator++();
282  inline const_iterator operator++(const int);
283 
284  inline const_iterator operator--();
285  inline const_iterator operator--(const int);
286 
287  inline const_iterator operator+=(const label);
288  inline const_iterator operator-=(const label);
289 
290  inline const_iterator operator+(const label) const;
291  inline const_iterator operator-(const label) const;
292 
293  inline label operator-(const const_iterator&) const;
294 
295  inline const T& operator[](const label);
296 
297  inline bool operator<(const const_iterator&) const;
298  inline bool operator>(const const_iterator&) const;
299 
300  inline bool operator<=(const const_iterator&) const;
301  inline bool operator>=(const const_iterator&) const;
302  };
303 
304  //- Return an const_iterator to begin traversing the UPtrList
305  inline const_iterator cbegin() const;
306 
307  //- Return an const_iterator to end traversing the UPtrList
308  inline const_iterator cend() const;
309 
310  //- Return an const_iterator to begin traversing the UPtrList
311  inline const_iterator begin() const;
312 
313  //- Return an const_iterator to end traversing the UPtrList
314  inline const_iterator end() const;
315 
316 
317  // IOstream operator
318 
319  //- Write UPtrList to Ostream
320  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
321 };
322 
323 
324 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325 
326 } // End namespace Foam
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 #include "UPtrListI.H"
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 #ifdef NoRepository
335  #include "UPtrList.C"
336 #endif
337 
338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
339 
340 #endif
341 
342 // ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
An STL-conforming const_iterator.
Definition: UPtrList.H:259
const_iterator operator-(const label) const
Definition: UPtrListI.H:439
bool operator!=(const const_iterator &) const
Definition: UPtrListI.H:350
bool operator>(const const_iterator &) const
Definition: UPtrListI.H:475
bool operator<=(const const_iterator &) const
Definition: UPtrListI.H:485
const T & operator[](const label)
Definition: UPtrListI.H:457
bool operator>=(const const_iterator &) const
Definition: UPtrListI.H:495
const_iterator operator--()
Definition: UPtrListI.H:393
bool operator==(const const_iterator &) const
Definition: UPtrListI.H:340
const_iterator operator+=(const label)
Definition: UPtrListI.H:412
bool operator<(const const_iterator &) const
Definition: UPtrListI.H:465
const_iterator(const T *const *)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:325
const_iterator operator+(const label) const
Definition: UPtrListI.H:430
const_iterator operator-=(const label)
Definition: UPtrListI.H:421
const_iterator operator++()
Definition: UPtrListI.H:374
An STL iterator.
Definition: UPtrList.H:206
bool operator!=(const iterator &) const
Definition: UPtrListI.H:167
bool operator>(const iterator &) const
Definition: UPtrListI.H:286
iterator operator-(const label) const
Definition: UPtrListI.H:254
T & operator[](const label)
Definition: UPtrListI.H:272
bool operator==(const iterator &) const
Definition: UPtrListI.H:160
bool operator<(const iterator &) const
Definition: UPtrListI.H:279
iterator operator+(const label) const
Definition: UPtrListI.H:245
iterator operator-=(const label)
Definition: UPtrListI.H:236
bool operator<=(const iterator &) const
Definition: UPtrListI.H:293
bool operator>=(const iterator &) const
Definition: UPtrListI.H:300
iterator operator+=(const label)
Definition: UPtrListI.H:227
iterator(T **)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:153
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:308
UPtrList< T2 > convert()
Convert to list of different pointer type.
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:43
T value_type
Type of values the UPtrList contains.
Definition: UPtrList.H:187
bool set(const label) const
Is element set.
Definition: UPtrListI.H:87
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:94
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:316
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
void resize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrListI.H:71
bool empty() const
Return true if the UPtrList is empty (ie, size() is zero)
Definition: UPtrListI.H:36
const T & operator[](const label) const
Return element const reference.
Definition: UPtrListI.H:105
T & reference
Type that can be used for storing into UPtrList::value_type objects.
Definition: UPtrList.H:190
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:521
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:529
void reorder(const labelUList &oldToNew)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:101
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:149
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:87
void append(T *)
Append an element at the end of the list.
Definition: UPtrListI.H:78
const T * operator()(const label) const
Return element const pointer.
Definition: UPtrListI.H:137
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:61
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:57
const T & const_reference
Type that can be used for storing into constant UPtrList::value_type.
Definition: UPtrList.H:194
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void T(LagrangianPatchField< Type > &f, const LagrangianPatchField< Type > &f1)
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)