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-2018 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 
57 template<class T>
59 
60 template<class T>
61 Ostream& operator<<(Ostream&, const UPtrList<T>&);
62 
63 
64 /*---------------------------------------------------------------------------*\
65  Class UPtrList Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 template<class T>
69 class UPtrList
70 {
71  // Private data
72 
73  List<T*> ptrs_;
74 
75 
76 public:
77 
78  // Related types
79 
80  //- Declare friendship with the UPtrList class
81  friend class PtrList<T>;
82 
83 
84  // Constructors
85 
86  //- Null Constructor
87  UPtrList();
88 
89  //- Construct with size specified
90  explicit UPtrList(const label);
91 
92  //- Construct by transferring the parameter contents
93  UPtrList(const Xfer<UPtrList<T>>&);
94 
95  //- Construct as copy or re-use as specified
96  UPtrList(UPtrList<T>&, bool reuse);
97 
98 
99  // Member functions
100 
101  // Access
102 
103  //- Return the number of elements in the UPtrList
104  inline label size() const;
105 
106  //- Return true if the UPtrList is empty (ie, size() is zero)
107  inline bool empty() const;
108 
109  //- Return reference to the first element of the list
110  inline T& first();
111 
112  //- Return reference to first element of the list
113  inline const T& first() const;
114 
115  //- Return reference to the last element of the list
116  inline T& last();
117 
118  //- Return reference to the last element of the list
119  inline const T& last() const;
120 
121 
122  // Edit
123 
124  //- Reset size of UPtrList. This can only be used to set the size
125  // of an empty UPtrList, extend a UPtrList, remove entries from
126  // the end of a UPtrList
127  void setSize(const label);
128 
129  //- Reset size of UPtrList. This can only be used to set the size
130  // of an empty UPtrList, extend a UPtrList, remove entries from
131  // the end of a UPtrList
132  inline void resize(const label);
133 
134  //- Clear the UPtrList, i.e. set size to zero
135  void clear();
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  //- Transfer contents to the Xfer container
142  inline Xfer<UPtrList<T>> xfer();
143 
144  //- Is element set
145  inline bool set(const label) const;
146 
147  //- Set element. Return old element (can be nullptr).
148  // No checks on new element
149  inline T* set(const label, T*);
150 
151  //- Reorders elements. Ordering does not have to be done in
152  // ascending or descending order. Reordering has to be unique.
153  // (is shuffle)
154  void reorder(const labelUList&);
155 
156 
157  // Member operators
158 
159  //- Return element const reference
160  inline const T& operator[](const label) const;
161 
162  //- Return element reference
163  inline T& operator[](const label);
164 
165  //- Return element const pointer
166  inline const T* operator()(const label) const;
167 
168 
169  // STL type definitions
170 
171  //- Type of values the UPtrList contains
172  typedef T value_type;
173 
174  //- Type that can be used for storing into UPtrList::value_type objects
175  typedef T& reference;
176 
177  //- Type that can be used for storing into constant UPtrList::value_type
178  // objects
179  typedef const T& const_reference;
180 
181 
182  // STL iterator
183  // Random access iterator for traversing UPtrList
184 
185  class iterator;
186  class const_iterator;
187  friend class iterator;
188 
189  //- An STL iterator
190  class iterator
191  {
192  T** ptr_;
193 
194  public:
196  friend class const_iterator;
197 
198  //- Construct for a given UPtrList entry
199  inline iterator(T**);
200 
201  // Member operators
202 
203  inline bool operator==(const iterator&) const;
204  inline bool operator!=(const iterator&) const;
205 
206  inline T& operator*();
207  inline T& operator()();
208 
209  inline iterator operator++();
210  inline iterator operator++(const int);
211 
212  inline iterator operator--();
213  inline iterator operator--(const int);
214 
215  inline iterator operator+=(const label);
216  inline iterator operator-=(const label);
217 
218  inline iterator operator+(const label) const;
219  inline iterator operator-(const label) const;
220 
221  inline label operator-(const iterator&) const;
222 
223  inline T& operator[](const label);
224 
225  inline bool operator<(const iterator&) const;
226  inline bool operator>(const iterator&) const;
227 
228  inline bool operator<=(const iterator&) const;
229  inline bool operator>=(const iterator&) const;
230  };
231 
232  //- Return an iterator to begin traversing the UPtrList
233  inline iterator begin();
234 
235  //- Return an iterator to end traversing the UPtrList
236  inline iterator end();
237 
238 
239  // STL const_iterator
240  // Random access iterator for traversing UPtrList
241 
242  //- An STL-conforming const_iterator
243  class const_iterator
244  {
245  const T* const* ptr_;
246 
247  public:
248 
249  //- Construct for a given UPtrList entry
250  inline const_iterator(const T* const*);
251 
252  //- Construct from an iterator
253  inline const_iterator(const iterator&);
254 
255 
256  // Member operators
257 
258  inline bool operator==(const const_iterator&) const;
259  inline bool operator!=(const const_iterator&) const;
261  typedef const T& Tref;
262  inline Tref operator*();
263  inline Tref operator()();
264 
265  inline const_iterator operator++();
266  inline const_iterator operator++(const int);
267 
268  inline const_iterator operator--();
269  inline const_iterator operator--(const int);
270 
271  inline const_iterator operator+=(const label);
272  inline const_iterator operator-=(const label);
273 
274  inline const_iterator operator+(const label) const;
275  inline const_iterator operator-(const label) const;
276 
277  inline label operator-(const const_iterator&) const;
278 
279  inline const T& operator[](const label);
280 
281  inline bool operator<(const const_iterator&) const;
282  inline bool operator>(const const_iterator&) const;
283 
284  inline bool operator<=(const const_iterator&) const;
285  inline bool operator>=(const const_iterator&) const;
286  };
287 
288  //- Return an const_iterator to begin traversing the UPtrList
289  inline const_iterator cbegin() const;
290 
291  //- Return an const_iterator to end traversing the UPtrList
292  inline const_iterator cend() const;
293 
294  //- Return an const_iterator to begin traversing the UPtrList
295  inline const_iterator begin() const;
296 
297  //- Return an const_iterator to end traversing the UPtrList
298  inline const_iterator end() const;
299 
300 
301  // IOstream operator
302 
303  //- Write UPtrList to Ostream
304  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
305 };
306 
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 } // End namespace Foam
311 
312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
313 
314 #include "UPtrListI.H"
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 
318 #ifdef NoRepository
319  #include "UPtrList.C"
320 #endif
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 #endif
325 
326 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
iterator operator-=(const label)
Definition: UPtrListI.H:227
void resize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrListI.H:71
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
Xfer< UPtrList< T > > xfer()
Transfer contents to the Xfer container.
Definition: UPtrListI.H:94
iterator(T **)
Construct for a given UPtrList entry.
Definition: UPtrListI.H:144
T & reference
Type that can be used for storing into UPtrList::value_type objects.
Definition: UPtrList.H:174
iterator operator-(const label) const
Definition: UPtrListI.H:245
T & operator[](const label)
Definition: UPtrListI.H:263
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
UPtrList()
Null Constructor.
Definition: UPtrList.C:31
const T & operator[](const label) const
Return element const reference.
Definition: UPtrListI.H:103
bool empty() const
Return true if the UPtrList is empty (ie, size() is zero)
Definition: UPtrListI.H:36
iterator end()
Return an iterator to end traversing the UPtrList.
Definition: UPtrListI.H:307
bool operator>(const iterator &) const
Definition: UPtrListI.H:277
T & last()
Return reference to the last element of the list.
Definition: UPtrListI.H:57
An STL-conforming const_iterator.
Definition: UPtrList.H:242
friend class iterator
Definition: UPtrList.H:185
An STL iterator.
Definition: UPtrList.H:189
bool operator<(const iterator &) const
Definition: UPtrListI.H:270
void setSize(const label)
Reset size of UPtrList. This can only be used to set the size.
Definition: UPtrList.C:61
bool operator<=(const iterator &) const
Definition: UPtrListI.H:284
T value_type
Type of values the UPtrList contains.
Definition: UPtrList.H:171
const T & const_reference
Type that can be used for storing into constant UPtrList::value_type.
Definition: UPtrList.H:178
iterator operator+=(const label)
Definition: UPtrListI.H:218
Istream & operator>>(Istream &, directionInfo &)
iterator operator+(const label) const
Definition: UPtrListI.H:236
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
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:61
bool operator>=(const iterator &) const
Definition: UPtrListI.H:291
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool operator==(const iterator &) const
Definition: UPtrListI.H:151
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
iterator begin()
Return an iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:299
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:63
friend class const_iterator
Definition: UPtrList.H:195
void transfer(UPtrList< T > &)
Transfer the contents of the argument UPtrList into this.
Definition: UPtrList.C:94
bool operator!=(const iterator &) const
Definition: UPtrListI.H:158
const_iterator cend() const
Return an const_iterator to end traversing the UPtrList.
Definition: UPtrListI.H:520
void clear()
Clear the UPtrList, i.e. set size to zero.
Definition: UPtrList.C:87
const_iterator cbegin() const
Return an const_iterator to begin traversing the UPtrList.
Definition: UPtrListI.H:512
const T * operator()(const label) const
Return element const pointer.
Definition: UPtrListI.H:135
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: UPtrList.C:101
Namespace for OpenFOAM.
T & first()
Return reference to the first element of the list.
Definition: UPtrListI.H:43