L4Re - L4 Runtime Environment
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  * 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 #include "type_traits"
24 
25 namespace cxx {
26 
32 template<typename ELEM_TYPE>
34 {
35 public:
41  H_list_item_t() : _pn(0) {}
48  ~H_list_item_t() noexcept { l_remove(); }
49 
50 private:
51  H_list_item_t(H_list_item_t const &) = delete;
52 
53  template<typename T, typename P> friend class H_list;
54  template<typename T, typename X> friend struct Bits::Basic_list_policy;
55 
56  void l_remove() noexcept
57  {
58  if (!_pn)
59  return;
60 
61  *_pn = _n;
62  if (_n)
63  _n->_pn = _pn;
64 
65  _pn = 0;
66  }
67 
68  H_list_item_t *_n, **_pn;
69 };
70 
72 
78 template< typename T, typename POLICY = Bits::Basic_list_policy< T, H_list_item> >
79 class H_list : public Bits::Basic_list<POLICY>
80 {
81 private:
82  typedef typename POLICY::Item_type Item;
84  H_list(H_list const &);
85  void operator = (H_list const &);
86 
87 public:
88  typedef typename Base::Iterator Iterator;
89 
90  // BSS allocation
91  explicit H_list(bool x) : Base(x) {}
92  H_list() : Base() {}
93 
103  static Iterator iter(T *c) { return Base::__iter(c->Item::_pn); }
104 
106  static bool in_list(T const *e) { return e->Item::_pn; }
107 
109  void add(T *e)
110  {
111  if (this->_f)
112  this->_f->_pn = &e->Item::_n;
113  e->Item::_n = this->_f;
114  e->Item::_pn = &this->_f;
115  this->_f = static_cast<Item*>(e);
116  }
117 
119  void push_front(T *e) { add(e); }
120 
127  {
128  T *r = this->front();
129  remove(r);
130  return r;
131  }
132 
143  Iterator insert(T *e, Iterator const &pred)
144  {
145  Item **x = &this->_f;
146  if (pred != Base::end())
147  x = &(*pred)->_n;
148 
149  e->Item::_n = *x;
150 
151  if (*x)
152  (*x)->_pn = &(e->Item::_n);
153 
154  e->Item::_pn = x;
155  *x = static_cast<Item*>(e);
156  return iter(e);
157  }
158 
170  static Iterator insert_after(T *e, Iterator const &pred)
171  {
172  Item **x = &(*pred)->_n;
173  e->Item::_n = *x;
174 
175  if (*x)
176  (*x)->_pn = &(e->Item::_n);
177 
178  e->Item::_pn = x;
179  *x = static_cast<Item*>(e);
180  return iter(e);
181  }
182 
190  static void insert_before(T *e, Iterator const &succ)
191  {
192  Item **x = Base::__get_internal(succ);
193 
194  e->Item::_n = *x;
195  e->Item::_pn = x;
196 
197  if (*x)
198  (*x)->_pn = &e->Item::_n;
199 
200  *x = static_cast<Item*>(e);
201  }
202 
212  static void replace(T *p, T *e)
213  {
214  e->Item::_n = p->Item::_n;
215  e->Item::_pn = p->Item::_pn;
216  *(p->Item::_pn) = static_cast<Item*>(e);
217  if (e->Item::_n)
218  e->Item::_n->_pn = &(e->Item::_n);
219 
220  p->Item::_pn = 0;
221  }
222 
228  static void remove(T *e)
229  { e->Item::l_remove(); }
230 
244  static Iterator erase(Iterator const &e)
245  { e->Item::l_remove(); return e; }
246 };
247 
255 template< typename T >
256 struct H_list_t : H_list<T, Bits::Basic_list_policy< T, H_list_item_t<T> > >
257 {
258  H_list_t() = default;
259  H_list_t(bool b)
261  {};
262 };
263 
264 template< typename T >
265 class H_list_bss : public H_list<T>
266 {
267 public:
268  H_list_bss() : H_list<T>(true) {}
269 };
270 
271 template< typename T >
272 class H_list_t_bss : public H_list_t<T>
273 {
274 public:
275  H_list_t_bss() : H_list_t<T>(true) {}
276 };
277 
278 
279 }
General double-linked list of unspecified cxx::H_list_item elements.
Definition: hlist:79
static bool in_list(T const *e)
Check if the given element is currently part of a list.
Definition: hlist:106
Our C++ library.
Definition: arith:22
static void replace(T *p, T *e)
Replace an element in a list with a new element.
Definition: hlist:212
Double-linked list of typed H_list_item_t elements.
Definition: hlist:256
Internal: Common functions for all head-based list implementations.
Definition: list_basics.h:50
void add(T *e)
Add element to the front of the list.
Definition: hlist:109
static void insert_before(T *e, Iterator const &succ)
Insert an element before the iterator position.
Definition: hlist:190
static Iterator insert_after(T *e, Iterator const &pred)
Insert an element after the iterator position.
Definition: hlist:170
Iterator insert(T *e, Iterator const &pred)
Insert an element at the iterator position.
Definition: hlist:143
~H_list_item_t() noexcept
Destructor.
Definition: hlist:48
static Iterator erase(Iterator const &e)
Remove the element at the given iterator position.
Definition: hlist:244
static Iterator iter(T *c)
Return an iterator for an arbitrary list element.
Definition: hlist:103
Basic element type for a double-linked H_list.
Definition: hlist:33
T * pop_front()
Remove and return the head element of the list.
Definition: hlist:126
void push_front(T *e)
Add element to the front of the list.
Definition: hlist:119
H_list_item_t()
Constructor.
Definition: hlist:41