MySQL 8.3.0
Source Code Documentation
sql_array.h
Go to the documentation of this file.
1#ifndef SQL_ARRAY_INCLUDED
2#define SQL_ARRAY_INCLUDED
3
4/* Copyright (c) 2005, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
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#include <assert.h>
27#include <array>
28
29#include "my_alloc.h"
30
31/**
32 A wrapper class which provides array bounds checking.
33 We do *not* own the array, we simply have a pointer to the first element,
34 and a length.
35
36 @remark
37 We want the compiler-generated versions of:
38 - the copy CTOR (memberwise initialization)
39 - the assignment operator (memberwise assignment)
40
41 This is roughly analogous to C++20's std::span.
42
43 @tparam Element_type The type of the elements of the container.
44 */
45template <typename Element_type>
47 public:
48 // Convenience typedef, same typedef name as std::vector
49 typedef Element_type value_type;
50
52
53 Bounds_checked_array(Element_type *el, size_t size_arg)
54 : m_array(el), m_size(size_arg) {}
55
56 // NOTE: non-const reference intentional; mirrors std::span's constructor.
57 template <class T, size_t N>
58 explicit Bounds_checked_array(std::array<T, N> &arr)
59 : m_array(arr.data()), m_size(arr.size()) {}
60
61 // Not a constructor because it does something else from the other
62 // constructors (allocates new memory instead of wrapping existing memory),
63 // and also because nullptr for the first argument be ambiguous. The latter
64 // could be solved with an explicit nullptr_t overload, though.
65 //
66 // The elements of the array are initialized with value initialization. For
67 // primitive types, like int and pointers, this means the elements will be set
68 // to the equivalent of 0 (or false or nullptr).
70 return {mem_root->ArrayAlloc<Element_type>(size), size};
71 }
72
73 /// Make a copy of '*this'. Allocate memory for m_array on 'mem_root'.
75 if (data() == nullptr) {
76 return {};
77 } else {
79 if (duplicate.m_array != nullptr) {
80 std::copy(cbegin(), cend(), duplicate.begin());
81 }
82 return duplicate;
83 }
84 }
85
86 void reset() {
87 m_array = nullptr;
88 m_size = 0;
89 }
90
91 void reset(Element_type *array, size_t size) {
92 m_array = array;
93 m_size = size;
94 }
95
96 /**
97 Set a new bound on the array. Does not resize the underlying
98 array, so the new size must be smaller than or equal to the
99 current size.
100 */
101 void resize(size_t new_size) {
102 assert(new_size <= m_size);
103 m_size = new_size;
104 }
105
106 /**
107 Like resize(), but returns a new view of the array without modifying
108 this one.
109 */
110 Bounds_checked_array prefix(size_t new_size) {
111 assert(new_size <= m_size);
112 return Bounds_checked_array(m_array, new_size);
113 }
114
115 Element_type *data() { return m_array; }
116
117 const Element_type *data() const { return m_array; }
118
119 Element_type &operator[](size_t n) {
120 assert(n < m_size);
121 return m_array[n];
122 }
123
124 const Element_type &operator[](size_t n) const {
125 assert(n < m_size);
126 return m_array[n];
127 }
128
129 typedef Element_type *iterator;
130 typedef const Element_type *const_iterator;
131
132 /// begin : Returns a pointer to the first element in the array.
133 iterator begin() { return m_array; }
134 /// end : Returns a pointer to the past-the-end element in the array.
135 iterator end() { return m_array + size(); }
136
137 /// begin : Returns a pointer to the first element in the array.
138 const_iterator begin() const { return m_array; }
139 /// end : Returns a pointer to the past-the-end element in the array.
140 const_iterator end() const { return m_array + size(); }
141
142 /// Returns a pointer to the first element in the array.
143 const_iterator cbegin() const { return m_array; }
144 /// Returns a pointer to the past-the-end element in the array.
145 const_iterator cend() const { return m_array + size(); }
146
148 assert(m_size > 0);
150 }
151
152 size_t element_size() const { return sizeof(Element_type); }
153 size_t size() const { return m_size; }
154 bool empty() const { return m_size == 0; }
155
156 bool is_null() const { return m_array == nullptr; }
157
158 void pop_front() {
159 assert(m_size > 0);
160 m_array += 1;
161 m_size -= 1;
162 }
163
164 Element_type *array() const { return m_array; }
165
167 return m_array == rhs.m_array && m_size == rhs.m_size;
168 }
170 return m_array != rhs.m_array || m_size != rhs.m_size;
171 }
172
173 private:
174 Element_type *m_array;
175 size_t m_size;
176};
177
178template <typename Element_type>
181}
182
183#endif /* SQL_ARRAY_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:250
A wrapper class which provides array bounds checking.
Definition: sql_array.h:46
Element_type * data()
Definition: sql_array.h:115
const Element_type * data() const
Definition: sql_array.h:117
Bounds_checked_array(std::array< T, N > &arr)
Definition: sql_array.h:58
Element_type & operator[](size_t n)
Definition: sql_array.h:119
Element_type value_type
Definition: sql_array.h:49
Element_type * m_array
Definition: sql_array.h:174
bool operator!=(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:169
static Bounds_checked_array Alloc(MEM_ROOT *mem_root, size_t size)
Definition: sql_array.h:69
bool is_null() const
Definition: sql_array.h:156
Element_type * array() const
Definition: sql_array.h:164
void resize(size_t new_size)
Set a new bound on the array.
Definition: sql_array.h:101
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:133
iterator end()
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:135
Bounds_checked_array(Element_type *el, size_t size_arg)
Definition: sql_array.h:53
const_iterator end() const
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:140
const_iterator cbegin() const
Returns a pointer to the first element in the array.
Definition: sql_array.h:143
size_t size() const
Definition: sql_array.h:153
size_t element_size() const
Definition: sql_array.h:152
const Element_type & operator[](size_t n) const
Definition: sql_array.h:124
void pop_front()
Definition: sql_array.h:158
const_iterator begin() const
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:138
Bounds_checked_array Clone(MEM_ROOT *mem_root) const
Make a copy of '*this'. Allocate memory for m_array on 'mem_root'.
Definition: sql_array.h:74
Bounds_checked_array()
Definition: sql_array.h:51
Bounds_checked_array without_back() const
Definition: sql_array.h:147
void reset()
Definition: sql_array.h:86
bool empty() const
Definition: sql_array.h:154
const Element_type * const_iterator
Definition: sql_array.h:130
Element_type * iterator
Definition: sql_array.h:129
size_t m_size
Definition: sql_array.h:175
void reset(Element_type *array, size_t size)
Definition: sql_array.h:91
bool operator==(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:166
Bounds_checked_array prefix(size_t new_size)
Like resize(), but returns a new view of the array without modifying this one.
Definition: sql_array.h:110
const_iterator cend() const
Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:145
static MEM_ROOT mem_root
Definition: client_plugin.cc:113
const char * p
Definition: ctype-mb.cc:1234
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
Bounds_checked_array< Element_type > make_array(Element_type *p, size_t n)
Definition: sql_array.h:179
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
T * ArrayAlloc(size_t num, Args... args)
Allocate 'num' objects of type T, and initialize them to a default value that is created by passing t...
Definition: my_alloc.h:179
int n
Definition: xcom_base.cc:508