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