MySQL 8.4.0
Source Code Documentation
visible_fields.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
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#ifndef SQL_VISIBLE_FIELDS_H
26#define SQL_VISIBLE_FIELDS_H
27
28/**
29 @file
30 An adapter class to support iteration over an iterator of Item *
31 (typically mem_root_deque<Item *>), while skipping over items that
32 are hidden (item->hidden == true). This is such a common operation
33 that it warrants having its own adapter. You can either do
34
35 for (Item *item : VisibleFields(fields))
36
37 or use select->visible_fields().
38
39 Behavior is undefined if you modify the hidden flag of an item during
40 iteration.
41
42 TODO(sgunders): When we get C++20, replace with an std::views::filter.
43 */
44
45#include <iterator>
46
47#include "mem_root_deque.h"
48#include "sql/item.h"
49
50template <class Iterator>
52 public:
53 VisibleFieldsAdapter(Iterator base, Iterator end) : m_it(base), m_end(end) {
54 while (m_it != m_end && (*m_it)->hidden) {
55 ++m_it;
56 }
57 }
58
59 // Pre-increment.
61 do {
62 ++m_it;
63 } while (m_it != m_end && (*m_it)->hidden);
64 return *this;
65 }
66
67 // Post-increment.
69 VisibleFieldsAdapter ret = *this;
70 do {
71 m_it++;
72 } while (m_it != m_end && (*m_it)->hidden);
73 return ret;
74 }
75
76 auto &operator*() const { return *m_it; }
77
78 bool operator==(const VisibleFieldsAdapter &other) const {
79 return m_it == other.m_it;
80 }
81 bool operator!=(const VisibleFieldsAdapter &other) const {
82 return m_it != other.m_it;
83 }
84
85 // Define aliases needed by std::iterator_traits, to allow using this iterator
86 // type with standard library templates.
87 using difference_type = typename Iterator::difference_type;
89 using pointer = typename Iterator::pointer;
90 using reference = typename Iterator::reference;
91 using iterator_category = std::forward_iterator_tag;
92
93 private:
94 Iterator m_it, m_end;
95};
96
97template <class Container, class Iterator>
99 public:
100 explicit VisibleFieldsContainer(Container &fields) : m_fields(fields) {}
102 return VisibleFieldsAdapter<Iterator>(m_fields.begin(), m_fields.end());
103 }
106 }
107
108 private:
109 Container &m_fields;
110};
111
115}
116
117inline auto VisibleFields(const mem_root_deque<Item *> &fields) {
120}
121
122#endif // SQL_VISIBLE_FIELDS_H
Definition: visible_fields.h:51
VisibleFieldsAdapter & operator++()
Definition: visible_fields.h:60
typename Iterator::difference_type difference_type
Definition: visible_fields.h:87
Iterator m_it
Definition: visible_fields.h:94
VisibleFieldsAdapter operator++(int)
Definition: visible_fields.h:68
typename Iterator::reference reference
Definition: visible_fields.h:90
bool operator==(const VisibleFieldsAdapter &other) const
Definition: visible_fields.h:78
bool operator!=(const VisibleFieldsAdapter &other) const
Definition: visible_fields.h:81
Iterator m_end
Definition: visible_fields.h:94
typename Iterator::pointer pointer
Definition: visible_fields.h:89
VisibleFieldsAdapter(Iterator base, Iterator end)
Definition: visible_fields.h:53
std::forward_iterator_tag iterator_category
Definition: visible_fields.h:91
typename Iterator::value_type value_type
Definition: visible_fields.h:88
auto & operator*() const
Definition: visible_fields.h:76
Definition: visible_fields.h:98
VisibleFieldsContainer(Container &fields)
Definition: visible_fields.h:100
VisibleFieldsAdapter< Iterator > begin()
Definition: visible_fields.h:101
Container & m_fields
Definition: visible_fields.h:109
VisibleFieldsAdapter< Iterator > end()
Definition: visible_fields.h:104
Definition: mem_root_deque.h:288
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:111
uint16_t value_type
Definition: vt100.h:184
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
auto VisibleFields(mem_root_deque< Item * > &fields)
Definition: visible_fields.h:112