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-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 \*---------------------------------------------------------------------------*/
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 
75 {
76  if (!size_)
77  {
79  << "list is empty"
81  }
82  return last_->next_;
83 }
84 
85 
86 inline const Foam::SLListBase::link*
88 {
89  if (!size_)
90  {
92  << "list is empty"
94  }
95  return last_->next_;
96 }
97 
98 
101 {
102  if (!size_)
103  {
105  << "list is empty"
107  }
108  return last_;
109 }
110 
111 
112 inline const Foam::SLListBase::link*
114 {
115  if (!size_)
116  {
118  << "list is empty"
119  << abort(FatalError);
120  }
121  return last_;
122 }
123 
124 
126 {
127  last_ = nullptr;
128  size_ = 0;
129 }
130 
131 
132 inline void Foam::SLListBase::swap(SLListBase& lst)
133 {
134  if (this == &lst)
135  {
136  return; // Self-swap is a no-op
137  }
138 
139  std::swap(last_, lst.last_);
140  std::swap(size_, lst.size_);
141 }
142 
143 
144 inline void Foam::SLListBase::transfer(SLListBase& lst)
145 {
146  if (this == &lst)
147  {
148  return; // Self-assignment is a no-op
149  }
150 
151  last_ = lst.last_;
152  size_ = lst.size_;
153 
154  lst.clear();
155 }
156 
157 
159 (
161 )
162 {
163  return remove(iter.node_);
164 }
165 
166 
167 // * * * * * * * * * * * * * * * STL iterator * * * * * * * * * * * * * * * //
168 
170 (
171  SLListBase* list,
172  SLListBase::link* item
173 )
174 :
175  node_(item),
176  list_(list),
177  copy_()
178 {
179  if (node_ != nullptr)
180  {
181  copy_ = *node_;
182  }
183 }
184 
185 
188 {
189  return node_;
190 }
191 
193 inline bool Foam::SLListBase::iterator::good() const noexcept
194 {
195  return (node_ != nullptr);
196 }
197 
198 
200 {
201  if (list_)
202  {
203  if (node_ == list_->last_ || list_->last_ == nullptr)
204  {
205  node_ = nullptr;
206  }
207  else
208  {
209  node_ = copy_.next_;
210  copy_ = *node_;
211  }
212  }
213 }
214 
215 
216 inline void Foam::SLListBase::iterator::operator=(const iterator& iter)
217 {
218  node_ = iter.node_;
219  list_ = iter.list_;
220  copy_ = iter.copy_;
221 }
222 
224 inline bool Foam::SLListBase::iterator::operator==(const iterator& iter) const
225 {
226  return node_ == iter.node_;
227 }
228 
229 
230 inline bool Foam::SLListBase::iterator::operator!=(const iterator& iter) const
231 {
232  return node_ != iter.node_;
233 }
234 
235 
238 {
239  if (size())
240  {
241  return iterator_first<iterator>();
242  }
243 
244  return end();
245 }
246 
247 
248 inline const Foam::SLListBase::iterator&
250 {
251  return iterator_end<SLListBase::iterator>();
252 }
253 
254 
257 {
258  return iterator_end<SLListBase::const_iterator>();
259 }
260 
261 
262 // * * * * * * * * * * * * * * STL const_iterator * * * * * * * * * * * * * //
263 
265 (
266  const SLListBase* list,
267  const SLListBase::link* item
268 )
269 :
270  node_(item),
271  list_(list)
272 {}
273 
274 
276 (
277  const SLListBase::iterator& iter
278 )
279 :
280  node_(iter.node_),
281  list_(iter.list_)
282 {}
283 
284 
287 {
288  return node_;
289 }
290 
293 {
294  return (node_ != nullptr);
295 }
296 
297 
299 {
300  if (list_)
301  {
302  if (node_ == list_->last_)
303  {
304  node_ = nullptr;
305  }
306  else
307  {
308  node_ = node_->next_;
309  }
310  }
311 }
312 
313 
314 inline bool Foam::SLListBase::const_iterator::operator==
315 (
316  const const_iterator& iter
317 ) const
318 {
319  return node_ == iter.node_;
320 }
321 
322 
323 inline bool Foam::SLListBase::const_iterator::operator!=
324 (
325  const const_iterator& iter
326 ) const
327 {
328  return node_ != iter.node_;
329 }
330 
331 
334 {
335  if (size())
336  {
337  return iterator_first<const_iterator>();
338  }
339 
340  return cend();
341 }
342 
343 
344 // ************************************************************************* //
link * get_node() const noexcept
The storage node.
Definition: SLListBaseI.H:180
void swap(SLListBase &lst)
Swap the contents of list.
Definition: SLListBaseI.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
friend class iterator
Definition: SLListBase.H:140
Base for singly-linked lists.
Definition: SLListBase.H:57
void clear()
Clear the list.
Definition: SLListBaseI.H:118
A primitive const node iterator.
Definition: SLListBase.H:320
bool operator==(const iterator &iter) const
Definition: SLListBaseI.H:217
void operator=(const iterator &iter)
Copy assignment.
Definition: SLListBaseI.H:209
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: SLListBaseI.H:326
link * back()
Return last entry.
Definition: SLListBaseI.H:93
iterator(const iterator &)=default
Copy construct.
const const_iterator & cend() const
End of list for iterators.
Definition: SLListBaseI.H:249
bool good() const noexcept
Pointing at a valid storage node.
Definition: SLListBaseI.H:186
label size() const noexcept
The number of elements in list.
Definition: SLListBase.H:180
link * front()
Return first entry.
Definition: SLListBaseI.H:67
bool good() const noexcept
Pointing at a valid storage node.
Definition: SLListBaseI.H:285
errorManip< error > abort(error &err)
Definition: errorManip.H:139
const direction noexcept
Definition: Scalar.H:258
void next()
Move forward through list.
Definition: SLListBaseI.H:291
const NullObject * nullObjectPtr
Pointer to the unique nullObject.
Definition: nullObject.C:29
const iterator & end()
End of list for iterators.
Definition: SLListBaseI.H:242
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: SLListBaseI.H:50
iterator begin()
Iterator to first item in list with non-const access.
Definition: SLListBaseI.H:230
const link * get_node() const noexcept
The storage node.
Definition: SLListBaseI.H:279
const_iterator(const const_iterator &)=default
Copy construct.
bool operator!=(const iterator &iter) const
Definition: SLListBaseI.H:223
void transfer(SLListBase &lst)
Transfer the contents of the argument into this list and annul the argument list. ...
Definition: SLListBaseI.H:137
link * remove(link *item)
Definition: SLListBase.C:99
A primitive non-const node iterator.
Definition: SLListBase.H:248
friend class const_iterator
Definition: SLListBase.H:143
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: SLListBaseI.H:35
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: SLListBaseI.H:28
void next()
Move forward through list.
Definition: SLListBaseI.H:192