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) 2011-2015 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::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 "label.H"
40 #include "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&,
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 
86  class const_iterator;
87  friend class const_iterator;
88 
89 
90  //- Link structure
91  struct link
92  :
93  public LListBase::link
94  {
95  //- Stored object
96  T obj_;
97 
98  //- Construct given object
99  link(T a)
100  :
101  obj_(a)
102  {}
103  };
104 
105 
106  // Constructors
107 
108  //- Null construct
109  LList()
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  ~LList();
127 
128 
129  // Member Functions
130 
131  // Access
132 
133  //- Return the first entry added
134  T& first()
135  {
136  return static_cast<link*>(LListBase::first())->obj_;
137  }
138 
139  //- Return const access to the first entry added
140  const T& first() const
141  {
142  return static_cast<const link*>(LListBase::first())->obj_;
143  }
144 
145  //- Return the last entry added
146  T& last()
147  {
148  return static_cast<link*>(LListBase::last())->obj_;
149  }
150 
151  //- Return const access to the last entry added
152  const T& last() const
153  {
154  return static_cast<const link*>(LListBase::last())->obj_;
155  }
156 
157 
158  // Edit
159 
160  //- Add at head of list
161  void insert(const T& a)
162  {
163  LListBase::insert(new link(a));
164  }
165 
166  //- Add at tail of list
167  void append(const T& a)
168  {
169  LListBase::append(new link(a));
170  }
171 
172  //- Remove and return head
173  T removeHead()
174  {
175  link* elmtPtr = static_cast<link*>(LListBase::removeHead());
176  T data = elmtPtr->obj_;
177  delete elmtPtr;
178  return data;
179  }
180 
181  //- Remove and return element
182  T remove(link* l)
183  {
184  link* elmtPtr = static_cast<link*>(LListBase::remove(l));
185  T data = elmtPtr->obj_;
186  delete elmtPtr;
187  return data;
188  }
189 
190  //- Remove and return element specified by iterator
191  T remove(iterator& it)
192  {
193  link* elmtPtr = static_cast<link*>(LListBase::remove(it));
194  T data = elmtPtr->obj_;
195  delete elmtPtr;
196  return data;
197  }
198 
199  //- Delete contents of list
200  void clear();
201 
202  //- Transfer the contents of the argument into this List
203  // and annul the argument list.
205 
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
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(LListBase_iterator baseIter)
243  :
244  LListBase_iterator(baseIter)
245  {}
246 
247 
248  // Member operators
250  T& operator*()
251  {
252  return
253  static_cast<link&>
255  }
257  T& operator()()
258  {
259  return operator*();
260  }
262  iterator& operator++()
263  {
264  LListBase_iterator::operator++();
265  return *this;
266  }
267  };
269  inline iterator begin()
270  {
271  return LListBase::begin();
272  }
274  inline const iterator& end()
275  {
276  return static_cast<const iterator&>(LListBase::end());
277  }
278 
279 
280  // STL const_iterator
282  typedef typename LListBase::const_iterator LListBase_const_iterator;
283 
284  //- An STL-conforming const_iterator
285  class const_iterator
286  :
287  public LListBase_const_iterator
288  {
289 
290  public:
291 
292  //- Construct from base const_iterator
293  const_iterator(LListBase_const_iterator baseIter)
294  :
295  LListBase_const_iterator(baseIter)
296  {}
297 
298 
299  //- Construct from base iterator
300  const_iterator(LListBase_iterator baseIter)
301  :
302  LListBase_const_iterator(baseIter)
303  {}
304 
305 
306  // Member operators
308  const T& operator*()
309  {
310  return
311  static_cast<const link&>
313  }
315  const T& operator()()
316  {
317  return operator*();
318  }
320  const_iterator& operator++()
321  {
322  LListBase_const_iterator::operator++();
323  return *this;
324  }
325  };
327  inline const_iterator cbegin() const
328  {
329  return LListBase::cbegin();
330  }
332  inline const const_iterator& cend() const
333  {
334  return static_cast<const const_iterator&>(LListBase::cend());
335  }
337  inline const_iterator begin() const
338  {
339  return LListBase::begin();
340  }
342  inline const const_iterator& end() const
343  {
344  return static_cast<const const_iterator&>(LListBase::end());
345  }
346 
347 
348  // IOstream operators
349 
350  friend Istream& operator>> <LListBase, T>
351  (
352  Istream&,
354  );
355 
356  friend Ostream& operator<< <LListBase, T>
357  (
358  Ostream&,
359  const LList<LListBase, T>&
360  );
361 };
362 
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 } // End namespace Foam
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 #ifdef NoRepository
371 # include "LList.C"
372 #endif
373 
374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 
376 #endif
377 
378 // ************************************************************************* //
tmp< fvMatrix< Type > > operator*(const DimensionedField< scalar, volMesh > &, const fvMatrix< Type > &)
void insert(const T &a)
Add at head of list.
Definition: LList.H:160
const iterator & end()
Definition: LList.H:273
friend class iterator
Definition: LList.H:82
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
Link structure.
Definition: DLListBase.H:56
const_iterator cbegin() const
Definition: LList.H:326
const T & const_reference
Type that can be used for storing into constant.
Definition: LList.H:222
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
T & last()
Return the last entry added.
Definition: LList.H:145
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
LListBase::iterator LListBase_iterator
Definition: LList.H:230
An STL-conforming const_iterator.
Definition: LList.H:284
const const_iterator & cend() const
Definition: LList.H:331
LList()
Null construct.
Definition: LList.H:108
Template class for non-intrusive linked lists.
Definition: LList.H:51
timeIndices insert(timeIndex, timeDirs[timeI].value())
Namespace for OpenFOAM.
T removeHead()
Remove and return head.
Definition: LList.H:172
Link structure.
Definition: LList.H:90
T obj_
Stored object.
Definition: LList.H:95
LListBase::const_iterator LListBase_const_iterator
Definition: LList.H:281
An STL-conforming const_iterator.
Definition: DLListBase.H:228
iterator begin()
Definition: LList.H:268
~LList()
Destructor.
Definition: LList.C:44
An STL-conforming iterator.
Definition: LList.H:233
T & reference
Type that can be used for storing into value_type.
Definition: LList.H:218
T & first()
Return the first entry added.
Definition: LList.H:133
void append(const T &a)
Add at tail of list.
Definition: LList.H:166
T value_type
Type of values the LList contains.
Definition: LList.H:214
void operator=(const LList< LListBase, T > &)
Definition: LList.C:76
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
link(T a)
Construct given object.
Definition: LList.H:98
friend class const_iterator
Definition: LList.H:85
void transfer(LList< LListBase, T > &)
Transfer the contents of the argument into this List.
Definition: LList.C:66
An STL-conforming iterator.
Definition: DLListBase.H:181
void clear()
Delete contents of list.
Definition: LList.C:53
label size_type
The type that can represent the size of a LList.
Definition: LList.H:225