L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 *_end;
18};
19
20template< typename T >
21void *List_iterator_end_ptr<T>::_end;
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 >
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 bool operator == (Iterator const &o) const { return *_c == *o._c; }
62 bool operator != (Iterator const &o) const { return !operator == (o); }
63
64 Iterator() : _c(__end()) {}
65
66 private:
67 friend class Basic_list;
68 static Internal_type __end()
69 {
70 union X { Internal_type l; void **v; } z;
71 z.v = &Bits::List_iterator_end_ptr<void>::_end;
72 return z.l;
73 }
74
75 explicit Iterator(Internal_type i) : _c(i) {}
76
77 Internal_type _c;
78 };
79
80 class Const_iterator
81 {
82 private:
83 typedef typename POLICY::Const_type Internal_type;
84
85 public:
86 typedef typename POLICY::Value_type value_type;
87 typedef typename POLICY::Value_type Value_type;
88
89 Value_type operator * () const { return static_cast<Value_type>(_c); }
90 Value_type operator -> () const { return static_cast<Value_type>(_c); }
91 Const_iterator operator ++ () { _c = POLICY::next(_c); return *this; }
92
93 friend bool operator == (Const_iterator const &lhs, Const_iterator const &rhs)
94 { return lhs._c == rhs._c; }
95 friend bool operator != (Const_iterator const &lhs, Const_iterator const &rhs)
96 { return lhs._c != rhs._c; }
97
98 Const_iterator() {}
99 Const_iterator(Iterator const &o) : _c(*o) {}
100
101 private:
102 friend class Basic_list;
103
104 explicit Const_iterator(Internal_type i) : _c(i) {}
105
106 Internal_type _c;
107 };
108
109 // BSS allocation
110 explicit Basic_list(bool) {}
111 Basic_list() : _f(0) {}
112
113 Basic_list(Basic_list &&o) : _f(o._f)
114 {
115 o.clear();
116 }
117
118 Basic_list &operator = (Basic_list &&o)
119 {
120 _f = o._f;
121 o.clear();
122 return *this;
123 }
124
126 bool empty() const { return !_f; }
128 Value_type front() const { return static_cast<Value_type>(_f); }
129
135 void clear() { _f = 0; }
136
138 Iterator begin() { return Iterator(&_f); }
140 Const_iterator begin() const { return Const_iterator(_f); }
148 static Const_iterator iter(Const_value_type c) { return Const_iterator(c); }
150 Const_iterator end() const { return Const_iterator(nullptr); }
152 Iterator end() { return Iterator(); }
153
154protected:
155 static typename POLICY::Type __get_internal(Iterator const &i) { return i._c; }
156 static Iterator __iter(typename POLICY::Type c) { return Iterator(c); }
157
159 typename POLICY::Head_type _f;
160};
161
162}}
163
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.
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.
Our C++ library.
Definition arith:11