MySQL 8.0.33
Source Code Documentation
visible_fields.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2020, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_VISIBLE_FIELDS_H
25#define SQL_VISIBLE_FIELDS_H
26
27/**
28 @file
29 An adapter class to support iteration over an iterator of Item *
30 (typically mem_root_deque<Item *>), while skipping over items that
31 are hidden (item->hidden == true). This is such a common operation
32 that it warrants having its own adapter. You can either do
33
34 for (Item *item : VisibleFields(fields))
35
36 or use select->visible_fields().
37
38 Behavior is undefined if you modify the hidden flag of an item during
39 iteration.
40
41 TODO(sgunders): When we get C++20, replace with an std::views::filter.
42 */
43
44#include <iterator>
45
46#include "mem_root_deque.h"
47#include "sql/item.h"
48
49template <class Iterator>
51 public:
52 VisibleFieldsAdapter(Iterator base, Iterator end) : m_it(base), m_end(end) {
53 while (m_it != m_end && (*m_it)->hidden) {
54 ++m_it;
55 }
56 }
57
58 // Pre-increment.
60 do {
61 ++m_it;
62 } while (m_it != m_end && (*m_it)->hidden);
63 return *this;
64 }
65
66 // Post-increment.
68 VisibleFieldsAdapter ret = *this;
69 do {
70 m_it++;
71 } while (m_it != m_end && (*m_it)->hidden);
72 return ret;
73 }
74
75 auto &operator*() const { return *m_it; }
76
77 bool operator==(const VisibleFieldsAdapter &other) const {
78 return m_it == other.m_it;
79 }
80 bool operator!=(const VisibleFieldsAdapter &other) const {
81 return m_it != other.m_it;
82 }
83
84 // Define aliases needed by std::iterator_traits, to allow using this iterator
85 // type with standard library templates.
86 using difference_type = typename Iterator::difference_type;
88 using pointer = typename Iterator::pointer;
89 using reference = typename Iterator::reference;
90 using iterator_category = std::forward_iterator_tag;
91
92 private:
93 Iterator m_it, m_end;
94};
95
96template <class Container, class Iterator>
98 public:
99 explicit VisibleFieldsContainer(Container &fields) : m_fields(fields) {}
101 return VisibleFieldsAdapter<Iterator>(m_fields.begin(), m_fields.end());
102 }
105 }
106
107 private:
108 Container &m_fields;
109};
110
114}
115
116inline auto VisibleFields(const mem_root_deque<Item *> &fields) {
119}
120
121#endif // SQL_VISIBLE_FIELDS_H
Definition: visible_fields.h:50
VisibleFieldsAdapter & operator++()
Definition: visible_fields.h:59
typename Iterator::difference_type difference_type
Definition: visible_fields.h:86
Iterator m_it
Definition: visible_fields.h:93
VisibleFieldsAdapter operator++(int)
Definition: visible_fields.h:67
typename Iterator::reference reference
Definition: visible_fields.h:89
bool operator==(const VisibleFieldsAdapter &other) const
Definition: visible_fields.h:77
bool operator!=(const VisibleFieldsAdapter &other) const
Definition: visible_fields.h:80
Iterator m_end
Definition: visible_fields.h:93
typename Iterator::pointer pointer
Definition: visible_fields.h:88
VisibleFieldsAdapter(Iterator base, Iterator end)
Definition: visible_fields.h:52
std::forward_iterator_tag iterator_category
Definition: visible_fields.h:90
typename Iterator::value_type value_type
Definition: visible_fields.h:87
auto & operator*() const
Definition: visible_fields.h:75
Definition: visible_fields.h:97
VisibleFieldsContainer(Container &fields)
Definition: visible_fields.h:99
VisibleFieldsAdapter< Iterator > begin()
Definition: visible_fields.h:100
Container & m_fields
Definition: visible_fields.h:108
VisibleFieldsAdapter< Iterator > end()
Definition: visible_fields.h:103
Definition: mem_root_deque.h:286
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
uint16_t value_type
Definition: vt100.h:183
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
auto VisibleFields(mem_root_deque< Item * > &fields)
Definition: visible_fields.h:111