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