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 Foundation, 24 51 Franklin Street, Suite 500, Boston, MA 02110-1335 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 @param Element_type The type of the elements of the container. 39 */ 40 template <typename Element_type> class Bounds_checked_array 41 { 42 public: 43 // Convenience typedef, same typedef name as std::vector 44 typedef Element_type value_type; 45 Bounds_checked_array()46 Bounds_checked_array() : m_array(NULL), m_size(0) {} 47 Bounds_checked_array(Element_type * el,size_t size_arg)48 Bounds_checked_array(Element_type *el, size_t size_arg) 49 : m_array(el), m_size(size_arg) 50 {} 51 reset()52 void reset() { m_array= NULL; m_size= 0; } 53 reset(Element_type * array,size_t size)54 void reset(Element_type *array, size_t size) 55 { 56 m_array= array; 57 m_size= size; 58 } 59 60 /** 61 Set a new bound on the array. Does not resize the underlying 62 array, so the new size must be smaller than or equal to the 63 current size. 64 */ resize(size_t new_size)65 void resize(size_t new_size) 66 { 67 assert(new_size <= m_size); 68 m_size= new_size; 69 } 70 71 Element_type &operator[](size_t n) 72 { 73 assert(n < m_size); 74 return m_array[n]; 75 } 76 77 const Element_type &operator[](size_t n) const 78 { 79 assert(n < m_size); 80 return m_array[n]; 81 } 82 83 typedef Element_type *iterator; 84 typedef const Element_type *const_iterator; 85 86 /// begin : Returns a pointer to the first element in the array. begin()87 iterator begin() { return m_array; } 88 /// end : Returns a pointer to the past-the-end element in the array. end()89 iterator end() { return m_array + size(); } 90 91 /// begin : Returns a pointer to the first element in the array. begin()92 const_iterator begin() const { return m_array; } 93 /// end : Returns a pointer to the past-the-end element in the array. end()94 const_iterator end() const { return m_array + size(); } 95 element_size()96 size_t element_size() const { return sizeof(Element_type); } size()97 size_t size() const { return m_size; } 98 is_null()99 bool is_null() const { return m_array == NULL; } 100 pop_front()101 void pop_front() 102 { 103 assert(m_size > 0); 104 m_array+= 1; 105 m_size-= 1; 106 } 107 array()108 Element_type *array() const { return m_array; } 109 110 bool operator==(const Bounds_checked_array<Element_type>&rhs) const 111 { 112 return m_array == rhs.m_array && m_size == rhs.m_size; 113 } 114 bool operator!=(const Bounds_checked_array<Element_type>&rhs) const 115 { 116 return m_array != rhs.m_array || m_size != rhs.m_size; 117 } 118 119 private: 120 Element_type *m_array; 121 size_t m_size; 122 }; 123 124 #endif /* SQL_ARRAY_INCLUDED */ 125