1 /* iostream specialisations for result and outcome
2 (C) 2017-2022 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 <template <class, class> class ValueStorage, class T, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, E> &v)
47 s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
48 if(v._status.have_value())
50 s << v._value; // NOLINT
52 if(v._status.have_error())
54 s << v._error; // NOLINT
58 template <template <class, class> class ValueStorage, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<void, E> &v)
60 s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
61 if(v._status.have_error())
63 s << v._error; // NOLINT
67 template <template <class, class> class ValueStorage, class T> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, void> &v)
69 s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
70 if(v._status.have_value())
72 s << v._value; // NOLINT
77 template <class T, class E> inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial<T, E> &v) { return value_storage_out(s, v); }
78 template <class T, class E> inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial<T, E> &v) { return value_storage_out(s, v); }
80 template <template <class, class> class ValueStorage, class T, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, E> &v)
82 using type = ValueStorage<T, E>;
87 v._status.status_value = static_cast<detail::status>(x);
88 v._status.spare_storage_value = y;
89 if(v._status.have_value())
91 new(&v._value) decltype(v._value)(); // NOLINT
92 s >> v._value; // NOLINT
94 if(v._status.have_error())
96 new(&v._error) decltype(v._error)(); // NOLINT
97 s >> v._error; // NOLINT
101 template <template <class, class> class ValueStorage, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<void, E> &v)
103 using type = ValueStorage<void, E>;
108 v._status.status_value = static_cast<detail::status>(x);
109 v._status.spare_storage_value = y;
110 if(v._status.have_error())
112 new(&v._error) decltype(v._error)(); // NOLINT
113 s >> v._error; // NOLINT
117 template <template <class, class> class ValueStorage, class T> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, void> &v)
119 using type = ValueStorage<T, void>;
124 v._status.status_value = static_cast<detail::status>(x);
125 v._status.spare_storage_value = y;
126 if(v._status.have_value())
128 new(&v._value) decltype(v._value)(); // NOLINT
129 s >> v._value; // NOLINT
133 template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T, E> &v) { return value_storage_in(s, v); }
134 template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T, E> &v) { return value_storage_in(s, v); }
135 BOOST_OUTCOME_TEMPLATE(class T)
136 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value))
137 inline std::string safe_message(T && /*unused*/) { return {}; }
138 inline std::string safe_message(const std::error_code &ec) { return " (" + ec.message() + ")"; }
139 } // namespace detail
141 /*! AWAITING HUGO JSON CONVERSION TOOL
142 SIGNATURE NOT RECOGNISED
144 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
145 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
146 inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v)
148 s >> v._iostreams_state();
151 s >> v.assume_error();
155 /*! AWAITING HUGO JSON CONVERSION TOOL
156 SIGNATURE NOT RECOGNISED
158 BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
159 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
160 inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v)
162 s << v._iostreams_state();
165 s << v.assume_error();
169 /*! AWAITING HUGO JSON CONVERSION TOOL
170 SIGNATURE NOT RECOGNISED
172 template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v)
181 s << v.error() << detail::safe_message(v.error());
185 /*! AWAITING HUGO JSON CONVERSION TOOL
186 SIGNATURE NOT RECOGNISED
188 template <class S, class P> inline std::string print(const basic_result<void, S, P> &v)
197 s << v.error() << detail::safe_message(v.error());
201 /*! AWAITING HUGO JSON CONVERSION TOOL
202 SIGNATURE NOT RECOGNISED
204 template <class R, class P> inline std::string print(const basic_result<R, void, P> &v)
217 /*! AWAITING HUGO JSON CONVERSION TOOL
218 SIGNATURE NOT RECOGNISED
220 template <class P> inline std::string print(const basic_result<void, void, P> &v)
234 /*! AWAITING HUGO JSON CONVERSION TOOL
235 SIGNATURE NOT RECOGNISED
237 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
238 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>()))
239 inline std::istream &operator>>(std::istream &s, outcome<R, S, P, N> &v)
241 s >> v._iostreams_state();
244 s >> v.assume_error();
246 if(v.has_exception())
248 s >> v.assume_exception();
252 /*! AWAITING HUGO JSON CONVERSION TOOL
253 SIGNATURE NOT RECOGNISED
255 BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
256 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>()))
257 inline std::ostream &operator<<(std::ostream &s, const outcome<R, S, P, N> &v)
259 s << v._iostreams_state();
262 s << v.assume_error();
264 if(v.has_exception())
266 s << v.assume_exception();
270 /*! AWAITING HUGO JSON CONVERSION TOOL
271 SIGNATURE NOT RECOGNISED
273 template <class R, class S, class P, class N> inline std::string print(const outcome<R, S, P, N> &v)
276 int total = static_cast<int>(v.has_value()) + static_cast<int>(v.has_error()) + static_cast<int>(v.has_exception());
281 s << print(static_cast<const basic_result<R, S, N> &>(static_cast<const detail::basic_result_final<R, S, N> &>(v))); // NOLINT
286 if(v.has_exception())
288 #ifndef BOOST_NO_EXCEPTIONS
291 rethrow_exception(v.exception());
293 catch(const std::system_error &e)
295 s << "std::system_error code " << e.code() << ": " << e.what();
297 catch(const std::exception &e)
299 s << "std::exception: " << e.what();
304 s << "unknown exception";
313 BOOST_OUTCOME_V2_NAMESPACE_END