SLListBaseI.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-2019 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 \*---------------------------------------------------------------------------*/
28 
29 #include "error.H"
30 #include "nullObject.H"
31 
32 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33 
34 template<class IteratorType>
35 inline const IteratorType& Foam::SLListBase::iterator_end()
36 {
37  return *reinterpret_cast<const IteratorType*>(nullObjectPtr);
38 }
39 
40 
41 template<class IteratorType>
42 inline IteratorType Foam::SLListBase::iterator_first() const
43 {
44  SLListBase* list = const_cast<SLListBase*>(this);
45 
46  if (size())
47  {
48  return IteratorType(list, const_cast<SLListBase::link*>(last_->next_));
49  }
50 
51  // Return an end iterator
52  return IteratorType(list, nullptr);
53 }
54 
55 
56 template<class IteratorType>
57 inline IteratorType Foam::SLListBase::iterator_last() const
58 {
59  SLListBase* list = const_cast<SLListBase*>(this);
60 
61  if (size())
62  {
63  return IteratorType(list, const_cast<SLListBase::link*>(last_));
64  }
65 
66  // Return an end iterator
67  return IteratorType(list, nullptr);
68 }
69 
70 
71 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
72 
73 inline Foam::label Foam::SLListBase::size() const noexcept
74 {
75  return size_;
76 }
77 
78 
79 inline bool Foam::SLListBase::empty() const noexcept
80 {
81  return !size_;
82 }
83 
84 
87 {
88  if (!size_)
89  {
91  << "list is empty"
92  << abort(FatalError);
93  }
94  return last_->next_;
95 }
96 
97 
98 inline const Foam::SLListBase::link*
100 {
101  if (!size_)
102  {
104  << "list is empty"
105  << abort(FatalError);
106  }
107  return last_->next_;
108 }
109 
110 
113 {
114  if (!size_)
115  {
117  << "list is empty"
118  << abort(FatalError);
119  }
120  return last_;
121 }
122 
123 
124 inline const Foam::SLListBase::link*
126 {
127  if (!size_)
128  {
130  << "list is empty"
131  << abort(FatalError);
132  }
133  return last_;
134 }
135 
136 
138 {
139  last_ = nullptr;
140  size_ = 0;
141 }
142 
143 
145 {
146  if (this == &lst)
147  {
148  return; // Self-swap is a no-op
149  }
150 
151  std::swap(last_, lst.last_);
152  std::swap(size_, lst.size_);
153 }
154 
155 
157 {
158  if (this == &lst)
159  {
160  return; // Self-assignment is a no-op
161  }
162 
163  last_ = lst.last_;
164  size_ = lst.size_;
165 
166  lst.clear();
167 }
168 
169 
171 (
173 )
174 {
175  return remove(iter.node_);
176 }
177 
178 
179 // * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * //
180 
182 (
183  SLListBase* list,
184  SLListBase::link* item
185 )
186 :
187  node_(item),
188  list_(list),
189  copy_()
190 {
191  if (node_ != nullptr)
192  {
193  copy_ = *node_;
194  }
195 }
196 
197 
200 {
201  return node_;
202 }
203 
204 
206 {
207  return (node_ != nullptr);
208 }
209 
210 
212 {
213  if (list_)
214  {
215  if (node_ == list_->last_ || list_->last_ == nullptr)
216  {
217  node_ = nullptr;
218  }
219  else
220  {
221  node_ = copy_.next_;
222  copy_ = *node_;
223  }
224  }
225 }
226 
227 
229 {
230  node_ = iter.node_;
231  list_ = iter.list_;
232  copy_ = iter.copy_;
233 }
234 
235 
236 inline bool Foam::SLListBase::iterator::operator==(const iterator& iter) const
237 {
238  return node_ == iter.node_;
239 }
240 
241 
242 inline bool Foam::SLListBase::iterator::operator!=(const iterator& iter) const
243 {
244  return node_ != iter.node_;
245 }
246 
247 
250 {
251  if (size())
252  {
253  return iterator_first<iterator>();
254  }
255 
256  return end();
257 }
258 
259 
260 inline const Foam::SLListBase::iterator&
262 {
263  return iterator_end<SLListBase::iterator>();
264 }
265 
266 
269 {
270  return iterator_end<SLListBase::const_iterator>();
271 }
272 
273 
274 // * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
275 
277 (
278  const SLListBase* list,
279  const SLListBase::link* item
280 )
281 :
282  node_(item),
283  list_(list)
284 {}
285 
286 
288 (
289  const SLListBase::iterator& iter
290 )
291 :
292  node_(iter.node_),
293  list_(iter.list_)
294 {}
295 
296 
297 inline const Foam::SLListBase::link*
299 {
300  return node_;
301 }
302 
303 
305 {
306  return (node_ != nullptr);
307 }
308 
309 
311 {
312  if (list_)
313  {
314  if (node_ == list_->last_)
315  {
316  node_ = nullptr;
317  }
318  else
319  {
320  node_ = node_->next_;
321  }
322  }
323 }
324 
325 
326 inline bool Foam::SLListBase::const_iterator::operator==
327 (
328  const const_iterator& iter
329 ) const
330 {
331  return node_ == iter.node_;
332 }
333 
334 
335 inline bool Foam::SLListBase::const_iterator::operator!=
336 (
337  const const_iterator& iter
338 ) const
339 {
340  return node_ != iter.node_;
341 }
342 
343 
346 {
347  if (size())
348  {
349  return iterator_first<const_iterator>();
350  }
351 
352  return cend();
353 }
354 
355 
356 // ************************************************************************* //
Foam::SLListBase::cbegin
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: SLListBaseI.H:345
Foam::nullObjectPtr
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:33
Foam::SLListBase::last
link * last()
Return last entry.
Definition: SLListBaseI.H:112
Foam::SLListBase::first
link * first()
Return first entry.
Definition: SLListBaseI.H:86
Foam::SLListBase::const_iterator::const_iterator
const_iterator(const const_iterator &)=default
Copy construct.
Foam::SLListBase::remove
link * remove(link *item)
Definition: SLListBase.C:103
Foam::SLListBase::clear
void clear()
Clear the list.
Definition: SLListBaseI.H:137
Foam::SLListBase::iterator::get_node
link * get_node() const
The storage node.
Definition: SLListBaseI.H:199
Foam::SLListBase::begin
iterator begin()
Iterator to first item in list with non-const access.
Definition: SLListBaseI.H:249
Foam::SLListBase::iterator_last
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: SLListBaseI.H:57
Foam::SLListBase::const_iterator::good
bool good() const
Pointing at a valid storage node.
Definition: SLListBaseI.H:304
Foam::SLListBase::iterator::operator!=
bool operator!=(const iterator &iter) const
Definition: SLListBaseI.H:242
error.H
Foam::SLListBase::const_iterator::get_node
const link * get_node() const
The storage node.
Definition: SLListBaseI.H:298
Foam::SLListBase::iterator_end
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: SLListBaseI.H:35
Foam::SLListBase::iterator::next
void next()
Move forward through list.
Definition: SLListBaseI.H:211
Foam::FatalError
error FatalError
Foam::SLListBase::swap
void swap(SLListBase &lst)
Swap the contents of list.
Definition: SLListBaseI.H:144
Foam::SLListBase::transfer
void transfer(SLListBase &lst)
Definition: SLListBaseI.H:156
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
stdFoam::cend
constexpr auto cend(const C &c) -> decltype(c.end())
Return const_iterator to the end of the container c.
Definition: stdFoam.H:137
Foam::SLListBase::iterator
A primitive non-const node iterator.
Definition: SLListBase.H:189
Foam::SLListBase::const_iterator
A primitive const node iterator.
Definition: SLListBase.H:245
Foam::SLListBase::end
const iterator & end()
End of list for iterators.
Definition: SLListBaseI.H:261
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::SLListBase::size
label size() const noexcept
The number of elements in list.
Definition: SLListBaseI.H:73
Foam::SLListBase::iterator::iterator
iterator(const iterator &)=default
Copy construct.
Foam::SLListBase
Base for singly-linked lists.
Definition: SLListBase.H:61
Foam::SLListBase::iterator::good
bool good() const
Pointing at a valid storage node.
Definition: SLListBaseI.H:205
nullObject.H
Foam::SLListBase::iterator::operator=
void operator=(const iterator &iter)
Copy assignment.
Definition: SLListBaseI.H:228
Foam::SLListBase::iterator::operator==
bool operator==(const iterator &iter) const
Definition: SLListBaseI.H:236
Foam::SLListBase::const_iterator::next
void next()
Move forward through list.
Definition: SLListBaseI.H:310
Foam::SLListBase::empty
bool empty() const noexcept
True if the list is empty.
Definition: SLListBaseI.H:79
Foam::SLListBase::iterator_first
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: SLListBaseI.H:42
Foam::SLListBase::cend
const const_iterator & cend() const
End of list for iterators.
Definition: SLListBaseI.H:268