L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
slist
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 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
9 *
10 * As a special exception, you may use this file as part of a free software
11 * library without restriction. Specifically, if other files instantiate
12 * templates or use macros or inline functions from this file, or you compile
13 * this file and link it with other files to produce an executable, this
14 * file does not by itself cause the resulting executable to be covered by
15 * the GNU General Public License. This exception does not however
16 * invalidate any other reasons why the executable file might be covered by
17 * the GNU General Public License.
18 */
19
20#pragma once
21
22#include "bits/list_basics.h"
23
24namespace cxx {
25
26class S_list_item
27{
28public:
29 S_list_item() : _n(0) {}
30 explicit S_list_item(bool) {}
31
32private:
33 template<typename T, typename P> friend class S_list;
34 template<typename T, typename P> friend class S_list_tail;
35 template<typename T, typename X> friend struct Bits::Basic_list_policy;
36
37 S_list_item(S_list_item const &);
38 void operator = (S_list_item const &);
39
40 S_list_item *_n;
41};
42
49template< typename T, typename POLICY = Bits::Basic_list_policy< T, S_list_item > >
50class S_list : public Bits::Basic_list<POLICY>
51{
52 S_list(S_list const &) = delete;
53 void operator = (S_list const &) = delete;
54
55private:
56 typedef typename Bits::Basic_list<POLICY> Base;
57
58public:
59 typedef typename Base::Iterator Iterator;
60
61 S_list(S_list &&o) : Base(static_cast<Base&&>(o)) {}
62
63 S_list &operator = (S_list &&o)
64 {
65 Base::operator = (static_cast<Base&&>(o));
66 return *this;
67 }
68
69 // BSS allocation
70 explicit S_list(bool x) : Base(x) {}
71
72 S_list() : Base() {}
73
75 void add(T *e)
76 {
77 e->_n = this->_f;
78 this->_f = e;
79 }
80
81 template< typename CAS >
82 void add(T *e, CAS const &c)
83 {
84 do
85 {
86 e->_n = this->_f;
87 }
88 while (!c(&this->_f, e->_n, e));
89 }
90
92 void push_front(T *e) { add(e); }
93
100 {
101 T *r = this->front();
102 if (this->_f)
103 this->_f = this->_f->_n;
104 return r;
105 }
106
107 void insert(T *e, Iterator const &pred)
108 {
109 S_list_item *p = *pred;
110 e->_n = p->_n;
111 p->_n = e;
112 }
113
114 static void insert_before(T *e, Iterator const &succ)
115 {
116 S_list_item **x = Base::__get_internal(succ);
117
118 e->_n = *x;
119 *x = e;
120 }
121
122 static void replace(Iterator const &p, T*e)
123 {
124 S_list_item **x = Base::__get_internal(p);
125 e->_n = (*x)->_n;
126 *x = e;
127 }
128
129 static Iterator erase(Iterator const &e)
130 {
131 S_list_item **x = Base::__get_internal(e);
132 *x = (*x)->_n;
133 return e;
134 }
135
136};
137
138
139template< typename T >
140class S_list_bss : public S_list<T>
141{
142public:
143 S_list_bss() : S_list<T>(true) {}
144};
145
146template< typename T, typename POLICY = Bits::Basic_list_policy< T, S_list_item > >
147class S_list_tail : public S_list<T, POLICY>
148{
149private:
150 typedef S_list<T, POLICY> Base;
151 void add(T *e) = delete;
152
153public:
154 using Iterator = typename Base::Iterator;
155 S_list_tail() : Base(), _tail(&this->_f) {}
156
157 S_list_tail(S_list_tail &&t)
158 : Base(static_cast<Base&&>(t)), _tail(t.empty() ? &this->_f : t._tail)
159 {
160 t._tail = &t._f;
161 }
162
163 S_list_tail &operator = (S_list_tail &&t)
164 {
165 if (&t == this)
166 return *this;
167
168 Base::operator = (static_cast<Base &&>(t));
169 _tail = t.empty() ? &this->_f : t._tail;
170 t._tail = &t._f;
171 return *this;
172 }
173
174 void push_front(T *e)
175 {
176 if (Base::empty())
177 _tail = &e->_n;
178
180 }
181
182 void push_back(T *e)
183 {
184 e->_n = 0;
185 *_tail = e;
186 _tail = &e->_n;
187 }
188
189 void clear()
190 {
191 Base::clear();
192 _tail = &this->_f;
193 }
194
195 void append(S_list_tail &o)
196 {
197 T *x = o.front();
198 *_tail = x;
199 if (x)
200 _tail = o._tail;
201 o.clear();
202 }
203
204 T *pop_front()
205 {
206 T *t = Base::pop_front();
207 if (t && Base::empty())
208 _tail = &this->_f;
209 return t;
210 }
211
212private:
213 static void insert(T *e, Iterator const &pred);
214 static void insert_before(T *e, Iterator const &succ);
215 static void replace(Iterator const &p, T*e);
216 static Iterator erase(Iterator const &e);
217
218private:
219 S_list_item **_tail;
220};
221
222}
Internal: Common functions for all head-based list implementations.
Definition list_basics.h:51
bool empty() const
Check if the list is empty.
void clear()
Remove all elements from the list.
POLICY::Head_type _f
Pointer to front of the list.
Value_type front() const
Return the first element in the list.
Simple single-linked list.
Definition slist:51
void add(T *e)
Add an element to the front of the list.
Definition slist:75
void push_front(T *e)
Add an element to the front of the list.
Definition slist:92
T * pop_front()
Remove and return the head element of the list.
Definition slist:99
Our C++ library.
Definition arith:22