Skip to content

Commit 6071cd5

Browse files
committed
simplify exception wrapping in value_to
1 parent b6c916f commit 6071cd5

File tree

3 files changed

+12
-119
lines changed

3 files changed

+12
-119
lines changed

include/boost/json/detail/value_to.hpp

Lines changed: 10 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -525,55 +525,6 @@ initialize_variant( V&& v, mp11::mp_int<1> )
525525
}
526526
#endif // BOOST_NO_CXX17_HDR_VARIANT
527527

528-
struct locally_prohibit_exceptions
529-
{};
530-
531-
template< class Ctx >
532-
Ctx const&
533-
make_locally_nonthrowing_context(Ctx const& ctx) noexcept
534-
{
535-
return ctx;
536-
}
537-
538-
template< class... Ctxes >
539-
std::tuple<Ctxes...> const&
540-
make_locally_nonthrowing_context(std::tuple<Ctxes...> const& ctx) noexcept
541-
{
542-
return ctx;
543-
}
544-
545-
template< class... Ctxes >
546-
std::tuple<locally_prohibit_exceptions, allow_exceptions, Ctxes...>
547-
make_locally_nonthrowing_context(std::tuple<allow_exceptions, Ctxes...> const& ctx)
548-
noexcept
549-
{
550-
return std::tuple_cat(std::make_tuple( locally_prohibit_exceptions() ), ctx);
551-
}
552-
553-
template< class Ctx >
554-
Ctx const&
555-
remove_local_exception_prohibition(Ctx const& ctx) noexcept
556-
{
557-
return ctx;
558-
}
559-
560-
template< class T, class... Ts, std::size_t... Is>
561-
std::tuple<Ts...>
562-
remove_local_exception_prohibition_helper(
563-
std::tuple<T, Ts...> const& tup,
564-
mp11::index_sequence<Is...>) noexcept
565-
{
566-
return std::tuple<Ts...>( std::get<Is + 1>(tup)... );
567-
}
568-
569-
template< class... Ctxes >
570-
std::tuple<Ctxes...>
571-
remove_local_exception_prohibition(
572-
std::tuple<locally_prohibit_exceptions, Ctxes...> const& ctx) noexcept
573-
{
574-
return remove_local_exception_prohibition_helper(
575-
ctx, mp11::index_sequence_for<Ctxes...>() );
576-
}
577528

578529
template< class T, class Ctx >
579530
struct alternative_converter
@@ -588,9 +539,8 @@ struct alternative_converter
588539
if( res )
589540
return;
590541

