MySQL 9.1.0
Source Code Documentation
enumeration_utils.h
Go to the documentation of this file.
1// Copyright (c) 2023, 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_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/// @brief Helper function that converts enum type to underlying integer type
40/// @note This function may be removed after switching to C++23
41/// @tparam Enum_type Type of the enumeration parameter that gets converted into
42/// the underlying type value
43template <typename Enum_type>
44constexpr inline decltype(auto) to_underlying(Enum_type enum_value) {
45 static_assert(
46 std::is_enum<Enum_type>::value,
47 "to_underlying conversion function called with non-enumeration argument");
48 using EnumValueType = std::underlying_type_t<Enum_type>;
49 return static_cast<EnumValueType>(enum_value);
50}
51
52/// @brief Template function that returns maximum *valid* constant that can
53/// appear in the enumeration type. It must be specialized for each
54/// enumeration type serialized
55/// @tparam Enum_type Type of the enumeration that will be returned
56/// @return Last valid enumeration constant within Enum_type
57template <typename Enum_type>
58constexpr inline Enum_type enum_max();
59
60/// @brief Helper function that converts value of enumeration underlying type
61/// into enumeration type constant
62/// @tparam Enum_type Type of the enumeration that Integral_type parameter is
63/// converted into
64/// @tparam Integral_type Type of the enumeration parameter that gets converted
65/// into the Enum_type
66template <typename Enum_type, typename Integral_type>
67constexpr inline std::pair<Enum_type, Return_status> to_enumeration(
68 Integral_type value) {
69 static_assert(std::is_enum_v<Enum_type>,
70 "to_enumeration conversion requested for non-enumeration type");
71 static_assert(std::is_integral_v<Integral_type>,
72 "to_enumeration conversion requested from non-integral type");
73 if (value > to_underlying(enum_max<Enum_type>())) {
74 return std::make_pair(enum_max<Enum_type>(), Return_status::error);
75 }
76 return std::make_pair(static_cast<Enum_type>(value), Return_status::ok);
77}
78
79} // namespace mysql::utils
80
81/// @}
82
83#endif // MYSQL_UTILS_ENUMERATION_UTILS_H
Definition: gtid_format.h:46
@ ok
operation succeeded
@ error
operation failed
constexpr std::pair< Enum_type, Return_status > to_enumeration(Integral_type value)
Helper function that converts value of enumeration underlying type into enumeration type constant.
Definition: enumeration_utils.h:67
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:44
Enum_type
Definition: parse_tree_column_attrs.h:851
Experimental API header.