SLListBase.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-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::SLListBase
29
30Description
31 Base for singly-linked lists.
32
33 The iterators associated with the list only have a core functionality
34 for navigation, with additional functionality to be added by inheriting
35 classes. The node iterators always have a node-pointer as the
36 first member data, which allows reinterpret_cast from anything else with
37 a nullptr as its first data member.
38 The nullObject is such an item (with a nullptr data member).
39
40SourceFiles
41 SLListBaseI.H
42 SLListBase.C
43
44\*---------------------------------------------------------------------------*/
45
46#ifndef Foam_SLListBase_H
47#define Foam_SLListBase_H
48
49#include "label.H"
50#include "uLabel.H"
51#include "stdFoam.H"
52
53// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54
55namespace Foam
56{
57
58/*---------------------------------------------------------------------------*\
59 Class SLListBase Declaration
60\*---------------------------------------------------------------------------*/
62class SLListBase
63{
64public:
65
66 //- The structure for a singly-linked storage node
67 struct link
68 {
69 //- Pointer to next entry in list
70 link* next_ = nullptr;
71
72 //- Default construct
73 link() = default;
74 };
75
76
77private:
78
79 // Private Data
80
81 //- A pointer to the last element.
82 // last_->next_ points to first element, i.e. circular storage
83 link* last_ = nullptr;
84
85 //- Number of elements in the list
86 label size_ = 0;
87
88
89protected:
90
91 // Protected Member Functions
92
93 //- Factory method to return an iterator end
94 // Simply reinterprets a NullObject as a SLListBase iterator.
95 template<class IteratorType>
96 inline static const IteratorType& iterator_end();
97
98 //- Factory method to return an iterator rend
99 // Deleted for SLListBase
100 template<class IteratorType>
101 static const IteratorType& iterator_rend() = delete;
102
103 //- Return iterator to first item or end-iterator if list is empty
104 // Removes constness which the caller promises to manage.
105 template<class IteratorType>
106 inline IteratorType iterator_first() const;
107
108 //- Return iterator to last item or end-iterator if list is empty
109 // Removes constness which the caller promises to manage.
110 template<class IteratorType>
111 inline IteratorType iterator_last() const;
112
113public:
114
115 // Forward declaration of iterators
116
117 class iterator;
118 friend class iterator;
119
121 friend class const_iterator;
122
123
124 // Generated Methods
125
126 //- Default construct
127 SLListBase() = default;
128
129 //- No copy construct
130 SLListBase(const SLListBase&) = delete;
131
132 //- No copy assignment
133 void operator=(const SLListBase&) = delete;
134
135 //- Destructor
136 ~SLListBase() = default;
137
138
139 // Member Functions
140
141 //- The number of elements in list
142 inline label size() const noexcept;
143
144 //- True if the list is empty
145 inline bool empty() const noexcept;
146
147 //- Return first entry
148 inline link* first();
149
150 //- Return const access to first entry
151 inline const link* first() const;
152
153 //- Return last entry
154 inline link* last();
155
156 //- Return const access to last entry
157 inline const link* last() const;
158
159
160 //- Add at front of list
161 void prepend(link* item);
162
163 //- Add at back of list
164 void append(link* item);
165
166 //- Remove and return first entry
167 link* removeHead();
168
169 // Remove and return element
170 link* remove(link* item);
171
172 // Remove and return element specified by iterator
173 inline link* remove(iterator& iter);
174
175 //- Clear the list
176 inline void clear();
177
178 //- Swap the contents of list
179 inline void swap(SLListBase& lst);
180
181 //- Transfer the contents of the argument into this list
182 //- and annul the argument list.
183 inline void transfer(SLListBase& lst);
184
185
186 // iterator
187
188 //- A primitive non-const node iterator.
189 // Must normally be extended by inheriting classes.
190 class iterator
192 friend class SLListBase;
193 friend class const_iterator;
194
195 //- The selected node.
196 // MUST be the first member for easy comparison between iterators
197 // and for reinterpret_cast from nullObject
198 link* node_;
199
200 //- The list being iterated on (as pointer for bitwise copy)
201 SLListBase* list_;
202
203 //- Copy of the node next pointer (to use after removal)
204 link copy_;
205
206 public:
207
208 //- Copy construct
209 iterator(const iterator&) = default;
210
211 //- Construct for a node on the list
212 inline iterator(SLListBase* list, link* item);
213
214 //- The storage node
215 inline link* get_node() const noexcept;
216
217 //- Pointing at a valid storage node
218 inline bool good() const noexcept;
219
220 //- Cannot move backward through list
221 inline void prev() = delete;
222
223 //- Move forward through list
224 inline void next();
225
226 //- Copy assignment
227 inline void operator=(const iterator& iter);
228
229 inline bool operator==(const iterator& iter) const;
230 inline bool operator!=(const iterator& iter) const;
231 };
232
233
234 // STL const_iterator
235
236 //- A primitive const node iterator.
237 // Must normally be extended by inheriting classes.
238 class const_iterator
239 {
240 //- The selected node.
241 // MUST be the first member for easy comparison between iterators
242 // and for reinterpret_cast from nullObject
243 const link* node_;
244
245 //- The list being iterated on (as pointer for bitwise copy)
246 const SLListBase* list_;
247
248 public:
249
250 //- Copy construct
251 const_iterator(const const_iterator&) = default;
252
253 //- Construct for a node on the list
254 inline const_iterator(const SLListBase* list, const link* item);
255
256 //- Construct from a non-const iterator
257 inline const_iterator(const SLListBase::iterator& iter);
258
259 //- The storage node
260 inline const link* get_node() const noexcept;
261
262 //- Pointing at a valid storage node
263 inline bool good() const noexcept;
264
265 //- Cannot move backward through list
266 inline void prev() = delete;
267
268 //- Move forward through list
269 inline void next();
270
271 //- Copy assignment
272 const_iterator& operator=(const const_iterator&) = default;
273
274 inline bool operator==(const const_iterator& iter) const;
275 inline bool operator!=(const const_iterator& iter) const;
276 };
277
278
279 //- Iterator to first item in list with non-const access
280 inline iterator begin();
281
282 //- Iterator to first item in list with const access
283 inline const_iterator cbegin() const;
284
285 //- No reverse iteration
286 const_iterator crbegin() const = delete;
287
288 //- End of list for iterators
289 inline const iterator& end();
290
291 //- End of list for iterators
292 inline const const_iterator& cend() const;
293
294 //- No reverse iteration
295 const const_iterator& crend() const = delete;
296};
297
298
299// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
300
301} // End namespace Foam
302
303// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304
305#include "SLListBaseI.H"
306
307// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
308
309#endif
310
311// ************************************************************************* //
A primitive const node iterator.
Definition: SLListBase.H:238
const_iterator(const const_iterator &)=default
Copy construct.
A primitive non-const node iterator.
Definition: SLListBase.H:190
iterator(const iterator &)=default
Copy construct.
Base for singly-linked lists.
Definition: SLListBase.H:62
link * remove(link *item)
Definition: SLListBase.C:103
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: SLListBaseI.H:345
link * first()
Return first entry.
Definition: SLListBaseI.H:86
const const_iterator & cend() const
End of list for iterators.
Definition: SLListBaseI.H:268
void transfer(SLListBase &lst)
Definition: SLListBaseI.H:156
const iterator & end()
End of list for iterators.
Definition: SLListBaseI.H:261
bool empty() const noexcept
True if the list is empty.
Definition: SLListBaseI.H:79
link * last()
Return last entry.
Definition: SLListBaseI.H:112
const_iterator crbegin() const =delete
No reverse iteration.
link * removeHead()
Remove and return first entry.
Definition: SLListBase.C:77
IteratorType iterator_first() const
Return iterator to first item or end-iterator if list is empty.
Definition: SLListBaseI.H:42
void prepend(link *item)
Add at front of list.
Definition: SLListBase.C:34
static const IteratorType & iterator_end()
Factory method to return an iterator end.
Definition: SLListBaseI.H:35
label size() const noexcept
The number of elements in list.
Definition: SLListBaseI.H:73
IteratorType iterator_last() const
Return iterator to last item or end-iterator if list is empty.
Definition: SLListBaseI.H:57
void swap(SLListBase &lst)
Swap the contents of list.
Definition: SLListBaseI.H:144
void append(link *item)
Add at back of list.
Definition: SLListBase.C:56
~SLListBase()=default
Destructor.
const const_iterator & crend() const =delete
No reverse iteration.
iterator begin()
Iterator to first item in list with non-const access.
Definition: SLListBaseI.H:249
void clear()
Clear the list.
Definition: SLListBaseI.H:137
SLListBase(const SLListBase &)=delete
No copy construct.
static const IteratorType & iterator_rend()=delete
Factory method to return an iterator rend.
SLListBase()=default
Default construct.
void operator=(const SLListBase &)=delete
No copy assignment.
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...