MySQL 8.0.32
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, 2022, 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 void reset() {
74 m_array = nullptr;
75 m_size = 0;
76 }
77
78 void reset(Element_type *array, size_t size) {
79 m_array = array;
80 m_size = size;
81 }
82
83 /**
84 Set a new bound on the array. Does not resize the underlying
85 array, so the new size must be smaller than or equal to the
86 current size.
87 */
88 void resize(size_t new_size) {
89 assert(new_size <= m_size);
90 m_size = new_size;
91 }
92
93 /**
94 Like resize(), but returns a new view of the array without modifying
95 this one.
96 */
97 Bounds_checked_array prefix(size_t new_size) {
98 assert(new_size <= m_size);
99 return Bounds_checked_array(m_array, new_size);
100 }
101
102 Element_type *data() { return m_array; }
103
104 const Element_type *data() const { return m_array; }
105
106 Element_type &operator[](size_t n) {
107 assert(n < m_size);
108 return m_array[n];
109 }
110
111 const Element_type &operator[](size_t n) const {
112 assert(n < m_size);
113 return m_array[n];
114 }
115
116 typedef Element_type *iterator;
117 typedef const Element_type *const_iterator;
118
119 /// begin : Returns a pointer to the first element in the array.
120 iterator begin() { return m_array; }
121 /// end : Returns a pointer to the past-the-end element in the array.
122 iterator end() { return m_array + size(); }
123
124 /// begin : Returns a pointer to the first element in the array.
125 const_iterator begin() const { return m_array; }
126 /// end : Returns a pointer to the past-the-end element in the array.
127 const_iterator end() const { return m_array + size(); }
128
130 assert(m_size > 0);
132 }
133
134 size_t element_size() const { return sizeof(Element_type); }
135 size_t size() const { return m_size; }
136 bool empty() const { return m_size == 0; }
137
138 bool is_null() const { return m_array == nullptr; }
139
140 void pop_front() {
141 assert(m_size > 0);
142 m_array += 1;
143 m_size -= 1;
144 }
145
146 Element_type *array() const { return m_array; }
147
149 return m_array == rhs.m_array && m_size == rhs.m_size;
150 }
152 return m_array != rhs.m_array || m_size != rhs.m_size;
153 }
154
155 private:
156 Element_type *m_array;
157 size_t m_size;
158};
159
160template <typename Element_type>
163}
164
165#endif /* SQL_ARRAY_INCLUDED */
A wrapper class which provides array bounds checking.
Definition: sql_array.h:46
Element_type * data()
Definition: sql_array.h:102
const Element_type * data() const
Definition: sql_array.h:104
Bounds_checked_array(std::array< T, N > &arr)
Definition: sql_array.h:58
Element_type & operator[](size_t n)
Definition: sql_array.h:106
Element_type value_type
Definition: sql_array.h:49
Element_type * m_array
Definition: sql_array.h:156
bool operator!=(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:151
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:138
Element_type * array() const
Definition: sql_array.h:146
void resize(size_t new_size)
Set a new bound on the array.
Definition: sql_array.h:88
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:120
iterator end()
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:122
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:127
size_t size() const
Definition: sql_array.h:135
size_t element_size() const
Definition: sql_array.h:134
const Element_type & operator[](size_t n) const
Definition: sql_array.h:111
void pop_front()
Definition: sql_array.h:140
const_iterator begin() const
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:125
Bounds_checked_array()
Definition: sql_array.h:51
Bounds_checked_array without_back() const
Definition: sql_array.h:129
void reset()
Definition: sql_array.h:73
bool empty() const
Definition: sql_array.h:136
const Element_type * const_iterator
Definition: sql_array.h:117
Element_type * iterator
Definition: sql_array.h:116
size_t m_size
Definition: sql_array.h:157
void reset(Element_type *array, size_t size)
Definition: sql_array.h:78
bool operator==(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:148
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:97
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
const char * p
Definition: ctype-mb.cc:1236
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:221
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:161
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