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