MySQL 9.0.0
Source Code Documentation
expected_ostream.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_STDX_EXPECTED_OSTREAM_H_
27#define MYSQL_HARNESS_STDX_EXPECTED_OSTREAM_H_
28
30
31#include <ostream>
32#include <type_traits>
33
34// operator<< for std::expected
35//
36// the functions are kept in a separate header as it
37//
38// - isn't part of the std-proposal
39// - includes <ostream> which isn't need for stdx::expected<> itself
40
41namespace stdx {
42namespace impl {
43template <typename S, typename T, typename = void>
44struct is_to_stream_writable : std::false_type {};
45
46template <typename S, typename T>
48 S, T, std::void_t<decltype(std::declval<S &>() << std::declval<T>())>>
49 : std::true_type {};
50
51} // namespace impl
52
53/**
54 * write stdx::expected<T, E> to std::ostream.
55 *
56 * T and E must be non-void.
57 *
58 * only takes part in overload-resolution if T and E support 'os << v'
59 */
60template <class T, class E>
61inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, T>::value &&
62 impl::is_to_stream_writable<std::ostream, E>::value,
63 std::ostream &>
64operator<<(std::ostream &os, const stdx::expected<T, E> &res) {
65 if (res)
66 os << res.value();
67 else
68 os << res.error();
69
70 return os;
71}
72
73/**
74 * write stdx::expected<void, E> to std::ostream.
75 *
76 * only takes part in overload-resolution if E supports 'os << v'
77 */
78template <class E>
79inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, E>::value,
80 std::ostream &>
81operator<<(std::ostream &os, const stdx::expected<void, E> &res) {
82 if (!res) os << res.error();
83
84 return os;
85}
86
87/**
88 * write stdx::unexpected<E> to std::ostream.
89 *
90 * only takes part in overload-resolution if E supports 'os << v'
91 */
92template <class E>
93inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, E>::value,
94 std::ostream &>
95operator<<(std::ostream &os, const stdx::unexpected<E> &res) {
96 os << res.error();
97
98 return os;
99}
100} // namespace stdx
101
102#endif
Definition: http_server_component.cc:34
Definition: gcs_xcom_synode.h:64
Definition: bit.h:32
Definition: expected_ostream.h:44