MySQL  8.0.17
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, 2017, Oracle and/or its affiliates. All rights reserved.
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 "my_dbug.h"
27 
28 /**
29  A wrapper class which provides array bounds checking.
30  We do *not* own the array, we simply have a pointer to the first element,
31  and a length.
32 
33  @remark
34  We want the compiler-generated versions of:
35  - the copy CTOR (memberwise initialization)
36  - the assignment operator (memberwise assignment)
37 
38  @tparam Element_type The type of the elements of the container.
39  */
40 template <typename Element_type>
42  public:
43  // Convenience typedef, same typedef name as std::vector
44  typedef Element_type value_type;
45 
47 
48  Bounds_checked_array(Element_type *el, size_t size_arg)
49  : m_array(el), m_size(size_arg) {}
50 
51  void reset() {
52  m_array = NULL;
53  m_size = 0;
54  }
55 
56  void reset(Element_type *array, size_t size) {
57  m_array = array;
58  m_size = size;
59  }
60 
61  /**
62  Set a new bound on the array. Does not resize the underlying
63  array, so the new size must be smaller than or equal to the
64  current size.
65  */
66  void resize(size_t new_size) {
67  DBUG_ASSERT(new_size <= m_size);
68  m_size = new_size;
69  }
70 
71  Element_type &operator[](size_t n) {
72  DBUG_ASSERT(n < m_size);
73  return m_array[n];
74  }
75 
76  const Element_type &operator[](size_t n) const {
77  DBUG_ASSERT(n < m_size);
78  return m_array[n];
79  }
80 
81  typedef Element_type *iterator;
82  typedef const Element_type *const_iterator;
83 
84  /// begin : Returns a pointer to the first element in the array.
85  iterator begin() { return m_array; }
86  /// end : Returns a pointer to the past-the-end element in the array.
87  iterator end() { return m_array + size(); }
88 
89  /// begin : Returns a pointer to the first element in the array.
90  const_iterator begin() const { return m_array; }
91  /// end : Returns a pointer to the past-the-end element in the array.
92  const_iterator end() const { return m_array + size(); }
93 
94  size_t element_size() const { return sizeof(Element_type); }
95  size_t size() const { return m_size; }
96 
97  bool is_null() const { return m_array == NULL; }
98 
99  void pop_front() {
100  DBUG_ASSERT(m_size > 0);
101  m_array += 1;
102  m_size -= 1;
103  }
104 
105  Element_type *array() const { return m_array; }
106 
108  return m_array == rhs.m_array && m_size == rhs.m_size;
109  }
111  return m_array != rhs.m_array || m_size != rhs.m_size;
112  }
113 
114  private:
115  Element_type *m_array;
116  size_t m_size;
117 };
118 
119 template <typename Element_type>
122 }
123 
124 #endif /* SQL_ARRAY_INCLUDED */
Element_type * m_array
Definition: sql_array.h:115
Element_type * iterator
Definition: sql_array.h:81
size_t m_size
Definition: sql_array.h:116
A wrapper class which provides array bounds checking.
Definition: sql_array.h:41
const_iterator end() const
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:92
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
void reset()
Definition: sql_array.h:51
const Element_type & operator[](size_t n) const
Definition: sql_array.h:76
#define DBUG_ASSERT(A)
Definition: my_dbug.h:183
Element_type * array() const
Definition: sql_array.h:105
void reset(Element_type *array, size_t size)
Definition: sql_array.h:56
Bounds_checked_array()
Definition: sql_array.h:46
Bounds_checked_array(Element_type *el, size_t size_arg)
Definition: sql_array.h:48
void pop_front()
Definition: sql_array.h:99
bool operator!=(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:110
const_iterator begin() const
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:90
iterator end()
end : Returns a pointer to the past-the-end element in the array.
Definition: sql_array.h:87
bool is_null() const
Definition: sql_array.h:97
size_t element_size() const
Definition: sql_array.h:94
Bounds_checked_array< Element_type > make_array(Element_type *p, size_t n)
Definition: sql_array.h:120
const Element_type * const_iterator
Definition: sql_array.h:82
size_t size() const
Definition: sql_array.h:95
bool operator==(const Bounds_checked_array< Element_type > &rhs) const
Definition: sql_array.h:107
const char * p
Definition: ctype-mb.cc:1232
iterator begin()
begin : Returns a pointer to the first element in the array.
Definition: sql_array.h:85
void resize(size_t new_size)
Set a new bound on the array.
Definition: sql_array.h:66
Element_type & operator[](size_t n)
Definition: sql_array.h:71
Element_type value_type
Definition: sql_array.h:44