L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
list_basics.h
1/*
2 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7
8#pragma once
9
10namespace cxx { namespace Bits {
11
12template< typename T >
13class List_iterator_end_ptr
14{
15private:
16 template< typename U > friend class Basic_list;
17 static void * const _end;
18};
19
20template< typename T >
21void * const List_iterator_end_ptr<T>::_end = nullptr;
22
23template< typename VALUE_T, typename TYPE >
24struct Basic_list_policy
25{
26 typedef VALUE_T *Value_type;
27 typedef VALUE_T const *Const_value_type;
28 typedef TYPE **Type;
29 typedef TYPE *Const_type;
30 typedef TYPE *Head_type;
31 typedef TYPE Item_type;
32
33 static Type next(Type c) { return &(*c)->_n; }
34 static Const_type next(Const_type c) { return c->_n; }
35};
36
38template< typename POLICY >
39class Basic_list
40{
41 Basic_list(Basic_list const &) = delete;
42 void operator = (Basic_list const &) = delete;
43
44public:
45 typedef typename POLICY::Value_type Value_type;
46 typedef typename POLICY::Const_value_type Const_value_type;
47
48 class Iterator
49 {
50 private:
51 typedef typename POLICY::Type Internal_type;
52
53 public:
54 typedef typename POLICY::Value_type value_type;
55 typedef typename POLICY::Value_type Value_type;
56
57 Value_type operator * () const { return static_cast<Value_type>(*_c); }
58 Value_type operator -> () const { return static_cast<Value_type>(*_c); }
59 Iterator operator ++ () { _c = POLICY::next(_c); return *this; }
60
61 friend bool operator == (Iterator const &lhs, Iterator const &rhs)
62 { return *lhs._c == *rhs._c; }
63 friend bool operator != (Iterator const &lhs, Iterator const &rhs)
64 { return *lhs._c != *rhs._c; }
65
66 Iterator() : _c(__end()) {}
67
68 private:
69 friend class Basic_list;
70 static Internal_type __end()
71 {
72 union X { Internal_type l; void * const *v; } z;
73 z.v = &Bits::List_iterator_end_ptr<void>::_end;
74 return z.l;
75 }
76
77 explicit Iterator(Internal_type i) : _c(i) {}
78
79 Internal_type _c;
80 };
81
82 class Const_iterator
83 {
84 private:
85 typedef typename POLICY::Const_type Internal_type;
86
87 public:
88 typedef typename POLICY::Value_type value_type;
89 typedef typename POLICY::Value_type Value_type;
90
91 Value_type operator * () const { return static_cast<Value_type>(_c); }
92 Value_type operator -> () const { return static_cast<Value_type>(_c); }
93 Const_iterator operator ++ () { _c = POLICY::next(_c); return *this; }
94
95 friend bool operator == (Const_iterator const &lhs, Const_iterator const &rhs)
96 { return lhs._c == rhs._c; }
97 friend bool operator != (Const_iterator const &lhs, Const_iterator const &rhs)
98 { return lhs._c != rhs._c; }
99
100 Const_iterator() {}
101 Const_iterator(Iterator const &o) : _c(*o) {}
102
103 private:
104 friend class Basic_list;
105
106 explicit Const_iterator(Internal_type i) : _c(i) {}
107
108 Internal_type _c;
109 };
110
111 // BSS allocation
112 explicit Basic_list(bool) {}
113 constexpr Basic_list() : _f(nullptr) {}
114
115 Basic_list(Basic_list &&o) : _f(o._f)
116 {
117 o.clear();
118 }
119
120 Basic_list &operator = (Basic_list &&o) &
121 {
122 if (&o != this)
123 {
124 _f = o._f;
125 o.clear();
126 }
127
128 return *this;
129 }
130
132 bool empty() const { return !_f; }
134 Value_type front() const { return static_cast<Value_type>(_f); }
135
141 void clear() { _f = nullptr; }
142
144 Iterator begin() { return Iterator(&_f); }
146 Const_iterator begin() const { return Const_iterator(_f); }
154 static Const_iterator iter(Const_value_type c) { return Const_iterator(c); }
156 Const_iterator end() const { return Const_iterator(nullptr); }
158 Iterator end() { return Iterator(); }
159
160protected:
161 static typename POLICY::Type __get_internal(Iterator const &i) { return i._c; }
162 static Iterator __iter(typename POLICY::Type c) { return Iterator(c); }
163
165 typename POLICY::Head_type _f;
166};
167
168}}
169
Const_iterator end() const
Return a const iterator to the end of the list.
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.
Const_iterator begin() const
Return a const iterator to the beginning of the list.
Iterator end()
Return an iterator to the end of the list.
static Const_iterator iter(Const_value_type c)
Return a const iterator that begins at the given element.
Iterator begin()
Return an iterator to the beginning of the list.
Value_type front() const
Return the first element in the list.
Internal helpers for the cxx package.
Definition avl_map:21
Our C++ library.
Definition arith:11