MySQL 26.7.0
Source Code Documentation
ut0expected.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2023, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0expected.h
29 Minimal implementation of C++23 std::expected.
30
31 https://en.cppreference.com/w/cpp/utility/expected
32
33 ****************************************************************************/
34
35#ifndef ut0expected_h
36#define ut0expected_h
37
38#include <utility>
39#include <variant>
40
41#include "db0err.h" // dberr_t
42#include "ut0dbg.h"
43
44namespace ut {
45
46/** C++23 std::unexpected. */
47template <class E>
49 public:
50 template <class Err = E>
51 constexpr explicit Unexpected(Err &&e) : m_error(std::forward<Err>(e)) {}
52
53 constexpr const E &error() const &noexcept { return m_error; }
54
55 constexpr E &error() &noexcept { return m_error; }
56
57 constexpr const E &&error() const &&noexcept { return std::move(m_error); }
58
59 constexpr E &&error() &&noexcept { return std::move(m_error); }
60
61 private:
63};
64
65template <class E>
67
68/* Replace with std::expected once it is available in all supported compilers -
69the support for C++23 is not enough for some older gcc and clang that we still
70support. */
71
72/** C++23 std::expected. */
73template <class T, class E = dberr_t>
74class Expected : public std::variant<T, E> {
75 public:
76 using value_type = T;
77 using error_type = E;
78
79 public:
80 constexpr Expected(Expected &&other) noexcept
81 : std::variant<T, E>(std::move(other)) {}
82
83 template <typename U = std::remove_cv_t<T>>
84 constexpr explicit(!std::is_convertible_v<U, T>) Expected(U &&u) noexcept
85 : std::variant<T, E>(std::in_place_index<0>, std::forward<U>(u)) {}
86
87 template <class U>
88 constexpr Expected(Unexpected<U> &&u)
89 : std::variant<T, E>(std::in_place_index<1>, std::move(u).error()) {}
90
91 constexpr const T *operator->() const noexcept {
92 ut_a(has_value());
93 return &std::get<0>(*this);
94 }
95
96 constexpr T *operator->() noexcept {
97 ut_a(has_value());
98 return &std::get<0>(*this);
99 }
100
101 constexpr const T &operator*() const &noexcept {
102 ut_a(has_value());
103 return std::get<0>(*this);
104 }
105
106 constexpr T &operator*() &noexcept { return std::get<0>(*this); }
107
108 constexpr const T &&operator*() const &&noexcept {
109 return std::move(std::get<0>(*this));
110 }
111
112 constexpr T &&operator*() &&noexcept { return std::move(std::get<0>(*this)); }
113
114 constexpr explicit operator bool() const noexcept { return has_value(); }
115
116 constexpr bool has_value() const noexcept { return this->index() == 0; }
117
118 constexpr const T &value() const & {
119 ut_a(has_value());
120 return std::get<0>(*this);
121 }
122
123 constexpr T &value() & {
124 ut_a(has_value());
125 return std::get<0>(*this);
126 }
127
128 constexpr const T &&value() const && {
129 ut_a(has_value());
130 return std::move(std::get<0>(*this));
131 }
132
133 constexpr T &&value() && {
134 ut_a(has_value());
135 return std::move(std::get<0>(*this));
136 }
137
138 constexpr const E &error() const &noexcept {
139 ut_a(!has_value());
140 return std::get<1>(*this);
141 }
142
143 constexpr E &error() &noexcept {
144 ut_a(!has_value());
145 return std::get<1>(*this);
146 }
147
148 constexpr const E &&error() const &&noexcept {
149 ut_a(!has_value());
150 return std::move(std::get<1>(*this));
151 }
152
153 constexpr E &&error() &&noexcept {
154 ut_a(!has_value());
155 return std::move(std::get<1>(*this));
156 }
157};
158
159} /* namespace ut */
160
161#endif /* !ut0expected_h */
C++23 std::expected.
Definition: ut0expected.h:74
constexpr const T * operator->() const noexcept
Definition: ut0expected.h:91
constexpr T & value() &
Definition: ut0expected.h:123
constexpr const E & error() const &noexcept
Definition: ut0expected.h:138
E error_type
Definition: ut0expected.h:77
constexpr const E && error() const &&noexcept
Definition: ut0expected.h:148
T value_type
Definition: ut0expected.h:76
constexpr T & operator*() &noexcept
Definition: ut0expected.h:106
constexpr T && operator*() &&noexcept
Definition: ut0expected.h:112
constexpr const T && operator*() const &&noexcept
Definition: ut0expected.h:108
constexpr T * operator->() noexcept
Definition: ut0expected.h:96
constexpr Expected(Expected &&other) noexcept
Definition: ut0expected.h:80
constexpr Expected(Unexpected< U > &&u)
Definition: ut0expected.h:88
constexpr E && error() &&noexcept
Definition: ut0expected.h:153
constexpr const T & operator*() const &noexcept
Definition: ut0expected.h:101
constexpr E & error() &noexcept
Definition: ut0expected.h:143
constexpr T && value() &&
Definition: ut0expected.h:133
constexpr const T && value() const &&
Definition: ut0expected.h:128
constexpr const T & value() const &
Definition: ut0expected.h:118
constexpr bool has_value() const noexcept
Definition: ut0expected.h:116
C++23 std::unexpected.
Definition: ut0expected.h:48
constexpr Unexpected(Err &&e)
Definition: ut0expected.h:51
constexpr E && error() &&noexcept
Definition: ut0expected.h:59
constexpr E & error() &noexcept
Definition: ut0expected.h:55
E m_error
Definition: ut0expected.h:62
constexpr const E && error() const &&noexcept
Definition: ut0expected.h:57
constexpr const E & error() const &noexcept
Definition: ut0expected.h:53
Global error codes for the database.
#define T
Definition: jit_executor_value.cc:373
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:76
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Define std::hash<Gtid>.
Definition: gtid.h:355
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
Unexpected(E) -> Unexpected< E >
Definition: dtoa.cc:595
Debug utilities for Innobase.
#define ut_a(EXPR)
Abort execution if EXPR does not evaluate to nonzero.
Definition: ut0dbg.h:97