Skip to content

Commit

Permalink
Changed on_error deferred behavior to not invocate the function unles…
Browse files Browse the repository at this point in the history
…s storage is available for the error type
  • Loading branch information
zajo committed Jan 8, 2024
1 parent cba8793 commit 8995495
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
34 changes: 27 additions & 7 deletions include/boost/leaf/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ namespace leaf_detail
}

template <bool OnError, class E, class F>
inline void dynamic_accumulate( int err_id, F && f ) noexcept(OnError)
inline void dynamic_load_accumulate( int err_id, F && f ) noexcept(OnError)
{
if( OnError )
{
Expand Down Expand Up @@ -512,9 +512,9 @@ namespace leaf_detail
template <bool OnError, class E>
BOOST_LEAF_CONSTEXPR inline int load_slot( int err_id, E && e ) noexcept(OnError)
{
static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
static_assert(!std::is_same<typename std::decay<E>::type, error_id>::value, "Error objects of type error_id are not allowed");
using T = typename std::decay<E>::type;
static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
static_assert(!std::is_same<T, error_id>::value, "Error objects of type error_id are not allowed");
BOOST_LEAF_ASSERT((err_id&3)==1);
if( slot<T> * p = tls::read_ptr<slot<T>>() )
{
Expand All @@ -529,7 +529,27 @@ namespace leaf_detail
}

template <bool OnError, class F>
BOOST_LEAF_CONSTEXPR inline int accumulate_slot( int err_id, F && f ) noexcept(OnError)
BOOST_LEAF_CONSTEXPR inline int load_slot_deferred( int err_id, F && f ) noexcept(OnError)
{
using E = typename function_traits<F>::return_type;
using T = typename std::decay<E>::type;
static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");
static_assert(!std::is_same<T, error_id>::value, "Error objects of type error_id are not allowed");
BOOST_LEAF_ASSERT((err_id&3)==1);
if( slot<T> * p = tls::read_ptr<slot<T>>() )
{
if( !OnError || !p->has_value(err_id) )
(void) p->load(err_id, std::forward<F>(f)());
}
#if BOOST_LEAF_CFG_CAPTURE
else
dynamic_load<OnError>(err_id, std::forward<F>(f)());
#endif
return 0;
}

template <bool OnError, class F>
BOOST_LEAF_CONSTEXPR inline int load_slot_accumulate( int err_id, F && f ) noexcept(OnError)
{
static_assert(function_traits<F>::arity==1, "Lambdas passed to accumulate must take a single e-type argument by reference");
using E = typename std::decay<fn_arg_type<F,0>>::type;
Expand All @@ -544,7 +564,7 @@ namespace leaf_detail
}
#if BOOST_LEAF_CFG_CAPTURE
else
dynamic_accumulate<OnError, E>(err_id, std::forward<F>(f));
dynamic_load_accumulate<OnError, E>(err_id, std::forward<F>(f));
#endif
return 0;
}
Expand Down Expand Up @@ -578,7 +598,7 @@ namespace leaf_detail
{
BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
{
return load_slot<false>(err_id, std::forward<F>(f)());
return load_slot_deferred<false>(err_id, std::forward<F>(f));
}
};

Expand All @@ -587,7 +607,7 @@ namespace leaf_detail
{
BOOST_LEAF_CONSTEXPR static int load_( int err_id, F && f ) noexcept
{
return accumulate_slot<false>(err_id, std::forward<F>(f));
return load_slot_accumulate<false>(err_id, std::forward<F>(f));
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions include/boost/leaf/on_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ namespace leaf_detail

BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
{
(void) load_slot<true>(err_id, f_());
(void) load_slot_deferred<true>(err_id, f_);
}
};

Expand Down Expand Up @@ -160,7 +160,7 @@ namespace leaf_detail

BOOST_LEAF_CONSTEXPR void trigger( int err_id ) noexcept
{
accumulate_slot<true>(err_id, std::move(f_));
load_slot_accumulate<true>(err_id, std::move(f_));
}
};

Expand Down

0 comments on commit 8995495

Please sign in to comment.