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  //- Emplace construct new element at front of list. Return reference.
169  template<class... Args>
170  T& emplace_front(Args&&... args)
171  {
172  T* ptr = new T(std::forward<Args>(args)...);
173  this->push_front(ptr);
174  return *ptr; // OR: return *(parent_type::front());
175  }
177  //- Emplace construct new element at back of list. Return reference.
178  template<class... Args>
179  T& emplace_back(Args&&... args)
180  {
181  T* ptr = new T(std::forward<Args>(args)...);
182  this->push_back(ptr);
183  return *ptr; // OR: return *(parent_type::back());
184  }
185 
186  //- Remove first element(s) from the list (deletes pointers)
187  void pop_front(label n = 1);
188 
189  //- Clear the contents of the list
190  void clear();
191 
192  //- Transfer the contents of the argument into this List
193  //- and annul the argument list.
194  void transfer(LPtrList<LListBase, T>& lst);
195 
196 
197  // Member Operators
198 
199  //- Copy assign by using 'clone()' for each element
200  void operator=(const LPtrList<LListBase, T>& lst);
202  //- Move assign
203  void operator=(LPtrList<LListBase, T>&& lst);
204 
205 
206  // STL iterator
207 
208  //- An STL-conforming iterator
209  class iterator
210  :
211  public parent_type::iterator
212  {
213  public:
214 
215  iterator(base_iterator iter)
216  :
217  parent_type::iterator(iter)
218  {}
219 
220  //- Return the address of the object being referenced
221  pointer get() const
222  {
224  }
225 
226  reference operator*() const
227  {
228  return *(this->get());
229  }
230 
231  pointer operator->() const
232  {
233  return this->get();
234  }
235 
236  reference operator()() const
237  {
238  return operator*();
239  }
240  };
241 
242 
243  // STL const_iterator
244 
245  //- An STL-conforming const_iterator
246  class const_iterator
247  :
248  public parent_type::const_iterator
249  {
250  public:
251 
253  :
255  {}
256 
258  :
260  {}
261 
262  //- Return the address of the object being referenced
263  const_pointer get() const
264  {
266  }
267 
269  {
270  return *(this->get());
271  }
272 
273  const_pointer operator->() const
274  {
275  return this->get();
276  }
277 
278  const_reference operator()() const
279  {
280  return operator*();
281  }
282  };
284 
285  // STL reverse_iterator
286 
287  //- A reverse_iterator, for base classes that support
288  //- reverse iteration
289  class reverse_iterator
290  :
291  public parent_type::reverse_iterator
292  {
293  public:
294 
296  :
298  {}
299 
300  //- Return the address of the object being referenced
301  pointer get() const
302  {
304  }
305 
307  {
308  return *(this->get());
309  }
310 
311  pointer operator->() const
312  {
313  return this->get();
314  }
315 
316  reference operator()() const
317  {
318  return operator*();
319  }
320  };
321 
322 
323  // STL const_reverse_iterator
325  //- A const_reverse_iterator, for base classes that support
326  //- reverse iteration
328  :
329  public parent_type::const_reverse_iterator
330  {
331  public:
332 
334  :
335  parent_type::const_reverse_iterator(iter)
336  {}
337 
338  //- Return the address of the object being referenced
339  const_pointer get() const
340  {
342  }
343 
344  const_reference operator*() const
345  {
346  return *(this->get());
347  }
349  const_pointer operator->() const
350  {
351  return this->get();
352  }
353 
354  const_reference operator()() const
355  {
356  return operator*();
357  }
358  };
359 
360 
361  //- Iterator to first item in list with non-const access
362  inline iterator begin()
363  {
364  return LListBase::template iterator_first<base_iterator>();
365  }
367  //- Iterator to first item in list with const access
368  inline const_iterator cbegin() const
369  {
370  return LListBase::template iterator_first<const_base_iterator>();
371  }
372 
373  //- Iterator to last item in list with non-const access
374  inline reverse_iterator rbegin()
375  {
376  return LListBase::template iterator_last<base_iterator>();
377  }
378 
379  //- Iterator to last item in list with const access
380  inline const_reverse_iterator crbegin() const
381  {
382  return LListBase::template iterator_last<const_base_iterator>();
383  }
385  //- Iterator to first item in list with const access
386  inline const_iterator begin() const
387  {
388  return LListBase::cbegin();
389  }
391  //- Iterator to last item in list with const access
392  inline const_reverse_iterator rbegin() const
393  {
394  return crbegin();
395  }
396 
397 
398  //- End of list for forward iterators
399  inline const iterator& end()
400  {
401  return LListBase::template iterator_end<iterator>();
402  }
404  //- End of list for forward iterators
405  inline const const_iterator& cend() const
406  {
407  return LListBase::template iterator_end<const_iterator>();
408  }
409 
410  //- End of list for reverse iterators
411  inline const reverse_iterator& rend()
412  {
413  return LListBase::template iterator_rend<reverse_iterator>();
414  }
415 
416  //- End of list for reverse iterators
417  inline const const_reverse_iterator& crend() const
418  {
419  return LListBase::template iterator_rend<const_reverse_iterator>();
420  }
421 
422  //- End of list for forward iterators
423  inline const const_iterator& end() const
424  {
425  return cend();
426  }
427 
428  //- End of list for reverse iterators
429  inline const const_reverse_iterator& rend() const
430  {
431  return crend();
432  }
433 
434 
435  // IOstream operators
436 
437  friend Istream& operator>> <LListBase, T>
438  (
439  Istream& is,
441  );
442 
443  friend Ostream& operator<< <LListBase, T>
444  (
445  Ostream& os,
446  const LPtrList<LListBase, T>& list
447  );
448 
449 
450  // Housekeeping
451 
452  //- The first entry in the list
453  //FOAM_DEPRECATED_FOR(2022-10, "front()")
454  T& first() { return front(); }
456  //- The first entry in the list (const access)
457  //FOAM_DEPRECATED_FOR(2022-10, "front()")
458  const T& first() const { return front(); }
459 
460  //- The last entry in the list
461  //FOAM_DEPRECATED_FOR(2022-10, "back()")
462  T& last() { return back(); }
464  //- The last entry in the list (const access)
465  //FOAM_DEPRECATED_FOR(2022-10, "back()")
466  const T& last() const { return back(); }
467 };
468 
469 
470 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
471 
472 } // End namespace Foam
473 
474 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
475 
476 #ifdef NoRepository
477  #include "LPtrList.C"
478  #include "LPtrListIO.C"
479 #endif
481 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
482 
483 #endif
484 
485 // ************************************************************************* //
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LPtrList.H:431
iterator begin()
Iterator to first item in list with non-const access.
Definition: LPtrList.H:423
reference operator*() const
Definition: LPtrList.H:361
A const_reverse_iterator, for base classes that support reverse iteration.
Definition: LPtrList.H:384
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:447
T & reference
Reference for LPtrList::value_type objects.
Definition: LPtrList.H:98
const_reference operator*() const
Definition: LPtrList.H:403
Template class for non-intrusive linked lists.
Definition: LList.H:46
T & first()
The first entry in the list.
Definition: LPtrList.H:540
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:439
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:324
An STL-conforming const_iterator.
Definition: LPtrList.H:295
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:273
T & emplace_back(Args &&... args)
Emplace construct new element at back of list. Return reference.
Definition: LPtrList.H:212
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:554
Template class for non-intrusive linked PtrLists.
Definition: LPtrList.H:46
An STL-conforming iterator.
Definition: LPtrList.H:254
const_iterator(const_base_iterator iter)
Definition: LPtrList.H:301
tmp< faMatrix< Type > > operator*(const areaScalarField::Internal &, const faMatrix< Type > &)
reverse_iterator(base_iterator iter)
Definition: LPtrList.H:348
const reverse_iterator & rend()
End of list for reverse iterators.
Definition: LPtrList.H:488
T & emplace_front(Args &&... args)
Emplace construct new element at front of list. Return reference.
Definition: LPtrList.H:201
T & back()
The last entry in the list.
Definition: LPtrList.H:184
void push_back(const T * &elem)
Add copy at back of list.
Definition: LList.H:299
void push_front(const T * &elem)
Add copy at front of list.
Definition: LList.H:283
reference operator()() const
Definition: LPtrList.H:283
pointer operator->() const
Definition: LPtrList.H:278
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
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:342
OBJstream os(runTime.globalPath()/outputName)
typename LListBase::iterator base_iterator
Definition: LPtrList.H:111
const_pointer operator->() const
Definition: LPtrList.H:408
constexpr auto cbegin(const C &c) -> decltype(c.begin())
Return const_iterator to the beginning of the container c.
Definition: stdFoam.H:190
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const iterator & end()
End of list for forward iterators.
Definition: LPtrList.H:472
const const_reverse_iterator & crend() const
End of list for reverse iterators.
Definition: LPtrList.H:496
const const_iterator & cend() const
End of list for forward iterators.
Definition: LPtrList.H:480
void clear()
Clear the contents of the list.
Definition: LPtrList.C:76
LPtrList()=default
Default construct.
const_reference operator()() const
Definition: LPtrList.H:329
const_reference operator*() const
Definition: LPtrList.H:319
const_reference operator()() const
Definition: LPtrList.H:413
A helper class when constructing from an Istream or dictionary.
Definition: INew.H:46
const_reverse_iterator(const_base_iterator iter)
Definition: LPtrList.H:390
iterator(base_iterator iter)
Definition: LPtrList.H:260
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
Foam::argList args(argc, argv)
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:371
Namespace for OpenFOAM.
void pop_front(label n=1)
Remove first element(s) from the list (deletes pointers)
Definition: LPtrList.C:59