/* iostream specialisations for result
(C) 2017-2025 Niall Douglas (21 commits)
File Created: May 2025
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef BOOST_OUTCOME_IOSTREAM_SUPPORT_RESULT_HPP
#define BOOST_OUTCOME_IOSTREAM_SUPPORT_RESULT_HPP
#include "result.hpp"
#include
#include
BOOST_OUTCOME_V2_NAMESPACE_BEGIN
namespace detail
{
template typename std::add_lvalue_reference::type lvalueref() noexcept;
template class ValueStorage, class T, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage &v)
{
s << static_cast(v._status.status_value) << " " << v._status.spare_storage_value << " ";
if(v._status.have_value())
{
s << v._value; // NOLINT
}
if(v._status.have_error())
{
s << v._error; // NOLINT
}
return s;
}
template class ValueStorage, class E> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage &v)
{
s << static_cast(v._status.status_value) << " " << v._status.spare_storage_value << " ";
if(v._status.have_error())
{
s << v._error; // NOLINT
}
return s;
}
template class ValueStorage, class T> inline std::ostream &value_storage_out(std::ostream &s, const ValueStorage &v)
{
s << static_cast(v._status.status_value) << " " << v._status.spare_storage_value << " ";
if(v._status.have_value())
{
s << v._value; // NOLINT
}
return s;
}
template inline std::ostream &operator<<(std::ostream &s, const value_storage_trivial &v)
{
return value_storage_out(s, v);
}
template inline std::ostream &operator<<(std::ostream &s, const value_storage_nontrivial &v)
{
return value_storage_out(s, v);
}
template class ValueStorage, class T, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage &v)
{
using type = ValueStorage;
v.~type();
new(&v) type;
uint16_t x, y;
s >> x >> y;
v._status.status_value = static_cast(x);
v._status.spare_storage_value = y;
if(v._status.have_value())
{
new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)(); // NOLINT
s >> v._value; // NOLINT
}
if(v._status.have_error())
{
new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)(); // NOLINT
s >> v._error; // NOLINT
}
return s;
}
template class ValueStorage, class E> inline std::istream &value_storage_in(std::istream &s, ValueStorage &v)
{
using type = ValueStorage;
v.~type();
new(&v) type;
uint16_t x, y;
s >> x >> y;
v._status.status_value = static_cast(x);
v._status.spare_storage_value = y;
if(v._status.have_error())
{
new(BOOST_OUTCOME_ADDRESS_OF(v._error)) decltype(v._error)(); // NOLINT
s >> v._error; // NOLINT
}
return s;
}
template class ValueStorage, class T> inline std::istream &value_storage_in(std::istream &s, ValueStorage &v)
{
using type = ValueStorage;
v.~type();
new(&v) type;
uint16_t x, y;
s >> x >> y;
v._status.status_value = static_cast(x);
v._status.spare_storage_value = y;
if(v._status.have_value())
{
new(BOOST_OUTCOME_ADDRESS_OF(v._value)) decltype(v._value)(); // NOLINT
s >> v._value; // NOLINT
}
return s;
}
template inline std::istream &operator>>(std::istream &s, value_storage_trivial &v)
{
return value_storage_in(s, v);
}
template inline std::istream &operator>>(std::istream &s, value_storage_nontrivial &v)
{
return value_storage_in(s, v);
}
BOOST_OUTCOME_TEMPLATE(class T)
BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_constructible::value))
inline std::string safe_message(T && /*unused*/)
{
return {};
}
inline std::string safe_message(const std::error_code &ec)
{
return " (" + ec.message() + ")";
}
} // namespace detail
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref() >> detail::lvalueref()),
BOOST_OUTCOME_TEXPR(detail::lvalueref() >> detail::lvalueref()))
inline std::istream &operator>>(std::istream &s, basic_result &v)
{
s >> v._iostreams_state();
if(v.has_error())
{
s >> v.assume_error();
}
return s;
}
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
BOOST_OUTCOME_TEMPLATE(class R, class S, class P)
BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(detail::lvalueref() << detail::lvalueref()),
BOOST_OUTCOME_TEXPR(detail::lvalueref() << detail::lvalueref()))
inline std::ostream &operator<<(std::ostream &s, const basic_result &v)
{
s << v._iostreams_state();
if(v.has_error())
{
s << v.assume_error();
}
return s;
}
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
template inline std::string print(const basic_result &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();
}
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
template inline std::string print(const basic_result &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();
}
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
template inline std::string print(const basic_result &v)
{
std::stringstream s;
if(v.has_value())
{
s << v.value();
}
if(v.has_error())
{
s << "(-void)";
}
return s.str();
}
/*! AWAITING HUGO JSON CONVERSION TOOL
SIGNATURE NOT RECOGNISED
*/
template inline std::string print(const basic_result &v)
{
std::stringstream s;
if(v.has_value())
{
s << "(+void)";
}
if(v.has_error())
{
s << "(-void)";
}
return s.str();
}
BOOST_OUTCOME_V2_NAMESPACE_END
#endif