MySQL 9.6.0
Source Code Documentation
enumeration_utils.h
Go to the documentation of this file.
1// Copyright (c) 2023, 2025, 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_UTILS_ENUMERATION_UTILS_H
25#define MYSQL_UTILS_ENUMERATION_UTILS_H
26
27/// @file
28/// Experimental API header
29
30#include <string>
31#include <utility>
33
34/// @addtogroup GroupLibsMysqlUtils
35/// @{
36
37namespace mysql::utils {
38
39/// true if Enum_t is an enumeration type (scoped or not).
40///
41/// This is equivalent to is_enum_v, but as a concept, is syntactically valid in
42/// more contexts.
43template <class Enum_t>
44concept Is_enum = std::is_enum_v<Enum_t>;
45
46/// @brief Helper function that converts enum type to underlying integer type
47/// @note This function may be removed after switching to C++23
48/// @tparam Enum_type Type of the enumeration parameter that gets converted into
49/// the underlying type value
50template <Is_enum Enum_type>
51constexpr decltype(auto) to_underlying(Enum_type enum_value) {
52 using EnumValueType = std::underlying_type_t<Enum_type>;
53 return static_cast<EnumValueType>(enum_value);
54}
55
56/// @brief Template function that returns maximum *valid* constant that can
57/// appear in the enumeration type. It must be specialized for each
58/// enumeration type serialized
59/// @tparam Enum_type Type of the enumeration that will be returned
60/// @return Last valid enumeration constant within Enum_type
61template <Is_enum Enum_type>
62constexpr Enum_type enum_max();
63
64/// @brief Helper function that converts value of enumeration underlying type
65/// into enumeration type constant
66/// @tparam Enum_type Type of the enumeration that Integral_type parameter is
67/// converted into
68template <Is_enum Enum_type>
69constexpr std::pair<Enum_type, Return_status> to_enumeration(
70 std::integral auto value) {
71 if (value > to_underlying(enum_max<Enum_type>())) {
72 return std::make_pair(enum_max<Enum_type>(), Return_status::error);
73 }
74 return std::make_pair(static_cast<Enum_type>(value), Return_status::ok);
75}
76
77} // namespace mysql::utils
78
79/// @}
80
81#endif // MYSQL_UTILS_ENUMERATION_UTILS_H
true if Enum_t is an enumeration type (scoped or not).
Definition: enumeration_utils.h:44
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Definition: gtid_format.h:47
constexpr std::pair< Enum_type, Return_status > to_enumeration(std::integral auto value)
Helper function that converts value of enumeration underlying type into enumeration type constant.
Definition: enumeration_utils.h:69
@ ok
operation succeeded
@ error
operation failed
constexpr Enum_type enum_max()
Template function that returns maximum valid constant that can appear in the enumeration type.
constexpr decltype(auto) to_underlying(Enum_type enum_value)
Helper function that converts enum type to underlying integer type.
Definition: enumeration_utils.h:51
Enum_type
Definition: parse_tree_column_attrs.h:926
Experimental API header.