FreeFOAM The Cross-Platform CFD Toolkit
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) 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::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
32  rather than to the pointer.
33 
34 SourceFiles
35  PtrList.C
36  PtrListIO.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef PtrList_H
41 #define PtrList_H
42 
43 #include <OpenFOAM/List.H>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 // Forward declaration of friend functions and operators
51 
52 template<class T> class PtrList;
53 template<class T> class SLPtrList;
54 
55 template<class T>
56 inline typename PtrList<T>::iterator operator+
57 (
58  const typename PtrList<T>::iterator&,
59  label
60 );
61 
62 template<class T>
63 inline typename PtrList<T>::iterator operator+
64 (
65  label,
66  const typename PtrList<T>::iterator&
67 );
68 
69 template<class T>
70 inline typename PtrList<T>::iterator operator-
71 (
72  const typename PtrList<T>::iterator&,
73  label
74 );
75 
76 template<class T>
77 inline label operator-
78 (
79  const typename PtrList<T>::iterator&,
80  const typename PtrList<T>::iterator&
81 );
82 
83 template<class T>
84 Istream& operator>>(Istream&, PtrList<T>&);
85 
86 template<class T>
87 Ostream& operator<<(Ostream&, const PtrList<T>&);
88 
89 template<class T> class autoPtr;
90 template<class T> class tmp;
91 
92 
93 /*---------------------------------------------------------------------------*\
94  Class PtrList Declaration
95 \*---------------------------------------------------------------------------*/
96 
97 template<class T>
98 class PtrList
99 {
100  // Private data
101 
102  List<T*> ptrs_;
103 
104 
105 protected:
106 
107  // Protected member functions
108 
109  //- Read from Istream using given Istream constructor class
110  template<class INew>
111  void read(Istream&, const INew& inewt);
112 
113 
114 public:
115 
116  // Constructors
117 
118  //- Null Constructor.
119  PtrList();
120 
121  //- Construct with length specified.
122  explicit PtrList(const label);
123 
124  //- Copy constructor.
125  PtrList(const PtrList<T>&);
126 
127  //- Copy constructor with additional argument for clone
128  template<class CloneArg>
129  PtrList(const PtrList<T>&, const CloneArg&);
130 
131  //- Construct by transferring the parameter contents
132  PtrList(const Xfer<PtrList<T> >&);
133 
134  //- Construct as copy or re-use as specified.
135  PtrList(PtrList<T>&, bool reUse);
136 
137  //- Construct as copy of SLPtrList<T>
138  PtrList(const SLPtrList<T>&);
139 
140  //- Construct from Istream using given Istream constructor class
141  template<class INew>
142  PtrList(Istream&, const INew&);
143 
144  //- Construct from Istream using default Istream constructor class
145  PtrList(Istream&);
146 
147 
148  // Destructor
149 
150  ~PtrList();
151 
152 
153  // Member functions
154 
155  // Access
156 
157  //- Return the number of elements in the PtrList
158  inline label size() const;
159 
160  //- Return true if the PtrList is empty (ie, size() is zero).
161  inline bool empty() const;
162 
163 
164  // Edit
165 
166  //- Reset size of PtrList. This can only be used to set the size
167  // of an empty PtrList, extend a PtrList, remove entries from
168  // the end of a PtrList. If the entries are non-empty they are
169  // deleted.
170  void setSize(const label);
171 
172  //- Reset size of PtrList. This can only be used to set the size
173  // of an empty PtrList, extend a PtrList, remove entries from
174  // the end of a PtrList. If the entries are non-empty they are
175  // deleted.
176  inline void resize(const label);
177 
178  //- Clear the PtrList, i.e. set size to zero deleting all the
179  // allocated entries.
180  void clear();
181 
182  //- Transfer the contents of the argument PtrList into this PtrList
183  // and annull the argument list.
184  void transfer(PtrList<T>&);
185 
186  //- Transfer contents to the Xfer container
187  inline Xfer<PtrList<T> > xfer();
188 
189  //- Is element set
190  inline bool set(const label) const;
191 
192  //- Set element. Return old element (can be NULL).
193  // No checks on new element.
194  inline autoPtr<T> set(const label, T*);
195  inline autoPtr<T> set(const label, const autoPtr<T>&);
196  inline autoPtr<T> set(const label, const tmp<T>&);
197 
198  //- Reorders elements. Ordering does not have to be done in
199  // ascending or descending order. Reordering has to be unique.
200  // (is shuffle)
201  void reorder(const UList<label>&);
202 
203 
204  // Member operators
205 
206  //- Return element const reference.
207  inline const T& operator[](const label) const;
208 
209  //- Return element reference.
210  inline T& operator[](const label);
211 
212  //- Return element const pointer.
213  inline const T* operator()(const label) const;
214 
215 
216  //- Assignment.
218 
219 
220  // STL type definitions
221 
222  //- Type of values the PtrList contains.
223  typedef T value_type;
224 
225  //- Type that can be used for storing into PtrList::value_type objects.
226  typedef T& reference;
227 
228  //- Type that can be used for storing into constant PtrList::value_type
229  // objects.
230  typedef const T& const_reference;
231 
232 
233  // STL iterator
234  // Random access iterator for traversing PtrList.
235 
236  class iterator;
237  friend class iterator;
238 
239  //- An STL-conforming iterator
240  class iterator
241  {
242  T** ptr_;
243 
244  public:
245 
246  //- Construct for a given PtrList entry
247  inline iterator(T**);
248 
249  // Member operators
250 
251  inline bool operator==(const iterator&) const;
252  inline bool operator!=(const iterator&) const;
253 
254  typedef T& Tref;
255  inline Tref operator*();
256  inline Tref operator()();
257 
258  inline iterator operator++();
259  inline iterator operator++(int);
260 
261  inline iterator operator--();
262  inline iterator operator--(int);
263 
264  inline iterator operator+=(label);
265 
266  friend iterator operator+ <T>(const iterator&, label);
267  friend iterator operator+ <T>(label, const iterator&);
268 
269  inline iterator operator-=(label);
270 
271  friend iterator operator- <T>(const iterator&, label);
272 
273  friend label operator- <T>
274  (
275  const iterator&,
276  const iterator&
277  );
278 
279  inline T& operator[](label);
280 
281  inline bool operator<(const iterator&) const;
282  inline bool operator>(const iterator&) const;
283 
284  inline bool operator<=(const iterator&) const;
285  inline bool operator>=(const iterator&) const;
286  };
287 
288  //- Return an iterator to begin traversing the PtrList.
289  inline iterator begin();
290 
291  //- Return an iterator to end traversing the PtrList.
292  inline iterator end();
293 
294 
295  // IOstream operator
296 
297  //- Read List from Istream, discarding contents of existing List.
298  friend Istream& operator>> <T>(Istream&, PtrList<T>&);
299 
300  // Write List to Ostream.
301  friend Ostream& operator<< <T>(Ostream&, const PtrList<T>&);
302 };
303 
304 
305 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
306 
307 } // End namespace Foam
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 # include <OpenFOAM/PtrListI.H>
312 
313 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
314 
315 #ifdef NoRepository
316 # include <OpenFOAM/PtrList.C>
317 #endif
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 #endif
322 
323 // ************************ vim: set sw=4 sts=4 et: ************************ //