591-
auto&& local_ctx = make_locally_nonthrowing_context(ctx);
592542
using V = mp11::mp_at<T, I>;
593-
auto attempt = try_value_to<V>(jv, local_ctx);
543+
auto attempt = try_value_to<V>(jv, ctx);
594544
if( attempt )
595545
{
596546
using cat = variant_construction_category<T, V, I>;
@@ -709,28 +659,6 @@ value_to_impl(
709659
return std::move(*res);
710660
}
711661

712-
template< class Ctx >
713-
std::tuple<allow_exceptions, Ctx>
714-
make_throwing_context(Ctx const& ctx)
715-
{
716-
return std::tuple<allow_exceptions, Ctx>(allow_exceptions(), ctx);
717-
}
718-
719-
template< class... Ctxes >
720-
std::tuple<allow_exceptions, Ctxes...>
721-
make_throwing_context(std::tuple<Ctxes...> const& ctx)
722-
{
723-
return std::tuple_cat(std::make_tuple( allow_exceptions() ), ctx);
724-
}
725-
726-
template< class... Ctxes >
727-
std::tuple<allow_exceptions, Ctxes...> const&
728-
make_throwing_context(std::tuple<allow_exceptions, Ctxes...> const& ctx)
729-
noexcept
730-
{
731-
return ctx;
732-
}
733-
734662
template<
735663
class T,
736664
class Ctx,
@@ -746,11 +674,7 @@ value_to_impl(
746674
value const& jv,
747675
Ctx const& ctx )
748676
{
749-
auto res = tag_invoke(
750-
try_value_to_tag<T>(),
751-
jv,
752-
Sup::get(ctx),
753-
make_throwing_context(ctx));
677+
auto res = tag_invoke(try_value_to_tag<T>(), jv, Sup::get(ctx), ctx);
754678
if( res.has_error() )
755679
throw_system_error( res.error() );
756680
return std::move(*res);
@@ -809,36 +733,17 @@ value_to_impl(
809733
//----------------------------------------------------------
810734
// User-provided conversions; nonthrowing -> throwing
811735

812-
template< class Ctx >
813-
struct does_allow_exceptions : std::false_type
814-
{ };
815-
816-
template< class... Ctxes >
817-
struct does_allow_exceptions< std::tuple<allow_exceptions, Ctxes...> >
818-
: std::true_type
819-
{ };
820-
821-
template< class T, class... Args >
822-
system::result<T>
823-
wrap_conversion_exceptions( std::true_type, value_to_tag<T>, Args&& ... args )
824-
{
825-
return {
826-
boost::system::in_place_value,
827-
tag_invoke( value_to_tag<T>(), static_cast<Args&&>(args)... )};
828-
}
829-
830736
template< class T, class... Args >
831737
system::result<T>
832-
wrap_conversion_exceptions( std::false_type, value_to_tag<T>, Args&& ... args )
738+
wrap_conversion_exceptions( value_to_tag<T>, Args&& ... args )
833739
{
834740
#ifndef BOOST_NO_EXCEPTIONS
835741
try
836742
{
837743
#endif
838-
return wrap_conversion_exceptions(
839-
std::true_type(),
840-
value_to_tag<T>(),
841-
static_cast<Args&&>(args)... );
744+
return {
745+
boost::system::in_place_value,
746+
tag_invoke( value_to_tag<T>(), static_cast<Args&&>(args)... )};
842747
#ifndef BOOST_NO_EXCEPTIONS
843748
}
844749
catch( std::bad_alloc const&)
@@ -865,8 +770,7 @@ mp11::mp_if_c<
865770
value_to_impl(
866771
user_conversion_tag, try_value_to_tag<T>, value const& jv, Ctx const& )
867772
{
868-
return wrap_conversion_exceptions(
869-
does_allow_exceptions<Ctx>(), value_to_tag<T>(), jv);
773+
return wrap_conversion_exceptions(value_to_tag<T>(), jv);
870774
}
871775

872776
template<
@@ -886,8 +790,7 @@ value_to_impl(
886790
value const& jv,
887791
Ctx const& ctx )
888792
{
889-
return wrap_conversion_exceptions(
890-
does_allow_exceptions<Ctx>(), value_to_tag<T>(), jv, Sup::get(ctx) );
793+
return wrap_conversion_exceptions( value_to_tag<T>(), jv, Sup::get(ctx) );
891794
}
892795

893796
template<
@@ -908,11 +811,7 @@ value_to_impl(
908811
Ctx const& ctx )
909812
{
910813
return wrap_conversion_exceptions(
911-
does_allow_exceptions<Ctx>(),
912-
value_to_tag<T>(),
913-
jv,
914-
Sup::get(ctx),
915-
remove_local_exception_prohibition(ctx) );
814+
value_to_tag<T>(), jv, Sup::get(ctx), ctx);
916815
}
917816

918817
// no suitable conversion implementation
@@ -930,8 +829,7 @@ template< class Impl, class T, class Ctx >
930829
T
931830
value_to_impl( Impl impl, value_to_tag<T>, value const& jv, Ctx const& ctx )
932831
{
933-
return value_to_impl(
934-
impl, try_value_to_tag<T>(), jv, make_throwing_context(ctx) ).value();
832+
return value_to_impl(impl, try_value_to_tag<T>(), jv, ctx).value();
935833
}
936834

937835
template< class Ctx, class T >

include/boost/json/impl/conversion.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,6 @@ struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
387387
struct no_context
388388
{};
389389

390-
struct allow_exceptions
391-
{};
392-
393390
template <class T, class Dir>
394391
using can_convert = mp11::mp_not<
395392
std::is_same<

test/value_to.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <map>
2424
#include <unordered_map>
2525
#include <vector>
26-
#include <iostream>
2726

2827
#ifndef BOOST_NO_CXX17_HDR_VARIANT
2928
# include <variant>
@@ -485,10 +484,9 @@ class value_to_test
485484
#endif // BOOST_NO_CXX17_HDR_OPTIONAL
486485
}
487486

488-
BOOST_TEST_THROWS(
487+
BOOST_TEST_THROWS_WITH_LOCATION(
489488
value_to<::value_to_test_ns::T10>(
490-
value{{"n", 0}, {"t3", "t10"}}, ctx... ),
491-
std::invalid_argument);
489+
value{{"n", 0}, {"t3", "t10"}}, ctx... ));
492490
#endif // BOOST_DESCRIBE_CXX14
493491
}
494492

0 commit comments

Comments
 (0)