1 /* iostream specialisations for result and outcome
2 (C) 2017-2019 Niall Douglas <http://www.nedproductions.biz/> (21 commits)
3 File Created: July 2017
6 Boost Software License - Version 1.0 - August 17th, 2003
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
31 #ifndef BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP
32 #define BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP
34 #include "outcome.hpp"
39 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
43 template <class T> typename std::add_lvalue_reference<T>::type lvalueref() noexcept;
45 template <class T> inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<T> &v)
47 s << v._status << " ";
48 if((v._status & status_have_value) != 0)
50 s << v._value; // NOLINT
54 inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<void> &v)
56 s << v._status << " ";
59 template <class T> inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial<T> &v)
61 s << v._status << " ";
62 if((v._status & status_have_value) != 0)
64 s << v._value; // NOLINT
68 template <class T> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T> &v)
70 v = value_storage_trivial<T>();
72 if((v._status & status_have_value) != 0)
74 new(&v._value) decltype(v._value)(); // NOLINT
75 s >> v._value; // NOLINT
79 inline std::istream &operator>>(std::istream &s, value_storage_trivial<devoid<void>> &v)
81 v = value_storage_trivial<devoid<void>>();
85 template <class T> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T> &v)
87 v = value_storage_nontrivial<T>();
89 if((v._status & status_have_value) != 0)
91 new(&v._value) decltype(v._value)(); // NOLINT
92 s >> v._value; // NOLINT
96 BOOST_OUTCOME_TEMPLATE(class T)
97 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value))
98 inline std::string safe_message(T && /*unused*/) { return {}; }
99 inline std::string safe_message(const std::error_code &ec) { return " (" + ec.message() + ")"; }
100 } // namespace detail
102 /*! AWAITING HUGO JSON CONVERSION TOOL
103 SIGNATURE NOT RECOGNISED
105 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
106 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
107 inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v)
109 s >> v._iostreams_state();
112 s >> v.assume_error();
116 /*! AWAITING HUGO JSON CONVERSION TOOL
117 SIGNATURE NOT RECOGNISED
119 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
120 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
121 inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v)
123 s << v._iostreams_state();
126 s << v.assume_error();
130 /*! AWAITING HUGO JSON CONVERSION TOOL
131 SIGNATURE NOT RECOGNISED
133 template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v)
142 s << v.error() << detail::safe_message(v.error());
146 /*! AWAITING HUGO JSON CONVERSION TOOL
147 SIGNATURE NOT RECOGNISED
149 template <class S, class P> inline std::string print(const basic_result<void, S, P> &v)
158 s << v.error() << detail::safe_message(v.error());
162 /*! AWAITING HUGO JSON CONVERSION TOOL
163 SIGNATURE NOT RECOGNISED
165 template <class R, class P> inline std::string print(const basic_result<R, void, P> &v)
178 /*! AWAITING HUGO JSON CONVERSION TOOL
179 SIGNATURE NOT RECOGNISED
181 template <class P> inline std::string print(const basic_result<void, void, P> &v)
195 /*! AWAITING HUGO JSON CONVERSION TOOL
196 SIGNATURE NOT RECOGNISED
198 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
199 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<P>()))
200 inline std::istream &operator>>(std::istream &s, outcome<R, S, P, N> &v)
202 s >> v._iostreams_state();
205 s >> v.assume_error();
207 if(v.has_exception())
209 s >> v.assume_exception();
213 /*! AWAITING HUGO JSON CONVERSION TOOL
214 SIGNATURE NOT RECOGNISED
216 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
217 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<P>()))
218 inline std::ostream &operator<<(std::ostream &s, const outcome<R, S, P, N> &v)
220 s << v._iostreams_state();
223 s << v.assume_error();
225 if(v.has_exception())
227 s << v.assume_exception();
231 /*! AWAITING HUGO JSON CONVERSION TOOL
232 SIGNATURE NOT RECOGNISED
234 template <class R, class S, class P, class N> inline std::string print(const outcome<R, S, P, N> &v)
237 int total = static_cast<int>(v.has_value()) + static_cast<int>(v.has_error()) + static_cast<int>(v.has_exception());
242 s << print(static_cast<const basic_result<R, S, N> &>(static_cast<const detail::basic_result_final<R, S, N> &>(v))); // NOLINT
247 if(v.has_exception())
249 #ifndef BOOST_NO_EXCEPTIONS
252 rethrow_exception(v.exception());
254 catch(const std::system_error &e)
256 s << "std::system_error code " << e.code() << ": " << e.what();
258 catch(const std::exception &e)
260 s << "std::exception: " << e.what();
265 s << "unknown exception";
274 BOOST_OUTCOME_V2_NAMESPACE_END