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