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