MySQL  8.0.27
Source Code Documentation
intrusive_list_iterator.h
Go to the documentation of this file.
1 #ifndef SQL_INTRUSIVE_LIST_ITERATOR_H_
2 #define SQL_INTRUSIVE_LIST_ITERATOR_H_
3 /* Copyright (c) 2019, 2021, Oracle and/or its affiliates.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation. The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24 
25 /**
26  @file intrusive_list_iterator.h
27 
28  Iterator utilities for working with intrusive pointers.
29 */
30 
31 #include <assert.h>
32 
33 /**
34  An iterator that follows a 'next' pointer with an accessor function.
35  @tparam T The type of the object holding the intrusive list.
36  @tparam GetNextPointer The accessor function, returning a pointer to the
37  next object in the list.
38 
39  @note Due to the nature of intrusive 'next' pointers it's not possible to
40  free an intrusive pointee while iterating over an intrusive list with
41  the pre-increment operator, as the enhanced for-loop does, e.g.
42 
43  ```
44  for(auto elem : elems)
45  delete *elem;
46  ```
47 
48  Will cause a core dump. However, the following is possible:
49 
50  ```
51  auto it = container.begin();
52  while(it != container.end()) delete *(it++);
53  ```
54 */
55 template <typename T, T *(*GetNextPointer)(const T *)>
57  public:
58  using value_type = T *;
59  /**
60  Constructs an iterator.
61 
62  @param start The object that the iterator will start iterating
63  from.
64  */
66 
67  /// Constructs a past-the-end iterator.
69 
71  assert(m_current != nullptr);
72  m_current = GetNextPointer(m_current);
73  return *this;
74  }
75 
77  auto pre_increment(*this);
78  ++(*this);
79  return pre_increment;
80  }
81 
82  T *operator*() const { return m_current; }
83 
84  bool operator==(const NextFunctionIterator &other) const {
85  return m_current == other.m_current;
86  }
87 
88  bool operator!=(const NextFunctionIterator &other) const {
89  return !((*this) == other);
90  }
91 
92  private:
94 };
95 
96 /**
97  Helper template for the case when the 'next' member can be used directly,
98  typically when it's public and the class definition is known.
99 */
100 template <typename T, T *T::*Member>
101 T *GetMember(const T *t) {
102  return t->*Member;
103 }
104 
105 /**
106  An iterator that follows the 'next' pointer in an intrusive list.
107  Conforms to the ForwardIterator named requirement.
108 
109  @tparam T The type of the object holding the intrusive list.
110  @tparam NextPointer The intrusive list's "next" pointer member.
111 */
112 template <typename T, T *T::*NextPointer>
114  : public NextFunctionIterator<T, GetMember<T, NextPointer>> {
115  public:
117  explicit IntrusiveListIterator(T *t)
118  : NextFunctionIterator<T, GetMember<T, NextPointer>>(t) {}
119 };
120 
121 /**
122  Adds a collection interface on top of an iterator. The iterator must support a
123  default constructor constructing a past-the-end iterator.
124 
125  @tparam IteratorType The iterator's class.
126 */
127 template <typename IteratorType>
129  public:
130  using Type = typename IteratorType::value_type;
131  explicit IteratorContainer(Type first) : m_first(first) {}
132 
133  IteratorType begin() { return IteratorType(m_first); }
134  IteratorType end() { return IteratorType(); }
135 
136  private:
138 };
139 
140 template <typename T>
141 using GetNextPointerFunction = T *(*)(const T *);
142 
143 /**
144  Convenience alias for instantiating a container directly from the accessor
145  function.
146 */
147 template <typename T, GetNextPointerFunction<T> Fn>
149 
150 /*
151  We inline the NextFunctionContainer definition below. We want to define this
152  alias as NextFunctionContainer<T, &GetMember<T, NextPointer>>, but VS2019
153  fails with C2996. It is likely a compiler bug.
154 */
155 template <typename T, T *T::*NextPointer>
158 
159 #endif // SQL_INTRUSIVE_LIST_ITERATOR_H_
An iterator that follows the 'next' pointer in an intrusive list.
Definition: intrusive_list_iterator.h:114
IntrusiveListIterator(T *t)
Definition: intrusive_list_iterator.h:117
IntrusiveListIterator()=default
Adds a collection interface on top of an iterator.
Definition: intrusive_list_iterator.h:128
Type m_first
Definition: intrusive_list_iterator.h:137
IteratorContainer(Type first)
Definition: intrusive_list_iterator.h:131
IteratorType end()
Definition: intrusive_list_iterator.h:134
typename IteratorType::value_type Type
Definition: intrusive_list_iterator.h:130
IteratorType begin()
Definition: intrusive_list_iterator.h:133
An iterator that follows a 'next' pointer with an accessor function.
Definition: intrusive_list_iterator.h:56
bool operator!=(const NextFunctionIterator &other) const
Definition: intrusive_list_iterator.h:88
bool operator==(const NextFunctionIterator &other) const
Definition: intrusive_list_iterator.h:84
T * value_type
Definition: intrusive_list_iterator.h:58
T * m_current
Definition: intrusive_list_iterator.h:93
NextFunctionIterator operator++(int)
Definition: intrusive_list_iterator.h:76
T * operator*() const
Definition: intrusive_list_iterator.h:82
NextFunctionIterator(T *start)
Constructs an iterator.
Definition: intrusive_list_iterator.h:65
NextFunctionIterator & operator++()
Definition: intrusive_list_iterator.h:70
NextFunctionIterator()
Constructs a past-the-end iterator.
Definition: intrusive_list_iterator.h:68
Dialog Client Authentication nullptr
Definition: dialog.cc:352
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:165
T * GetMember(const T *t)
Helper template for the case when the 'next' member can be used directly, typically when it's public ...
Definition: intrusive_list_iterator.h:101
T *(*)(const T *) GetNextPointerFunction
Definition: intrusive_list_iterator.h:141
uint16_t value_type
Definition: vt100.h:182