123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #ifndef BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP
- #define BOOST_OUTCOME_IOSTREAM_SUPPORT_HPP
- #include "outcome.hpp"
- #include <iostream>
- #include <sstream>
- BOOST_OUTCOME_V2_NAMESPACE_BEGIN
- namespace detail
- {
- template <class T> typename std::add_lvalue_reference<T>::type lvalueref() noexcept;
- template <template <class, class> class ValueStorage, class T, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, E> &v)
- {
- s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
- if(v._status.have_value())
- {
- s << v._value;
- }
- if(v._status.have_error())
- {
- s << v._error;
- }
- return s;
- }
- template <template <class, class> class ValueStorage, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<void, E> &v)
- {
- s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
- if(v._status.have_error())
- {
- s << v._error;
- }
- return s;
- }
- template <template <class, class> class ValueStorage, class T> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage<T, void> &v)
- {
- s << static_cast<uint16_t>(v._status.status_value) << " " << v._status.spare_storage_value << " ";
- if(v._status.have_value())
- {
- s << v._value;
- }
- return s;
- }
- 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); }
- 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); }
- template <template <class, class> class ValueStorage, class T, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, E> &v)
- {
- using type = ValueStorage<T, E>;
- v.~type();
- new(&v) type;
- uint16_t x, y;
- s >> x >> y;
- v._status.status_value = static_cast<detail::status>(x);
- v._status.spare_storage_value = y;
- if(v._status.have_value())
- {
- new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)();
- s >> v._value;
- }
- if(v._status.have_error())
- {
- new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)();
- s >> v._error;
- }
- return s;
- }
- template <template <class, class> class ValueStorage, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage<void, E> &v)
- {
- using type = ValueStorage<void, E>;
- v.~type();
- new(&v) type;
- uint16_t x, y;
- s >> x >> y;
- v._status.status_value = static_cast<detail::status>(x);
- v._status.spare_storage_value = y;
- if(v._status.have_error())
- {
- new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)();
- s >> v._error;
- }
- return s;
- }
- template <template <class, class> class ValueStorage, class T> inline std::istream &value_storage_in(std::istream &s, ValueStorage<T, void> &v)
- {
- using type = ValueStorage<T, void>;
- v.~type();
- new(&v) type;
- uint16_t x, y;
- s >> x >> y;
- v._status.status_value = static_cast<detail::status>(x);
- v._status.spare_storage_value = y;
- if(v._status.have_value())
- {
- new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)();
- s >> v._value;
- }
- return s;
- }
- template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_trivial<T, E> &v) { return value_storage_in(s, v); }
- template <class T, class E> inline std::istream &operator>>(std::istream &s, value_storage_nontrivial<T, E> &v) { return value_storage_in(s, v); }
- BOOST_OUTCOME_TEMPLATE(class T)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible<std::error_code, T>::value))
- inline std::string safe_message(T && ) { return {}; }
- inline std::string safe_message(const std::error_code &ec) { return " (" + ec.message() + ")"; }
- }
- BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::istream>() >> detail::lvalueref<S>()))
- inline std::istream &operator>>(std::istream &s, basic_result<R, S, P> &v)
- {
- s >> v._iostreams_state();
- if(v.has_error())
- {
- s >> v.assume_error();
- }
- return s;
- }
- BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
- BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<R>()), BOOST_OUTCOME_TEXPR(detail::lvalueref<std::ostream>() << detail::lvalueref<S>()))
- inline std::ostream &operator<<(std::ostream &s, const basic_result<R, S, P> &v)
- {
- s << v._iostreams_state();
- if(v.has_error())
- {
- s << v.assume_error();
- }
- return s;
- }
- template <class R, class S, class P> inline std::string print(const basic_result<R, S, P> &v)
- {
- std::stringstream s;
- if(v.has_value())
- {
- s << v.value();
- }
- if(v.has_error())
- {
- s << v.error() << detail::safe_message(v.error());
- }
- return s.str();
- }
- template <class S, class P> inline std::string print(const basic_result<void, S, P> &v)
- {
- std::stringstream s;
- if(v.has_value())
- {
- s << "(+void)";
- }
- if(v.has_error())
- {
- s << v.error() << detail::safe_message(v.error());
- }
- return s.str();
- }
- template <class R, class P> inline std::string print(const basic_result<R, void, P> &v)
- {
- std::stringstream s;
- if(v.has_value())
- {
- s << v.value();
- }
- if(v.has_error())
- {
- s << "(-void)";
- }
- return s.str();
- }
- template <class P> inline std::string print(const basic_result<void, void, P> &v)
- {
- std::stringstream s;
- if(v.has_value())
- {
- s << "(+void)";
- }
- if(v.has_error())
- {
- s << "(-void)";
- }
- return s.str();
- }
- BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
- 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>()))
- inline std::istream &operator>>(std::istream &s, outcome<R, S, P, N> &v)
- {
- s >> v._iostreams_state();
- if(v.has_error())
- {
- s >> v.assume_error();
- }
- if(v.has_exception())
- {
- s >> v.assume_exception();
- }
- return s;
- }
- BOOST_OUTCOME_TEMPLATE(class R, class S, class P, class N)
- 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>()))
- inline std::ostream &operator<<(std::ostream &s, const outcome<R, S, P, N> &v)
- {
- s << v._iostreams_state();
- if(v.has_error())
- {
- s << v.assume_error();
- }
- if(v.has_exception())
- {
- s << v.assume_exception();
- }
- return s;
- }
- template <class R, class S, class P, class N> inline std::string print(const outcome<R, S, P, N> &v)
- {
- std::stringstream s;
- int total = static_cast<int>(v.has_value()) + static_cast<int>(v.has_error()) + static_cast<int>(v.has_exception());
- if(total > 1)
- {
- s << "{ ";
- }
- s << print(static_cast<const basic_result<R, S, N> &>(static_cast<const detail::basic_result_final<R, S, N> &>(v)));
- if(total > 1)
- {
- s << ", ";
- }
- if(v.has_exception())
- {
- #ifndef BOOST_NO_EXCEPTIONS
- try
- {
- rethrow_exception(v.exception());
- }
- catch(const std::system_error &e)
- {
- s << "std::system_error code " << e.code() << ": " << e.what();
- }
- catch(const std::exception &e)
- {
- s << "std::exception: " << e.what();
- }
- catch(...)
- #endif
- {
- s << "unknown exception";
- }
- }
- if(total > 1)
- {
- s << " }";
- }
- return s.str();
- }
- BOOST_OUTCOME_V2_NAMESPACE_END
- #endif
|