MySQL 9.1.0
Source Code Documentation
array_base.h
Go to the documentation of this file.
1// Copyright (c) 2024, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is also distributed with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have included with MySQL.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License, version 2.0, for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22
23#ifndef MYSQL_ABI_HELPERS_DETAIL_ARRAY_BASE_H
24#define MYSQL_ABI_HELPERS_DETAIL_ARRAY_BASE_H
25
26/// @file
27/// Experimental API header
28
29#include <cassert> // assert
30#include <cstddef> // std::size_t
31#include <cstdint> // int32_t
32#include <type_traits> // is_trivial_v
33
34/// @addtogroup GroupLibsMysqlAbiHelpers
35/// @{
36
38
39/// Base class for specific implementations of standard-layout classes for
40/// arrays.
41///
42/// This stores the length and the array, and exposes iterators, index operators
43/// and member functions to query size/emptiness.
44///
45/// @tparam Element_t The type of each element.
46///
47/// @tparam Array_t The type of the array, for example `Element_t *` or
48/// `Element_t[7]`.
49template <class Element_t, class Array_t>
50 requires requires { std::is_trivial_v<Element_t>; }
52 public:
53 /// @return Number of elements.
54 std::size_t size() const { return m_size; }
55
56 /// @return Number of elements (signed).
57 std::ptrdiff_t ssize() const { return (std::ptrdiff_t)m_size; }
58
59 /// @return true if there are no elements, false if there are any.
60 bool empty() const { return m_size == 0; }
61
62 /// @return true if there are any elements, false if there are none.
63 operator bool() const { return m_size != 0; }
64
65 /// Index operator (non-const).
66 ///
67 /// @param index Array index.
68 /// @return Non-const reference to element at the given index.
69 Element_t &operator[](std::size_t index) {
70 assert((int32_t)index < m_size);
71 return m_data[index];
72 }
73
74 /// @return Pointer to the array (non-const, equal to `begin()`).
75 Element_t *data() { return m_data; }
76
77 /// @return Iterator to the beginning (non-const).
78 ///
79 /// Actually, just the raw pointer. This is a contiguous iterator.
80 Element_t *begin() { return m_data; }
81
82 /// @return Iterator to the end (non-const).
83 ///
84 /// Actually, just the raw pointer. This is a contiguous iterator.
85 Element_t *end() { return m_data + m_size; }
86
87 /// Index operator (const).
88 ///
89 /// @param index Array index.
90 /// @return Const reference to element at the given index.
91 const Element_t &operator[](std::size_t index) const {
92 assert((int32_t)index < m_size);
93 return m_data[index];
94 }
95
96 /// @return Pointer to the array (const, equal to `cbegin()`).
97 const Element_t *data() const { return m_data; }
98
99 /// @return Iterator to the beginning (const).
100 const Element_t *begin() const { return m_data; }
101
102 /// @return Iterator to the end (const).
103 const Element_t *end() const { return m_data + m_size; }
104
105 /// @return Iterator to the beginning (const).
106 const Element_t *cbegin() const { return m_data; }
107
108 /// @return Iterator to the end (const).
109 const Element_t *cend() const { return m_data + m_size; }
110
111 protected:
112 /// Number of elements in the array.
113 ///
114 /// We keep it as 32 bits, so that small arrays may fit in 64 bits.
115 int32_t m_size;
116
117 /// Array data
118 Array_t m_data;
119};
120
121} // namespace mysql::abi_helpers::detail
122
123// addtogroup GroupLibsMysqlAbiHelpers
124/// @}
125
126#endif // ifndef MYSQL_ABI_HELPERS_DETAIL_ARRAY_BASE_H
Base class for specific implementations of standard-layout classes for arrays.
Definition: array_base.h:51
const Element_t * cend() const
Definition: array_base.h:109
Element_t * begin()
Definition: array_base.h:80
const Element_t * data() const
Definition: array_base.h:97
std::ptrdiff_t ssize() const
Definition: array_base.h:57
Element_t & operator[](std::size_t index)
Index operator (non-const).
Definition: array_base.h:69
bool empty() const
Definition: array_base.h:60
int32_t m_size
Number of elements in the array.
Definition: array_base.h:115
const Element_t * cbegin() const
Definition: array_base.h:106
std::size_t size() const
Definition: array_base.h:54
Array_t m_data
Array data.
Definition: array_base.h:118
const Element_t * end() const
Definition: array_base.h:103
const Element_t & operator[](std::size_t index) const
Index operator (const).
Definition: array_base.h:91
Element_t * end()
Definition: array_base.h:85
const Element_t * begin() const
Definition: array_base.h:100
Element_t * data()
Definition: array_base.h:75
Definition: array_base.h:37