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