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