L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hlist
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11#include "bits/list_basics.h"
12#include "type_traits"
13
14namespace cxx {
15
21template<typename ELEM_TYPE>
23{
24public:
30 H_list_item_t() : _n(0), _pn(0) {}
37 ~H_list_item_t() noexcept { l_remove(); }
38
39private:
40 H_list_item_t(H_list_item_t const &) = delete;
41
42 template<typename T, typename P> friend class H_list;
43 template<typename T, typename X> friend struct Bits::Basic_list_policy;
44
45 void l_remove() noexcept
46 {
47 if (!_pn)
48 return;
49
50 *_pn = _n;
51 if (_n)
52 _n->_pn = _pn;
53
54 _pn = 0;
55 }
56
57 H_list_item_t *_n, **_pn;
58};
59
62
68template< typename T, typename POLICY = Bits::Basic_list_policy< T, H_list_item> >
69class H_list : public Bits::Basic_list<POLICY>
70{
71private:
72 typedef typename POLICY::Item_type Item;
74 H_list(H_list const &);
75 void operator = (H_list const &);
76
77public:
78 typedef typename Base::Iterator Iterator;
79
80 // BSS allocation
81 explicit H_list(bool x) : Base(x) {}
82 H_list() : Base() {}
83
93 static Iterator iter(T *c) { return Base::__iter(c->Item::_pn); }
94
96 static bool in_list(T const *e) { return e->Item::_pn; }
97
99 void add(T *e)
100 {
101 if (this->_f)
102 this->_f->_pn = &e->Item::_n;
103 e->Item::_n = this->_f;
104 e->Item::_pn = &this->_f;
105 this->_f = static_cast<Item*>(e);
106 }
107
109 void push_front(T *e) { add(e); }
110
117 {
118 T *r = this->front();
119 remove(r);
120 return r;
121 }
122
133 Iterator insert(T *e, Iterator const &pred)
134 {
135 Item **x = &this->_f;
136 if (pred != Base::end())
137 x = &(*pred)->_n;
138
139 e->Item::_n = *x;
140
141 if (*x)
142 (*x)->_pn = &(e->Item::_n);
143
144 e->Item::_pn = x;
145 *x = static_cast<Item*>(e);
146 return iter(e);
147 }
148
160 static Iterator insert_after(T *e, Iterator const &pred)
161 {
162 Item **x = &(*pred)->_n;
163 e->Item::_n = *x;
164
165 if (*x)
166 (*x)->_pn = &(e->Item::_n);
167
168 e->Item::_pn = x;
169 *x = static_cast<Item*>(e);
170 return iter(e);
171 }
172
180 static void insert_before(T *e, Iterator const &succ)
181 {
182 Item **x = Base::__get_internal(succ);
183
184 e->Item::_n = *x;
185 e->Item::_pn = x;
186
187 if (*x)
188 (*x)->_pn = &e->Item::_n;
189
190 *x = static_cast<Item*>(e);
191 }
192
204 static void replace(T *p, T *e)
205 {
206 e->Item::_n = p->Item::_n;
207 e->Item::_pn = p->Item::_pn;
208 *(p->Item::_pn) = static_cast<Item*>(e);
209 if (e->Item::_n)
210 e->Item::_n->_pn = &(e->Item::_n);
211
212 p->Item::_pn = 0;
213 }
214
220 static void remove(T *e)
221 { e->Item::l_remove(); }
222
236 static Iterator erase(Iterator const &e)
237 { e->Item::l_remove(); return e; }
238};
239
247template< typename T >
248struct H_list_t : H_list<T, Bits::Basic_list_policy< T, H_list_item_t<T> > >
249{
250 H_list_t() = default;
251 H_list_t(bool b)
253 {};
254};
255
256template< typename T >
257class H_list_bss : public H_list<T>
258{
259public:
260 H_list_bss() : H_list<T>(true) {}
261};
262
263template< typename T >
264class H_list_t_bss : public H_list_t<T>
265{
266public:
267 H_list_t_bss() : H_list_t<T>(true) {}
268};
269
270
271}
Internal: Common functions for all head-based list implementations.
Definition list_basics.h:40
Const_iterator end() const
Return a const iterator to the end of the list.
POLICY::Head_type _f
Pointer to front of the list.
Value_type front() const
Return the first element in the list.
Basic element type for a double-linked H_list.
Definition hlist:23
H_list_item_t()
Constructor.
Definition hlist:30
~H_list_item_t() noexcept
Destructor.
Definition hlist:37
General double-linked list of unspecified cxx::H_list_item elements.
Definition hlist:70
static Iterator insert_after(T *e, Iterator const &pred)
Insert an element after the iterator position.
Definition hlist:160
T * pop_front()
Remove and return the head element of the list.
Definition hlist:116
static void replace(T *p, T *e)
Replace an element in a list with a new element.
Definition hlist:204
static Iterator erase(Iterator const &e)
Remove the element at the given iterator position.
Definition hlist:236
static void insert_before(T *e, Iterator const &succ)
Insert an element before the iterator position.
Definition hlist:180
void add(T *e)
Add element to the front of the list.
Definition hlist:99
Iterator insert(T *e, Iterator const &pred)
Insert an element at the iterator position.
Definition hlist:133
static void remove(T *e)
Remove the given element from its list.
Definition hlist:220
static Iterator iter(T *c)
Return an iterator for an arbitrary list element.
Definition hlist:93
static bool in_list(T const *e)
Check if the given element is currently part of a list.
Definition hlist:96
void push_front(T *e)
Add element to the front of the list.
Definition hlist:109
Our C++ library.
Definition arith:11
H_list_item_t< void > H_list_item
Untyped list item.
Definition hlist:61
Double-linked list of typed H_list_item_t elements.
Definition hlist:249