UDT serialization of optionals

This commit is contained in:
Dmitry Arkhipov 2024-07-02 14:44:22 +03:00
parent 4a5e6bbccf
commit 8f7b1edef9
2 changed files with 62 additions and 0 deletions

View File

@ -46,6 +46,10 @@ suspend(state st, U u, T const* pt)
return false;
}
template<class T, bool StackEmpty>
bool
write_impl(writer& w, stream& ss);
template<class T, bool StackEmpty>
BOOST_FORCEINLINE
bool
@ -714,6 +718,55 @@ write_impl(variant_conversion_tag, writer& w, stream& ss)
}
}
template<class T, bool StackEmpty>
BOOST_FORCEINLINE
bool
write_impl(optional_conversion_tag, writer& w, stream& ss)
{
using Elem = value_result_type<T>;
bool done;
bool has_value;
#if defined(_MSC_VER)
# pragma warning( push )
# pragma warning( disable : 4127 )
#endif
if(StackEmpty || w.st_.empty())
#if defined(_MSC_VER)
# pragma warning( pop )
#endif
{
BOOST_ASSERT( w.p_ );
T const* pt = reinterpret_cast<T const*>(w.p_);
has_value = static_cast<bool>(*pt);
if( has_value )
{
w.p_ = std::addressof( *(*pt) );
done = write_impl<Elem, true>(w, ss);
}
else
{
w.p_ = nullptr;
done = write_impl<std::nullptr_t, true>(w, ss);;
}
}
else
{
w.st_.pop(has_value);
if( has_value )
done = write_impl<Elem, false>(w, ss);
else
done = write_impl<std::nullptr_t, false>(w, ss);
}
if(BOOST_JSON_UNLIKELY( !done ))
w.st_.push(has_value);
return done;
}
template<class T, bool StackEmpty>
bool
write_impl(writer& w, stream& ss)

View File

@ -857,6 +857,15 @@ public:
check_udt(std::monostate(), "null");
}
#endif // BOOST_NO_CXX17_HDR_VARIANT
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
{
std::optional<int> o;
check_udt( o, "null" );
o = 2315;
check_udt( o, "2315" );
}
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
}
void