1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* Owning version of intrusive_list for GDB, the GNU debugger.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDBSUPPORT_OWNING_INTRUSIVE_LIST_H
#define GDBSUPPORT_OWNING_INTRUSIVE_LIST_H
#include "intrusive_list.h"
/* An owning version of intrusive_list. */
template<typename T, typename AsNode = intrusive_base_node<T>>
class owning_intrusive_list : private intrusive_list<T, AsNode>
{
using base = intrusive_list<T, AsNode>;
public:
using value_type = typename base::value_type;
using reference = typename base::reference;
using iterator = typename base::iterator;
using reverse_iterator = typename base::reverse_iterator;
using const_iterator = typename base::const_iterator;
using unique_pointer = std::unique_ptr<T>;
using base::iterator_to;
using base::front;
using base::back;
using base::empty;
using base::begin;
using base::cbegin;
using base::end;
using base::cend;
using base::rbegin;
using base::crbegin;
using base::rend;
using base::crend;
owning_intrusive_list () noexcept = default;
owning_intrusive_list (owning_intrusive_list &&other) noexcept
: base (std::move (other))
{
}
~owning_intrusive_list ()
{ this->clear (); }
owning_intrusive_list &operator= (owning_intrusive_list &&other) noexcept
{
this->clear ();
this->base::operator= (std::move (other));
return *this;
}
void swap (owning_intrusive_list &other) noexcept
{ this->base::swap (other); }
/* Insert ELEM at the front of the list.
The list takes ownership of ELEM. */
void push_front (unique_pointer elem) noexcept
{ this->base::push_front (*elem.release ()); }
/* Insert ELEM at the back of the list.
The list takes ownership of ELEM. */
void push_back (unique_pointer elem) noexcept
{ this->base::push_back (*elem.release ()); }
/* Insert ELEM before POS in the list.
The list takes ownership of ELEM. */
iterator insert (const_iterator pos, unique_pointer elem) noexcept
{ return this->base::insert (pos, *elem.release ()); }
void splice (owning_intrusive_list &&other) noexcept
{ this->base::splice (std::move (other)); }
/* Remove the element at the front of the list. The element is destroyed. */
void pop_front () noexcept
{
unique_pointer holder (&this->front ());
this->base::pop_front ();
}
/* Remove the element at the back of the list. The element is destroyed. */
void pop_back () noexcept
{
unique_pointer holder (&this->back ());
this->base::pop_back ();
}
/* Remove the element pointed by I from the list. The element
pointed by I is destroyed. */
iterator erase (const_iterator i) noexcept
{
unique_pointer holder (&*i);
return this->base::erase (i);
}
/* Remove all elements from the list. All elements are destroyed. */
void clear () noexcept
{
while (!this->empty ())
this->pop_front ();
}
/* Construct an element in-place at the front of the list.
Return a reference to the new element. */
template<typename... Args>
reference emplace_front (Args &&...args)
{ return this->emplace (this->begin (), std::forward<Args> (args)...); }
/* Construct an element in-place at the back of the list.
Return a reference to the new element. */
template<typename... Args>
reference emplace_back (Args &&...args)
{ return this->emplace (this->end (), std::forward<Args> (args)...); }
/* Construct an element in-place in the list, before POS.
Return a reference to the new element. */
template<typename... Args>
reference emplace (const_iterator pos, Args &&...args)
{
return *this->insert (pos,
std::make_unique<T> (std::forward<Args> (args)...));
}
/* Return type for the release method. */
struct release_ret
{
/* Iterator to the following element in the list. */
iterator next;
/* The released element. */
unique_pointer released;
};
release_ret release (const_iterator i) noexcept
{
iterator next = i;
++next;
unique_pointer released (&*i);
this->unlink_element (*i);
return { next, std::move (released) };
}
};
#endif /* GDBSUPPORT_OWNING_INTRUSIVE_LIST_H */
|