L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
smart_ptr_list.h
Go to the documentation of this file.
1
5/*
6 * Copyright (C) 2018, 2022 Kernkonzept GmbH.
7 * Author(s): Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
8 *
9 * This file is distributed under the terms of the GNU General Public
10 * License, version 2. Please see the COPYING-GPL-2 file for details.
11 */
12#pragma once
13
14#include "../type_traits"
15
16namespace cxx { namespace Bits {
17
27template <typename T, typename STORE_T>
29{
30 using Value_type = T;
31 using Storage_type = STORE_T;
32
33 template<typename U> friend class Smart_ptr_list;
34 Storage_type _n;
35};
36
46template <typename ITEM>
48{
49 using Value_type = typename ITEM::Value_type;
50 using Next_type = typename ITEM::Storage_type;
51
52public:
53 class Iterator
54 {
55 public:
56 Iterator() : _c(nullptr) {}
57
58 Value_type *operator * () const { return _c; }
59 Value_type *operator -> () const { return _c; }
60
61 Iterator operator ++ ()
62 {
63 _c = _c->_n.get();
64 return *this;
65 }
66
67 bool operator == (Iterator const &o) const { return _c == o._c; }
68 bool operator != (Iterator const &o) const { return !operator == (o); }
69
70 private:
71 friend class Smart_ptr_list;
72
73 explicit Iterator(Value_type *i) : _c(i) {}
74
75 Value_type *_c;
76 };
77
78 class Const_iterator
79 {
80 public:
81 Const_iterator() : _c(nullptr) {}
82
83 Value_type const *operator * () const { return _c; }
84 Value_type const *operator -> () const { return _c; }
85
86 Const_iterator operator ++ ()
87 {
88 _c = _c->_n.get();
89 return *this;
90 }
91
92 bool operator == (Const_iterator const &o) const { return _c == o._c; }
93 bool operator != (Const_iterator const &o) const { return !operator == (o); }
94
95 private:
96 friend class Smart_ptr_list;
97
98 explicit Const_iterator(Value_type const *i) : _c(i) {}
99
100 Value_type const *_c;
101 };
102
103 Smart_ptr_list() : _b(&_f) {}
104
106 void push_front(Next_type &&e)
107 {
108 e->_n = cxx::move(this->_f);
109 this->_f = cxx::move(e);
110
111 if (_b == &_f)
112 _b = &(_f->_n);
113 }
114
116 void push_front(Next_type const &e)
117 {
118 e->_n = cxx::move(this->_f);
119 this->_f = e;
120
121 if (_b == &_f)
122 _b = &(_f->_n);
123 }
124
126 void push_back(Next_type &&e)
127 {
128 *_b = cxx::move(e);
129 _b = &((*_b)->_n);
130 }
131
133 void push_back(Next_type const &e)
134 {
135 *_b = e;
136 _b = &((*_b)->_n);
137 }
138
140 Value_type *front() const
141 { return _f.get(); }
142
150 Next_type pop_front()
151 {
152 Next_type ret = cxx::move(_f);
153
154 if (ret)
155 _f = cxx::move(ret->_n);
156
157 if (!_f)
158 _b = &_f;
159
160 return ret;
161 }
162
164 bool empty() const
165 { return !_f; }
166
167 Iterator begin() { return Iterator(_f.get()); }
168 Iterator end() { return Iterator(); }
169
170 Const_iterator begin() const { return Const_iterator(_f.get()); }
171 Const_iterator end() const { return Const_iterator(); }
172
173 Const_iterator cbegin() const { return const_iterator(_f.get()); }
174 Const_iterator cend() const { return Const_iterator(); }
175
176private:
177 Next_type _f;
178 Next_type *_b;
179};
180
181} }
List item for an arbitrary item in a Smart_ptr_list.
List of smart-pointer-managed objects.
Value_type * front() const
Return a pointer to the first element in the list.
Next_type pop_front()
Remove the element in front of the list and return it.
bool empty() const
Check if the list is empty.
void push_front(Next_type &&e)
Add an element to the front of the list.
void push_front(Next_type const &e)
Add an element to the front of the list.
void push_back(Next_type &&e)
Add an element at the end of the list.
void push_back(Next_type const &e)
Add an element at the end of the list.
Our C++ library.
Definition arith:22