All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2024 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 
93  // Member Functions
94 
95  // Access
96 
97  //- Return the number of elements in the UPtrList
98  inline label size() const;
99 
100  //- Return true if the UPtrList is empty (ie, size() is zero)
101  inline bool empty() const;
102 
103  //- Return reference to the first element of the list
104  inline T& first();
105 
106  //- Return reference to first element of the list
107  inline const T& first() const;
108 
109  //- Return reference to the last element of the list
110  inline T& last();
111 
112  //- Return reference to the last element of the list
113  inline const T& last() const;
114 
115 
116  // Edit
117 
118  //- Reset size of UPtrList. This can only be used to set the size
119  // of an empty UPtrList, extend a UPtrList, remove entries from
120  // the end of a UPtrList
121  void setSize(const label);
122 
123  //- Reset size of UPtrList. This can only be used to set the size
124  // of an empty UPtrList, extend a UPtrList, remove entries from
125  // the end of a UPtrList
126  inline void resize(const label);
127 
128  //- Clear the UPtrList, i.e. set size to zero
129  void clear();
130 
131  //- Append an element at the end of the list
132  inline void append(T*);
133 
134  //- Transfer the contents of the argument UPtrList into this
135  // UPtrList and annul the argument list
136  void transfer(UPtrList<T>&);
137 
138  //- Is element set
139  inline bool set(const label) const;
140 
141  //- Set element. Return old element (can be nullptr).
142  // No checks on new element
143  inline T* set(const label, T*);
144 
145  //- Reorders elements. Ordering does not have to be done in
146  // ascending or descending order. Reordering has to be unique.
147  // (is shuffle)
148  void reorder(const labelUList& oldToNew);
149 
150  //- Reorders elements. Ordering does not have to be done in
151  // ascending or descending order. Reordering has to be unique.
152  // Note: can create unset elements
153  void shuffle(const labelUList& newToOld);
154 
155 
156  // Conversion
157 
158  //- Convert to list of different pointer type
159  template<class T2>
161 
162  //- Convert to list of different pointer type
163  template<class T2>
164  UPtrList<const T2> convert() const;
165 
166 
167  // Member Operators
168 
169  //- Return element const reference
170  inline const T& operator[](const label) const;
171 
172  //- Return element reference
173  inline T& operator[](const label);
174 
175  //- Return element const pointer
176  inline const T* operator()(const label) const;
177 
178  //- Return element const pointer
179  inline T* operator()(const label);
180 
181 
182  // STL type definitions
183 
184  //- Type of values the UPtrList contains
185  typedef T value_type;
186 
187  //- Type that can be used for storing into UPtrList::value_type objects
188  typedef T& reference;
189 
190  //- Type that can be used for storing into constant UPtrList::value_type
191  // objects
192  typedef const T& const_reference;
193 
194 
195  // STL iterator
196  // Random access iterator for traversing UPtrList
197 
198  class iterator;
200  friend class iterator;
201 
202  //- An STL iterator
203  class iterator
204  {
205  T** ptr_;
206 
207  public:
208 
209  friend class const_iterator;
210 
211  //- Construct for a given UPtrList entry
212  inline iterator(T**);
213 
214  // Member Operators
215 
216  inline bool operator==(const iterator&) const;
217  inline bool operator!=(const iterator&) const;
218 
219  inline T& operator*();
220  inline T& operator()();
221 
222  inline iterator operator++();
223  inline iterator operator++(const int);
224 
225  inline iterator operator--();
226  inline iterator operator--(const int);
227 
228  inline iterator operator+=(const label);
229  inline iterator operator-=(const label);
230 
231  inline iterator operator+(const label) const;
232  inline iterator operator-(const label) const;
233 
234  inline label operator-(const iterator&) const;
235 
236  inline T& operator[](const label);
237 
238  inline bool operator<(const iterator&) const;
239  inline bool operator>(const iterator&) const;
240 
241  inline bool operator<=(const iterator&) const;
242  inline bool operator>=(const iterator&) const;
243  };
244 
245  //- Return an iterator to begin traversing the UPtrList
246  inline iterator begin();
247 
248  //- Return an iterator to end traversing the UPtrList
249  inline iterator end();
250 
251 
252  // STL const_iterator
253  // Random access iterator for traversing UPtrList
254 
255  //- An STL-conforming const_iterator
256  class const_iterator
257  {
258  const T* const* ptr_;
259 
260  public:
261 
262  //- Construct for a given UPtrList entry
263  inline const_iterator(const T* const*);
264 
265  //- Construct from an iterator
266  inline const_iterator(const iterator&);
267 
268 
269  // Member Operators
270 
271  inline bool operator==(const const_iterator&) const;
272  inline bool operator!=(const const_iterator&) const;
273 
274  typedef const T& Tref;
275  inline Tref operator*();
276  inline Tref operator()();
277 
278  inline const_iterator operator++();
279  inline const_iterator operator++(const int);
280 
281  inline const_iterator operator--();
282  inline const_iterator operator--(const int);
283 
284  inline const_iterator operator+=(const label);
285  inline const_iterator operator-=(const label);
286 
287  inline const_iterator operator+(const label) const;
288  inline const_iterator operator-(const label) const;
289 
290  inline label operator-(const const_iterator&) const;
291 
292  inline const T& operator[](const label);
293 
294  inline bool operator<(const const_iterator&) const;
295  inline bool operator>(const const_iterator&) const;
296 
297  inline bool operator<=(const const_iterator&) const;
298  inline bool operator>=(const const_iterator&) const;
299  };
300 
301  //- Return an const_iterator to begin traversing the UPtrList
302  inline const_iterator cbegin() const;
303 
304  //- Return an const_iterator to end traversing the UPtrList
305  inline const_iterator cend() const;
306 
307  //- Return an const_iterator to begin traversing the UPtrList
308  inline const_iterator begin() const;
309 
310  //- Return an const_iterator to end traversing the UPtrList
311  inline const_iterator end() const;
312 
313 
314  // IOstream operator
315 
316  //- Write UPtrList to Ostream
317  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
318 };
319 
320 
321 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
322 
323 } // End namespace Foam
324 
325 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
326 
327 #include "UPtrListI.H"
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 #ifdef NoRepository
332  #include "UPtrList.C"
333 #endif
334 
335 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
336 
337 #endif
338 
339 // ************************************************************************* //
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:256
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:203
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:184
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:87
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:187
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:94
void shuffle(const labelUList &newToOld)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:142
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:80
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:54
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:191
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 writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Istream & operator>>(Istream &, pistonPointEdgeData &)
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)