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 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
8 *
9 * As a special exception, you may use this file as part of a free software
10 * library without restriction. Specifically, if other files instantiate
11 * templates or use macros or inline functions from this file, or you compile
12 * this file and link it with other files to produce an executable, this
13 * file does not by itself cause the resulting executable to be covered by
14 * the GNU General Public License. This exception does not however
15 * invalidate any other reasons why the executable file might be covered by
16 * the GNU General Public License.
17 */
18
19#pragma once
20
21namespace cxx { namespace Bits {
22
23template< typename T >
24class List_iterator_end_ptr
25{
26private:
27 template< typename U > friend class Basic_list;
28 static void *_end;
29};
30
31template< typename T >
32void *List_iterator_end_ptr<T>::_end;
33
34template< typename VALUE_T, typename TYPE >
35struct Basic_list_policy
36{
37 typedef VALUE_T *Value_type;
38 typedef VALUE_T const *Const_value_type;
39 typedef TYPE **Type;
40 typedef TYPE *Const_type;
41 typedef TYPE *Head_type;
42 typedef TYPE Item_type;
43
44 static Type next(Type c) { return &(*c)->_n; }
45 static Const_type next(Const_type c) { return c->_n; }
46};
47
49template< typename POLICY >
51{
52 Basic_list(Basic_list const &) = delete;
53 void operator = (Basic_list const &) = delete;
54
55public:
56 typedef typename POLICY::Value_type Value_type;
57 typedef typename POLICY::Const_value_type Const_value_type;
58
59 class Iterator
60 {
61 private:
62 typedef typename POLICY::Type Internal_type;
63
64 public:
65 typedef typename POLICY::Value_type value_type;
66 typedef typename POLICY::Value_type Value_type;
67
68 Value_type operator * () const { return static_cast<Value_type>(*_c); }
69 Value_type operator -> () const { return static_cast<Value_type>(*_c); }
70 Iterator operator ++ () { _c = POLICY::next(_c); return *this; }
71
72 bool operator == (Iterator const &o) const { return *_c == *o._c; }
73 bool operator != (Iterator const &o) const { return !operator == (o); }
74
75 Iterator() : _c(__end()) {}
76
77 private:
78 friend class Basic_list;
79 static Internal_type __end()
80 {
81 union X { Internal_type l; void **v; } z;
82 z.v = &Bits::List_iterator_end_ptr<void>::_end;
83 return z.l;
84 }
85
86 explicit Iterator(Internal_type i) : _c(i) {}
87
88 Internal_type _c;
89 };
90
91 class Const_iterator
92 {
93 private:
94 typedef typename POLICY::Const_type Internal_type;
95
96 public:
97 typedef typename POLICY::Value_type value_type;
98 typedef typename POLICY::Value_type Value_type;
99
100 Value_type operator * () const { return static_cast<Value_type>(_c); }
101 Value_type operator -> () const { return static_cast<Value_type>(_c); }
102 Const_iterator operator ++ () { _c = POLICY::next(_c); return *this; }
103
104 friend bool operator == (Const_iterator const &lhs, Const_iterator const &rhs)
105 { return lhs._c == rhs._c; }
106 friend bool operator != (Const_iterator const &lhs, Const_iterator const &rhs)
107 { return lhs._c != rhs._c; }
108
109 Const_iterator() {}
110 Const_iterator(Iterator const &o) : _c(*o) {}
111
112 private:
113 friend class Basic_list;
114
115 explicit Const_iterator(Internal_type i) : _c(i) {}
116
117 Internal_type _c;
118 };
119
120 // BSS allocation
121 explicit Basic_list(bool) {}
122 Basic_list() : _f(0) {}
123
124 Basic_list(Basic_list &&o) : _f(o._f)
125 {
126 o.clear();
127 }
128
129 Basic_list &operator = (Basic_list &&o)
130 {
131 _f = o._f;
132 o.clear();
133 return *this;
134 }
135
137 bool empty() const { return !_f; }
139 Value_type front() const { return static_cast<Value_type>(_f); }
140
146 void clear() { _f = 0; }
147
149 Iterator begin() { return Iterator(&_f); }
151 Const_iterator begin() const { return Const_iterator(_f); }
159 static Const_iterator iter(Const_value_type c) { return Const_iterator(c); }
161 Const_iterator end() const { return Const_iterator(nullptr); }
163 Iterator end() { return Iterator(); }
164
165protected:
166 static typename POLICY::Type __get_internal(Iterator const &i) { return i._c; }
167 static Iterator __iter(typename POLICY::Type c) { return Iterator(c); }
168
170 typename POLICY::Head_type _f;
171};
172
173}}
174
Internal: Common functions for all head-based list implementations.
Definition list_basics.h:51
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:22