FreeFOAM The Cross-Platform CFD Toolkit
LList.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::LList
26 
27 Description
28  Template class for non-intrusive linked lists.
29 
30 SourceFiles
31  LList.C
32  LListIO.C
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef LList_H
37 #define LList_H
38 
39 #include <OpenFOAM/label.H>
40 #include <OpenFOAM/uLabel.H>
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 
47 class Istream;
48 class Ostream;
49 
50 // Forward declaration of friend functions and operators
51 
52 template<class LListBase, class T> class LList;
53 
54 template<class LListBase, class T>
55 Istream& operator>>
56 (
57  Istream&,
58  LList<LListBase, T>&
59 );
60 
61 template<class LListBase, class T>
62 Ostream& operator<<
63 (
64  Ostream&,
65  const LList<LListBase, T>&
66 );
67 
68 
69 /*---------------------------------------------------------------------------*\
70  Class LList Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 template<class LListBase, class T>
74 class LList
75 :
76  public LListBase
77 {
78 
79 public:
80 
81  // Forward declaration of STL iterators
82 
83  class iterator;
84  friend class iterator;
85 
87  friend class const_iterator;
88 
89 
90  //- Link structure
91  struct link
92  :
93  public LListBase::link
94  {
95  //- Stored object
97 
98  //- Construct given object
99  link(T a)
100  :
101  obj_(a)
102  {}
103  };
104 
105 
106  // Constructors
107 
108  //- Null construct
110  {}
111 
112  //- Construct given initial T
113  LList(T a)
114  :
115  LListBase(new link(a))
116  {}
117 
118  //- Construct from Istream
119  LList(Istream&);
120 
121  //- Construct as copy
122  LList(const LList<LListBase, T>&);
123 
124 
125  // Destructor
126 
127  ~LList();
128 
129 
130  // Member Functions
131 
132  // Access
133 
134  //- Return the first entry added
135  T& first()
136  {
137  return static_cast<link*>(LListBase::first())->obj_;
138  }
139 
140  //- Return const access to the first entry added
141  const T& first() const
142  {
143  return static_cast<const link*>(LListBase::first())->obj_;
144  }
145 
146  //- Return the last entry added
147  T& last()
148  {
149  return static_cast<link*>(LListBase::last())->obj_;
150  }
151 
152  //- Return const access to the last entry added
153  const T& last() const
154  {
155  return static_cast<const link*>(LListBase::last())->obj_;
156  }
157 
158 
159  // Edit
160 
161  //- Add at head of list
162  void insert(const T& a)
163  {
164  LListBase::insert(new link(a));
165  }
166 
167  //- Add at tail of list
168  void append(const T& a)
169  {
170  LListBase::append(new link(a));
171  }
172 
173  //- Remove and return head
175  {
176  link* elmtPtr = static_cast<link*>(LListBase::removeHead());
177  T data = elmtPtr->obj_;
178  delete elmtPtr;
179  return data;
180  }
181 
182  //- Remove and return element
183  T remove(link* l)
184  {
185  link* elmtPtr = static_cast<link*>(LListBase::remove(l));
186  T data = elmtPtr->obj_;
187  delete elmtPtr;
188  return data;
189  }
190 
191  //- Remove and return element specified by iterator
192  T remove(iterator& it)
193  {
194  link* elmtPtr = static_cast<link*>(LListBase::remove(it));
195  T data = elmtPtr->obj_;
196  delete elmtPtr;
197  return data;
198  }
199 
200  //- Delete contents of list
201  void clear();
202 
203  //- Transfer the contents of the argument into this List
204  // and annull the argument list.
206 
207  // Member operators
208 
209  void operator=(const LList<LListBase, T>&);
210 
211 
212  // STL type definitions
213 
214  //- Type of values the LList contains.
215  typedef T value_type;
216 
217  //- Type that can be used for storing into value_type
218  // objects.
219  typedef T& reference;
220 
221  //- Type that can be used for storing into constant
222  // LList::value_type objects.
223  typedef const T& const_reference;
224 
225  //- The type that can represent the size of a LList.
226  typedef label size_type;
227 
228 
229  // STL iterator
230 
231  typedef typename LListBase::iterator LListBase_iterator;
232 
233  //- An STL-conforming iterator
234  class iterator
235  :
236  public LListBase_iterator
237  {
238 
239  public:
240 
241  //- Construct from base iterator
242  iterator
243  (
244  LListBase_iterator baseIter
245  )
246  :
247  LListBase_iterator(baseIter)
248  {}
249 
250 
251  // Member operators
252 
254  {
255  return
256  static_cast<link&>
258  }
259 
261  {
262  return operator*();
263  }
264 
266  {
268  return *this;
269  }
270  };
271 
272 
273  // STL const_iterator
274 
275  typedef typename LListBase::const_iterator LListBase_const_iterator;
276 
277  //- An STL-conforming const_iterator
279  :
281  {
282 
283  public:
284 
285  //- Construct from base const_iterator
287  (
288  LListBase_const_iterator baseIter
289  )
290  :
291  LListBase_const_iterator(baseIter)
292  {}
293 
294 
295  //- Construct from base iterator
297  (
298  LListBase_iterator baseIter
299  )
300  :
301  LListBase_const_iterator(baseIter)
302  {}
303 
304 
305  // Member operators
306 
307  const T& operator*()
308  {
309  return
310  static_cast<const link&>
312  }
313 
314  const T& operator()()
315  {
316  return operator*();
317  }
318 
320  {
322  return *this;
323  }
324  };
325 
326 
327  // IOstream operators
328 
329  friend Istream& operator>> <LListBase, T>
330  (
331  Istream&,
333  );
334 
335  friend Ostream& operator<< <LListBase, T>
336  (
337  Ostream&,
338  const LList<LListBase, T>&
339  );
340 };
341 
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
345 } // End namespace Foam
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #ifdef NoRepository
350 # include "LList.C"
351 #endif
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 #endif
356 
357 // ************************ vim: set sw=4 sts=4 et: ************************ //