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>&);
117 
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:
287 
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;
297 
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;
360 
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 
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 
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 // ************************************************************************* //
Foam::PtrList::value_type
T value_type
Type of values the PtrList contains.
Definition: PtrList.H:262
Foam::PtrList::const_iterator::operator+=
const_iterator operator+=(label)
Definition: PtrListI.H:464
Foam::PtrList::const_iterator::operator>=
bool operator>=(const const_iterator &) const
Definition: PtrListI.H:557
Foam::PtrList::last
T & last()
Return reference to the last element of the list.
Definition: PtrListI.H:60
List.H
Foam::PtrList::iterator::operator<
bool operator<(const iterator &) const
Definition: PtrListI.H:333
Foam::PtrList::iterator::operator++
iterator operator++()
Definition: PtrListI.H:233
Foam::PtrList::append
void append(T *)
Append an element at the end of the list.
Foam::PtrList::iterator::operator*
Tref operator*()
Definition: PtrListI.H:218
Foam::PtrList::iterator::Tref
T & Tref
Definition: PtrList.H:297
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:118
Foam::PtrList::begin
iterator begin()
Return an iterator to begin traversing the PtrList.
Definition: PtrListI.H:361
Foam::PtrList::PtrList
PtrList()
Null Constructor.
Foam::PtrList::~PtrList
~PtrList()
Destructor.
Definition: PtrList.C:127
Foam::PtrList::xfer
Xfer< PtrList< T > > xfer()
Transfer contents to the Xfer container.
Definition: PtrListI.H:145
Foam::PtrList::const_iterator::const_iterator
const_iterator(const T *const *)
Construct for a given PtrList entry.
Foam::PtrList::iterator::iterator
iterator(T **)
Construct for a given PtrList entry.
Definition: PtrListI.H:197
Foam::PtrList::const_iterator::operator<=
bool operator<=(const const_iterator &) const
Definition: PtrListI.H:547
Foam::PtrList::iterator::operator[]
T & operator[](label)
Definition: PtrListI.H:326
Foam::PtrList::const_iterator::operator--
const_iterator operator--()
Definition: PtrListI.H:445
Foam::PtrList::const_iterator::operator()
Tref operator()()
Definition: PtrListI.H:418
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
Foam::PtrList::iterator
An STL-conforming iterator.
Definition: PtrList.H:281
Foam::PtrList::operator()
const T * operator()(const label) const
Return element const pointer.
Definition: PtrListI.H:188
Foam::PtrList::end
iterator end()
Return an iterator to end traversing the PtrList.
Definition: PtrListI.H:368
Foam::PtrList::iterator::operator<=
bool operator<=(const iterator &) const
Definition: PtrListI.H:347
Foam::PtrList::const_iterator
An STL-conforming const_iterator.
Definition: PtrList.H:342
Foam::INew
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:49
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::SLPtrList
Non-intrusive singly-linked pointer list.
Definition: SLPtrList.H:47
Foam::label
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
Foam::PtrList::const_iterator::Tref
const typedef T & Tref
Definition: PtrList.H:360
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
PtrList.C
Foam::PtrList::empty
bool empty() const
Return true if the PtrList is empty (ie, size() is zero).
Definition: PtrListI.H:39
Foam::PtrList::iterator::ptr_
T ** ptr_
Definition: PtrList.H:283
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::PtrList::cend
const_iterator cend() const
Return an const_iterator to end traversing the PtrList.
Definition: PtrListI.H:591
Foam::PtrList::operator[]
const T & operator[](const label) const
Return element const reference.
Foam::PtrList::iterator::operator!=
bool operator!=(const iterator &) const
Definition: PtrListI.H:211
Foam::PtrList
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
Foam::PtrList::ptrs_
List< T * > ptrs_
Definition: PtrList.H:130
Foam::PtrList::transfer
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
Foam::PtrList::iterator::operator>
bool operator>(const iterator &) const
Definition: PtrListI.H:340
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::PtrList::reorder
void reorder(const labelUList &)
Reorders elements. Ordering does not have to be done in.
Definition: PtrList.C:208
Foam::PtrList::const_iterator::operator<
bool operator<(const const_iterator &) const
Definition: PtrListI.H:527
Foam::PtrList::reference
T & reference
Type that can be used for storing into PtrList::value_type objects.
Definition: PtrList.H:265
Foam::PtrList::clear
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::PtrList::iterator::operator-=
iterator operator-=(label)
Definition: PtrListI.H:298
Foam::PtrList::const_iterator::operator-=
const_iterator operator-=(label)
Definition: PtrListI.H:491
Foam::PtrList::cbegin
const_iterator cbegin() const
Return an const_iterator to begin traversing the PtrList.
Definition: PtrListI.H:583
Foam::PtrList::iterator::operator+=
iterator operator+=(label)
Definition: PtrListI.H:271
Foam::PtrList::const_iterator::operator==
bool operator==(const const_iterator &) const
Definition: PtrListI.H:392
Foam::PtrList::resize
void resize(const label)
Alias for setSize(const label)
Definition: PtrListI.H:74
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::PtrList::first
T & first()
Return reference to the first element of the list.
Definition: PtrListI.H:46
Foam::PtrList::iterator::operator>=
bool operator>=(const iterator &) const
Definition: PtrListI.H:354
Foam::PtrList::iterator::operator()
Tref operator()()
Definition: PtrListI.H:225
Foam::PtrList::const_iterator::operator[]
const T & operator[](label)
Definition: PtrListI.H:519
Foam::List< T * >
Foam::PtrList< T >::iterator
friend class iterator
Definition: PtrList.H:276
Foam::PtrList::const_iterator::ptr_
const T *const * ptr_
Definition: PtrList.H:344
Foam::PtrList::const_iterator::operator*
Tref operator*()
Definition: PtrListI.H:411
Foam::PtrList::read
void read(Istream &, const INew &inewt)
Read from Istream using given Istream constructor class.
Definition: PtrListIO.C:36
Foam::PtrList::const_iterator::operator++
const_iterator operator++()
Definition: PtrListI.H:426
Foam::PtrList::const_iterator::operator>
bool operator>(const const_iterator &) const
Definition: PtrListI.H:537
Foam::UList
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
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::PtrList::const_reference
const typedef T & const_reference
Type that can be used for storing into constant PtrList::value_type.
Definition: PtrList.H:269
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::PtrList::const_iterator::operator!=
bool operator!=(const const_iterator &) const
Definition: PtrListI.H:402
Foam::PtrList::iterator::operator==
bool operator==(const iterator &) const
Definition: PtrListI.H:204
PtrListI.H
Foam::PtrList::iterator::operator--
iterator operator--()
Definition: PtrListI.H:252
Foam::PtrList< T >::const_iterator
friend class const_iterator
Definition: PtrList.H:278
Foam::PtrList::operator=
PtrList< T > & operator=(const PtrList< T > &)
Assignment.
Definition: PtrList.C:261