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