LPtrList.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::LPtrList
29 
30 Description
31  Template class for non-intrusive linked PtrLists.
32 
33 SourceFiles
34  LPtrList.C
35  LPtrListIO.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef Foam_LPtrList_H
40 #define Foam_LPtrList_H
41 
42 #include "LList.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // Forward Declarations
50 
51 template<class LListBase, class T> class LPtrList;
52 
53 template<class LListBase, class T>
54 Istream& operator>>
55 (
56  Istream& is,
58 );
59 
60 template<class LListBase, class T>
61 Ostream& operator<<
62 (
63  Ostream& os,
64  const LPtrList<LListBase, T>& list
65 );
66 
67 
68 /*---------------------------------------------------------------------------*\
69  Class LPtrList Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 template<class LListBase, class T>
73 class LPtrList
74 :
75  public LList<LListBase, T*>
76 {
77  // Private Member Functions
78 
79  //- Read from Istream using given Istream constructor class
80  template<class INew>
81  void readIstream(Istream& is, const INew& inew);
82 
83 
84 public:
85 
86  // STL type definitions
87 
88  //- Pointer for LPtrList::value_type objects.
89  typedef T* pointer;
90 
91  //- Const pointer for LPtrList::value_type objects.
92  typedef const T* const_pointer;
93 
94  //- Reference for LPtrList::value_type objects.
95  typedef T& reference;
96 
97  //- Const reference for LPtrList::value_type objects.
98  typedef const T& const_reference;
99 
100 
101  // Forward Declaration (iterators)
102 
103  class iterator;
104  class const_iterator;
105 
106  using base_iterator = typename LListBase::iterator;
107  using const_base_iterator = typename LListBase::const_iterator;
108 
109  //- The parent list storage
113  // Constructors
114 
115  //- Default construct
116  LPtrList() = default;
118  //- Construct and add initial item pointer
119  explicit LPtrList(T* item)
120  {
121  this->push_front(item);
122  }
123 
124  //- Copy construct by using 'clone()' for each element
125  LPtrList(const LPtrList& lst);
126 
127  //- Move construct
128  LPtrList(LPtrList&& lst);
129 
130  //- Construct from Istream using given Istream constructor class
131  template<class INew>
132  LPtrList(Istream& is, const INew& inew);
133 
134  //- Construct from Istream using default Istream constructor class
135  explicit LPtrList(Istream& is);
136 
137 
138  //- Destructor. Calls clear()
139  ~LPtrList();
140 
141 
142  // Member Functions
143 
144  //- The first entry in the list
145  T& front()
146  {
147  return *(parent_type::front());
148  }
149 
150  //- The first entry in the list (const access)
151  const T& front() const
152  {
153  return *(parent_type::front());
154  }
155 
156  //- The last entry in the list
157  T& back()
158  {
159  return *(parent_type::back());
160  }
161 
162  //- The last entry in the list (const access)
163  const T& back() const
164  {
165  return *(parent_type::back());
166  }
167 
168  //- Remove first element(s) from the list (deletes pointers)
169  void pop_front(label n = 1);
170 
171  //- Clear the contents of the list
172  void clear();
173 
174  //- Transfer the contents of the argument into this List
175  //- and annul the argument list.
177 
178 
179  // Member Operators
180 
181  //- Copy assign by using 'clone()' for each element
182  void operator=(const LPtrList<LListBase, T>& lst);
183 
184  //- Move assign
185  void operator=(LPtrList<LListBase, T>&& lst);
186 
187 
188  // STL iterator
189 
190  //- An STL-conforming iterator
191  class iterator
192  :
193  public parent_type::iterator
194  {
195  public:
196 
197  iterator(base_iterator iter)
198  :
199  parent_type::iterator(iter)
200  {}
201 
202  //- Return the address of the object being referenced
203  pointer get() const
204  {
206  }
207 
208  reference operator*() const
209  {
210  return *(this->get());
211  }
212 
213  pointer operator->() const
214  {
215  return this->get();
216  }
217 
218  reference operator()() const
219  {
220  return operator*();
221  }
222  };
223 
224 
225  // STL const_iterator
226 
227  //- An STL-conforming const_iterator
228  class const_iterator
229  :
230  public parent_type::const_iterator
231  {
232  public:
233 
235  :
237  {}
240  :
242  {}
243 
244  //- Return the address of the object being referenced
245  const_pointer get() const
246  {
248  }
249 
250  const_reference operator*() const
251  {
252  return *(this->get());
253  }
254 
255  const_pointer operator->() const
256  {
257  return this->get();
258  }
259 
260  const_reference operator()() const
261  {
262  return operator*();
263  }
264  };
265 
266 
267  // STL reverse_iterator
268 
269  //- A reverse_iterator, for base classes that support
270  //- reverse iteration
271  class reverse_iterator
272  :
273  public parent_type::reverse_iterator
274  {
275  public:
276 
278  :
279  parent_type::reverse_iterator(iter)
280  {}
281 
282  //- Return the address of the object being referenced
283  pointer get() const
284  {
286  }
287 
288  reference operator*() const
289  {
290  return *(this->get());
291  }
293  pointer operator->() const
294  {
295  return this->get();
296  }
298  reference operator()() const
299  {
300  return operator*();
301  }
302  };
303 
304 
305  // STL const_reverse_iterator
306 
307  //- A const_reverse_iterator, for base classes that support
308  //- reverse iteration
310  :
311  public parent_type::const_reverse_iterator
312  {
313  public:
314 
316  :
317  parent_type::const_reverse_iterator(iter)
318  {}
319 
320  //- Return the address of the object being referenced
321  const_pointer get() const
322  {
324  }
325 
327  {
328  return *(this->get());
329  }
330 
331  const_pointer operator->() const
332  {
333  return this->get();
334  }
335 
336  const_reference operator()() const
337  {
338  return operator*();
339  }
340  };
341 
342 
343  //- Iterator to first item in list with non-const access
344  inline iterator begin()
345  {
346  return LListBase::template iterator_first<base_iterator>();
347  }
348 
349  //- Iterator to first item in list with const access
350  inline const_iterator cbegin() const
351  {
352  return LListBase::template iterator_first<const_base_iterator>();
353  }
354 
355  //- Iterator to last item in list with non-const access
356  inline reverse_iterator rbegin()
357  {
358  return LListBase::template iterator_last<base_iterator>();
359  }
360 
361  //- Iterator to last item in list with const access
363  {
364  return LListBase::template iterator_last<const_base_iterator>();
365  }
366 
367  //- Iterator to first item in list with const access
368  inline const_iterator begin() const
369  {
370  return LListBase::cbegin();
371  }
372 
373  //- Iterator to last item in list with const access
374  inline const_reverse_iterator rbegin() const
375  {
376  return crbegin();
377  }
378 
379 
380  //- End of list for forward iterators
381  inline const iterator& end()
382  {
383  return LListBase::template iterator_end<iterator>();
384  }
385 
386  //- End of list for forward iterators
387  inline const const_iterator& cend() const
388  {
389  return LListBase::template iterator_end<const_iterator>();
390  }
392  //- End of list for reverse iterators
393  inline const reverse_iterator& rend()
394  {
395  return LListBase::template iterator_rend<reverse_iterator>();
396  }
397 
398  //- End of list for reverse iterators
399  inline const const_reverse_iterator& crend() const
400  {
401  return LListBase::template iterator_rend<const_reverse_iterator>();
402  }
403 
404  //- End of list for forward iterators
405  inline const const_iterator& end() const
406  {
407  return cend();
408  }
410  //- End of list for reverse iterators
411  inline const const_reverse_iterator& rend() const
412  {
413  return crend();
414  }
415 
416 
417  // IOstream operators
418 
419  friend Istream& operator>> <LListBase, T>
420  (
421  Istream& is,
423  );
424 
425  friend Ostream& operator<< <LListBase, T>
426  (
427  Ostream& os,
428  const LPtrList<LListBase, T>& list
429  );
430 
431 
432  // Housekeeping
434  //- The first entry in the list
435  //FOAM_DEPRECATED_FOR(2022-10, "front()")
436  T& first() { return front(); }
437 
438  //- The first entry in the list (const access)
439  //FOAM_DEPRECATED_FOR(2022-10, "front()")
440  const T& first() const { return front(); }
442  //- The last entry in the list
443  //FOAM_DEPRECATED_FOR(2022-10, "back()")
444  T& last() { return back(); }
445 
446  //- The last entry in the list (const access)
447  //FOAM_DEPRECATED_FOR(2022-10, "back()")
448  const T& last() const { return back(); }
449 };
451 
452 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
453 
454 } // End namespace Foam
455 
456 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457 
458 #ifdef NoRepository
459  #include "LPtrList.C"
460  #include "LPtrListIO.C"
461 #endif
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 #endif
467 // ************************************************************************* //
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LPtrList.H:409
iterator begin()
Iterator to first item in list with non-const access.
Definition: LPtrList.H:401
reference operator*() const
Definition: LPtrList.H:339
A const_reverse_iterator, for base classes that support reverse iteration.
Definition: LPtrList.H:362
reference front()
The first entry in the list.
Definition: LList.H:250
const_reverse_iterator crbegin() const
Iterator to last item in list with const access.
Definition: LPtrList.H:425
T & reference
Reference for LPtrList::value_type objects.
Definition: LPtrList.H:98
const_reference operator*() const
Definition: LPtrList.H:381
Template class for non-intrusive linked lists.
Definition: LList.H:46
T & first()
The first entry in the list.
Definition: LPtrList.H:518
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
reverse_iterator rbegin()
Iterator to last item in list with non-const access.
Definition: LPtrList.H:417
typename LListBase::const_iterator const_base_iterator
Definition: LPtrList.H:112
void operator=(const LPtrList< LListBase, T > &lst)
Copy assign by using &#39;clone()&#39; for each element.
Definition: LPtrList.C:94
const_pointer operator->() const
Definition: LPtrList.H:302
An STL-conforming const_iterator.
Definition: LPtrList.H:273
T * pointer
Pointer for LPtrList::value_type objects.
Definition: LPtrList.H:88
~LPtrList()
Destructor. Calls clear()
Definition: LPtrList.C:50
reference operator*() const
Definition: LPtrList.H:251
const T & const_reference
Const reference for LPtrList::value_type objects.
Definition: LPtrList.H:103
T & last()
The last entry in the list.
Definition: LPtrList.H:532
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:46
An STL-conforming iterator.
Definition: LPtrList.H:232
const_iterator(const_base_iterator iter)
Definition: LPtrList.H:279
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
reverse_iterator(base_iterator iter)
Definition: LPtrList.H:326
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: LPtrList.H:466
T & back()
The last entry in the list.
Definition: LPtrList.H:184
void push_front(const T * &elem)
Add copy at front of list.
Definition: LList.H:283
reference operator()() const
Definition: LPtrList.H:261
pointer operator->() const
Definition: LPtrList.H:256
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
T & front()
The first entry in the list.
Definition: LPtrList.H:168
A reverse_iterator, for base classes that support reverse iteration.
Definition: LPtrList.H:320
OBJstream os(runTime.globalPath()/outputName)
typename LListBase::iterator base_iterator
Definition: LPtrList.H:111
const_pointer operator->() const
Definition: LPtrList.H:386
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:183
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const iterator & end()
End of list for forward iterators.
Definition: LPtrList.H:450
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: LPtrList.H:474
const const_iterator & cend() const
End of list for forward iterators.
Definition: LPtrList.H:458
void clear()
Clear the contents of the list.
Definition: LPtrList.C:76
LPtrList()=default
Default construct.
const_reference operator()() const
Definition: LPtrList.H:307
const_reference operator*() const
Definition: LPtrList.H:297
const_reference operator()() const
Definition: LPtrList.H:391
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
const_reverse_iterator(const_base_iterator iter)
Definition: LPtrList.H:368
iterator(base_iterator iter)
Definition: LPtrList.H:238
label n
void transfer(LPtrList< LListBase, T > &lst)
Transfer the contents of the argument into this List and annul the argument list. ...
Definition: LPtrList.C:84
const T * const_pointer
Const pointer for LPtrList::value_type objects.
Definition: LPtrList.H:93
LList< LListBase, T * > parent_type
The parent list storage.
Definition: LPtrList.H:117
reference back()
The last entry in the list.
Definition: LList.H:266
reference operator()() const
Definition: LPtrList.H:349
Namespace for OpenFOAM.
void pop_front(label n=1)
Remove first element(s) from the list (deletes pointers)
Definition: LPtrList.C:59