FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 1D array of pointers to objects of type <T>, where the size of the
29  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 <OpenFOAM/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>
84 Istream& operator>>(Istream&, UPtrList<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 length 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 
130  // Edit
131 
132  //- Reset size of UPtrList. This can only be used to set the size
133  // of an empty UPtrList, extend a UPtrList, remove entries from
134  // the end of a UPtrList.
135  void setSize(const label);
136 
137  //- Reset size of UPtrList. This can only be used to set the size
138  // of an empty UPtrList, extend a UPtrList, remove entries from
139  // the end of a UPtrList.
140  inline void resize(const label);
141 
142  //- Clear the UPtrList, i.e. set size to zero
143  void clear();
144 
145  //- Transfer the contents of the argument UPtrList into this
146  // UPtrList and annull the argument list.
147  void transfer(UPtrList<T>&);
148 
149  //- Transfer contents to the Xfer container
150  inline Xfer<UPtrList<T> > xfer();
151 
152  //- Is element set
153  inline bool set(const label) const;
154 
155  //- Set element. Return old element (can be NULL).
156  // No checks on new element.
157  inline T* set(const label, T*);
158 
159  //- Reorders elements. Ordering does not have to be done in
160  // ascending or descending order. Reordering has to be unique.
161  // (is shuffle)
162  void reorder(const UList<label>&);
163 
164 
165  // Member operators
166 
167  //- Return element const reference.
168  inline const T& operator[](const label) const;
169 
170  //- Return element reference.
171  inline T& operator[](const label);
172 
173  //- Return element const pointer.
174  inline const T* operator()(const label) const;
175 
176 
177  // STL type definitions
178 
179  //- Type of values the UPtrList contains.
180  typedef T value_type;
181 
182  //- Type that can be used for storing into UPtrList::value_type objects.
183  typedef T& reference;
184 
185  //- Type that can be used for storing into constant UPtrList::value_type
186  // objects.
187  typedef const T& const_reference;
188 
189 
190  // STL iterator
191  // Random access iterator for traversing UPtrList.
192 
193  class iterator;
194  friend class iterator;
195 
196  //- An STL iterator
197  class iterator
198  {
199  T** ptr_;
200 
201  public:
202 
203  //- Construct for a given UPtrList entry
204  inline iterator(T**);
205 
206  // Member operators
207 
208  inline bool operator==(const iterator&) const;
209  inline bool operator!=(const iterator&) const;
210 
211  inline T& operator*();
212  inline T& operator()();
213 
214  inline iterator operator++();
215  inline iterator operator++(int);
216 
217  inline iterator operator--();
218  inline iterator operator--(int);
219 
220  inline iterator operator+=(label);
221 
222  friend iterator operator+ <T>(const iterator&, label);
223  friend iterator operator+ <T>(label, const iterator&);
224 
225  inline iterator operator-=(label);
226 
227  friend iterator operator- <T>(const iterator&, label);
228 
229  friend label operator- <T>
230  (
231  const iterator&,
232  const iterator&
233  );
234 
235  inline T& operator[](label);
236 
237  inline bool operator<(const iterator&) const;
238  inline bool operator>(const iterator&) const;
239 
240  inline bool operator<=(const iterator&) const;
241  inline bool operator>=(const iterator&) const;
242  };
243 
244  //- Return an iterator to begin traversing the UPtrList.
245  inline iterator begin();
246 
247  //- Return an iterator to end traversing the UPtrList.
248  inline iterator end();
249 
250 
251  // IOstream operator
252 
253  // Write List to Ostream.
254  friend Ostream& operator<< <T>(Ostream&, const UPtrList<T>&);
255 };
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 # include <OpenFOAM/UPtrListI.H>
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 #ifdef NoRepository
269 # include <OpenFOAM/UPtrList.C>
270 #endif
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #endif
275 
276 // ************************ vim: set sw=4 sts=4 et: ************************ //