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 | Copyright (C) 2011-2015 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::PtrList
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.
33 
34 SourceFiles
35  PtrListI.H
36  PtrList.C
37  PtrListIO.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef PtrList_H
42 #define PtrList_H
43 
44 #include "List.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Forward declaration of friend functions and operators
52 
53 template<class T> class PtrList;
54 template<class T> class SLPtrList;
55 
56 template<class T>
57 inline typename PtrList<T>::iterator operator+
58 (
59  const typename PtrList<T>::iterator&,
60  label
61 );
62 
63 template<class T>
64 inline typename PtrList<T>::iterator operator+
65 (
66  label,
67  const typename PtrList<T>::iterator&
68 );
69 
70 template<class T>
71 inline typename PtrList<T>::iterator operator-
72 (
73  const typename PtrList<T>::iterator&,
74  label
75 );
76 
77 template<class T>
78 inline label operator-
79 (
80  const typename PtrList<T>::iterator&,
81  const typename PtrList<T>::iterator&
82 );
83 
84 template<class T>
85 inline typename PtrList<T>::const_iterator operator+
86 (
87  const typename PtrList<T>::const_iterator&,
88  label
89 );
90 
91 template<class T>
92 inline typename PtrList<T>::const_iterator operator+
93 (
94  label,
95  const typename PtrList<T>::const_iterator&
96 );
97 
98 template<class T>
99 inline typename PtrList<T>::const_iterator operator-
100 (
101  const typename PtrList<T>::const_iterator&,
102  label
103 );
104 
105 template<class T>
106 inline label operator-
107 (
108  const typename PtrList<T>::const_iterator&,
109  const typename PtrList<T>::const_iterator&
110 );
111 
112 template<class T>
113 Istream& operator>>(Istream&, PtrList<T>&);
114 
115 template<class T>
116 Ostream& operator<<(Ostream&, const PtrList<T>&);
118 template<class T> class autoPtr;
119 template<class T> class tmp;
120 
121 
122 /*---------------------------------------------------------------------------*\
123  Class PtrList Declaration
124 \*---------------------------------------------------------------------------*/
125 
126 template<class T>
127 class PtrList
128 {
129  // Private data
130 
131  List<T*> ptrs_;
132 
133 
134 protected:
135 
136  // Protected Member Functions
137 
138  //- Read from Istream using given Istream constructor class
139  template<class INew>
140  void read(Istream&, const INew& inewt);
141 
142 
143 public:
144 
145  // Constructors
146 
147  //- Null Constructor.
148  PtrList();
149 
150  //- Construct with size specified.
151  explicit PtrList(const label);
152 
153  //- Copy constructor.
154  PtrList(const PtrList<T>&);
155 
156  //- Copy constructor with additional argument for clone
157  template<class CloneArg>
158  PtrList(const PtrList<T>&, const CloneArg&);
159 
160  //- Construct by transferring the parameter contents
161  PtrList(const Xfer<PtrList<T> >&);
162 
163  //- Construct as copy or re-use as specified.
164  PtrList(PtrList<T>&, bool reUse);
165 
166  //- Construct as copy of SLPtrList<T>
167  explicit PtrList(const SLPtrList<T>&);
168 
169  //- Construct from Istream using given Istream constructor class
170  template<class INew>
171  PtrList(Istream&, const INew&);
172 
173  //- Construct from Istream using default Istream constructor class
174  PtrList(Istream&);
175 
176 
177  //- Destructor
178  ~PtrList();
179 
180 
181  // Member functions
182 
183  // Access
184 
185  //- Return the number of elements in the PtrList
186  inline label size() const;
187 
188  //- Return true if the PtrList is empty (ie, size() is zero).
189  inline bool empty() const;
190 
191  //- Return reference to the first element of the list.
192  inline T& first();
193 
194  //- Return reference to first element of the list.
195  inline const T& first() const;
196 
197  //- Return reference to the last element of the list.
198  inline T& last();
199 
200  //- Return reference to the last element of the list.
201  inline const T& last() const;
202 
203  // Edit
204 
205  //- Reset size of PtrList. If extending the PtrList, new entries are
206  // set to NULL. If truncating the PtrList, removed entries are
207  // deleted.
208  void setSize(const label);
209 
210  //- Alias for setSize(const label)
211  inline void resize(const label);
212 
213  //- Clear the PtrList, i.e. set size to zero deleting all the
214  // allocated entries.
215  void clear();
216 
217  //- Append an element at the end of the list
218  inline void append(T*);
219  inline void append(const autoPtr<T>&);
220  inline void append(const tmp<T>&);
221 
222  //- Transfer the contents of the argument PtrList into this PtrList
223  // and annul the argument list.
224  void transfer(PtrList<T>&);
225 
226  //- Transfer contents to the Xfer container
227  inline Xfer<PtrList<T> > xfer();
228 
229  //- Is element set
230  inline bool set(const label) const;
231 
232  //- Set element. Return old element (can be NULL).
233  // No checks on new element.
234  inline autoPtr<T> set(const label, T*);
235  inline autoPtr<T> set(const label, const autoPtr<T>&);
236  inline autoPtr<T> set(const label, const tmp<T>&);
237 
238  //- Reorders elements. Ordering does not have to be done in
239  // ascending or descending order. Reordering has to be unique.
240  // (is shuffle)
241  void reorder(const labelUList&);
242 
243 
244  // Member operators
245 
246  //- Return element const reference.
247  inline const T& operator[](const label) const;
248 
249  //- Return element reference.
250  inline T& operator[](const label);
251 
252  //- Return element const pointer.
253  inline const T* operator()(const label) const;
254 
255 
256  //- Assignment.
258 
259 
260  // STL type definitions
261 
262  //- Type of values the PtrList contains.
263  typedef T value_type;
264 
265  //- Type that can be used for storing into PtrList::value_type objects.
266  typedef T& reference;
267 
268  //- Type that can be used for storing into constant PtrList::value_type
269  // objects.
270  typedef const T& const_reference;
271 
272 
273  // STL iterator
274  // Random access iterator for traversing PtrList.
275 
276  class iterator;
277  class const_iterator;
278  friend class iterator;
279  friend class const_iterator;
280 
281  //- An STL-conforming iterator
282  class iterator
283  {
284  T** ptr_;
285 
286  public:
288  friend class const_iterator;
289 
290  //- Construct for a given PtrList entry
291  inline iterator(T**);
292 
293  // Member operators
294 
295  inline bool operator==(const iterator&) const;
296  inline bool operator!=(const iterator&) const;
298  typedef T& Tref;
299  inline Tref operator*();
300  inline Tref operator()();
301 
302  inline iterator operator++();
303  inline iterator operator++(int);
304 
305  inline iterator operator--();
306  inline iterator operator--(int);
307 
308  inline iterator operator+=(label);
309 
310  friend iterator operator+ <T>(const iterator&, label);
311  friend iterator operator+ <T>(label, const iterator&);
312 
313  inline iterator operator-=(label);
314 
315  friend iterator operator- <T>(const iterator&, label);
316 
317  friend label operator- <T>
318  (
319  const iterator&,
320  const iterator&
321  );
322 
323  inline T& operator[](label);
324 
325  inline bool operator<(const iterator&) const;
326  inline bool operator>(const iterator&) const;
327 
328  inline bool operator<=(const iterator&) const;
329  inline bool operator>=(const iterator&) const;
330  };
331 
332  //- Return an iterator to begin traversing the PtrList.
333  inline iterator begin();
334 
335  //- Return an iterator to end traversing the PtrList.
336  inline iterator end();
337 
338 
339  // STL const_iterator
340  // Random access iterator for traversing PtrList.
341 
342  //- An STL-conforming const_iterator
343  class const_iterator
344  {
345  const T* const* ptr_;
346 
347  public:
348 
349  //- Construct for a given PtrList entry
350  inline const_iterator(const T* const*);
351 
352  //- Construct from an iterator
353  inline const_iterator(const iterator&);
354 
355 
356  // Member operators
357 
358  inline bool operator==(const const_iterator&) const;
359  inline bool operator!=(const const_iterator&) const;
361  typedef const T& Tref;
362  inline Tref operator*();
363  inline Tref operator()();
364 
365  inline const_iterator operator++();
366  inline const_iterator operator++(int);
367 
368  inline const_iterator operator--();
369  inline const_iterator operator--(int);
370 
371  inline const_iterator operator+=(label);
372 
373  friend const_iterator operator+ <T>
374  (
375  const const_iterator&,
376  label
377  );
378  friend const_iterator operator+ <T>
379  (
380  label,
381  const const_iterator&
382  );
383 
384  inline const_iterator operator-=(label);
385 
386  friend const_iterator operator- <T>
387  (
388  const const_iterator&,
389  label
390  );
391 
392  friend label operator- <T>
393  (
394  const const_iterator&,
395  const const_iterator&
396  );
397 
398  inline const T& operator[](label);
399 
400  inline bool operator<(const const_iterator&) const;
401  inline bool operator>(const const_iterator&) const;
402 
403  inline bool operator<=(const const_iterator&) const;
404  inline bool operator>=(const const_iterator&) const;
405  };
406 
407  //- Return an const_iterator to begin traversing the PtrList.
408  inline const_iterator cbegin() const;
409 
410  //- Return an const_iterator to end traversing the PtrList.
411  inline const_iterator cend() const;
412 
413  //- Return an const_iterator to begin traversing the PtrList.
414  inline const_iterator begin() const;
415 
416  //- Return an const_iterator to end traversing the PtrList.
417  inline const_iterator end() const;
418 
419 
420  // IOstream operator
421 
422  //- Read List from Istream, discarding contents of existing List.
423  friend Istream& operator>> <T>(Istream&, PtrList<T>&);
424 
425  // Write List to Ostream.
426  friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
427 };
428 
429 
430 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
431 
432 } // End namespace Foam
433 
434 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
435 
436 # include "PtrListI.H"
437 
438 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
439 
440 #ifdef NoRepository
441 # include "PtrList.C"
442 #endif
443 
444 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
445 
446 #endif
447 
448 // ************************************************************************* //
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
bool empty() const
Return true if the PtrList is empty (ie, size() is zero).
Definition: PtrListI.H:39
friend class const_iterator
Definition: PtrList.H:278
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
bool operator<(const iterator &) const
Definition: PtrListI.H:333
T & reference
Type that can be used for storing into PtrList::value_type objects.
Definition: PtrList.H:265
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
T & last()
Return reference to the last element of the list.
Definition: PtrListI.H:60
bool operator>=(const iterator &) const
Definition: PtrListI.H:354
Xfer< PtrList< T > > xfer()
Transfer contents to the Xfer container.
Definition: PtrListI.H:145
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
iterator operator+=(label)
Definition: PtrListI.H:271
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
~PtrList()
Destructor.
Definition: PtrList.C:127
const_iterator cbegin() const
Return an const_iterator to begin traversing the PtrList.
Definition: PtrListI.H:583
const T * operator()(const label) const
Return element const pointer.
Definition: PtrListI.H:188
An STL-conforming iterator.
Definition: PtrList.H:281
bool operator!=(const iterator &) const
Definition: PtrListI.H:211
bool operator>(const iterator &) const
Definition: PtrListI.H:340
iterator end()
Return an iterator to end traversing the PtrList.
Definition: PtrListI.H:368
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
void resize(const label)
Alias for setSize(const label)
Definition: PtrListI.H:74
bool operator==(const iterator &) const
Definition: PtrListI.H:204
Namespace for OpenFOAM.
PtrList()
Null Constructor.
Definition: PtrList.C:34
iterator operator--()
Definition: PtrListI.H:252
void read(Istream &, const INew &inewt)
Read from Istream using given Istream constructor class.
Definition: PtrListIO.C:36
friend class iterator
Definition: PtrList.H:276
An STL-conforming const_iterator.
Definition: PtrList.H:342
iterator(T **)
Construct for a given PtrList entry.
Definition: PtrListI.H:197
iterator operator++()
Definition: PtrListI.H:233
friend class const_iterator
Definition: PtrList.H:287
const_iterator cend() const
Return an const_iterator to end traversing the PtrList.
Definition: PtrListI.H:591
T & first()
Return reference to the first element of the list.
Definition: PtrListI.H:46
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
iterator begin()
Return an iterator to begin traversing the PtrList.
Definition: PtrListI.H:361
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:144
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void append(T *)
Append an element at the end of the list.
Definition: PtrListI.H:81
PtrList< T > & operator=(const PtrList< T > &)
Assignment.
Definition: PtrList.C:261
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:208
T & operator[](label)
Definition: PtrListI.H:326
bool operator<=(const iterator &) const
Definition: PtrListI.H:347
iterator operator-=(label)
Definition: PtrListI.H:298
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
A class for managing temporary objects.
Definition: PtrList.H:118
const T & const_reference
Type that can be used for storing into constant PtrList::value_type.
Definition: PtrList.H:269
Non-intrusive singly-linked pointer list.
Definition: SLPtrList.H:47
const T & operator[](const label) const
Return element const reference.
Definition: PtrListI.H:154
T value_type
Type of values the PtrList contains.
Definition: PtrList.H:262