MySQL 8.3.0
Source Code Documentation
expected_ostream.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2023, 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 also distributed 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 included with MySQL.
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 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
25#ifndef MYSQL_HARNESS_STDX_EXPECTED_OSTREAM_H_
26#define MYSQL_HARNESS_STDX_EXPECTED_OSTREAM_H_
27
29
30#include <ostream>
31#include <type_traits>
32
33// operator<< for std::expected
34//
35// the functions are kept in a separate header as it
36//
37// - isn't part of the std-proposal
38// - includes <ostream> which isn't need for stdx::expected<> itself
39
40namespace stdx {
41namespace impl {
42template <typename S, typename T, typename = void>
43struct is_to_stream_writable : std::false_type {};
44
45template <typename S, typename T>
47 S, T, std::void_t<decltype(std::declval<S &>() << std::declval<T>())>>
48 : std::true_type {};
49
50} // namespace impl
51
52/**
53 * write stdx::expected<T, E> to std::ostream.
54 *
55 * T and E must be non-void.
56 *
57 * only takes part in overload-resolution if T and E support 'os << v'
58 */
59template <class T, class E>
60inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, T>::value &&
61 impl::is_to_stream_writable<std::ostream, E>::value,
62 std::ostream &>
63operator<<(std::ostream &os, const stdx::expected<T, E> &res) {
64 if (res)
65 os << res.value();
66 else
67 os << res.error();
68
69 return os;
70}
71
72/**
73 * write stdx::expected<void, E> to std::ostream.
74 *
75 * only takes part in overload-resolution if E supports 'os << v'
76 */
77template <class E>
78inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, E>::value,
79 std::ostream &>
80operator<<(std::ostream &os, const stdx::expected<void, E> &res) {
81 if (!res) os << res.error();
82
83 return os;
84}
85
86/**
87 * write stdx::unexpected<E> to std::ostream.
88 *
89 * only takes part in overload-resolution if E supports 'os << v'
90 */
91template <class E>
92inline std::enable_if_t<impl::is_to_stream_writable<std::ostream, E>::value,
93 std::ostream &>
94operator<<(std::ostream &os, const stdx::unexpected<E> &res) {
95 os << res.value();
96
97 return os;
98}
99} // namespace stdx
100
101#endif
Definition: authentication.cc:35
Definition: varlen_sort.h:174
Definition: bit.h:33
Definition: expected_ostream.h:43