Skip to content

Commit

Permalink
Deleted leaf::dynamic_capture, replaced with simpler try_capture_all API
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Dec 30, 2023
1 parent 971ecac commit 92e8143
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 552 deletions.
8 changes: 2 additions & 6 deletions example/dynamic_capture_eh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ int main()
std::launch::async,
[&]
{
return leaf::try_catch(
[&]() -> leaf::result<task_result>
return leaf::try_capture_all(
[&]
{
return task();
},
[]( leaf::dynamic_capture const & cap ) -> leaf::result<task_result>
{
return cap;
} );
} );
} );
Expand Down
8 changes: 2 additions & 6 deletions example/dynamic_capture_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ int main()
std::launch::async,
[&]
{
return leaf::try_handle_some(
[&]() -> leaf::result<task_result>
return leaf::try_capture_all(
[&]
{
return task();
},
[]( leaf::dynamic_capture const & cap ) -> leaf::result<task_result>
{
return cap;
} );
} );
} );
Expand Down
2 changes: 0 additions & 2 deletions include/boost/leaf/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace boost { namespace leaf {
class error_info;
class diagnostic_info;
class verbose_diagnostic_info;
class dynamic_capture;

template <class>
struct is_predicate: std::false_type
Expand Down Expand Up @@ -60,7 +59,6 @@ namespace leaf_detail
static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &");
static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &");
static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &");
static_assert(!std::is_same<E, dynamic_capture>::value, "Handlers must take leaf::dynamic_capture arguments by const &");
};

template <class Pred>
Expand Down
7 changes: 2 additions & 5 deletions include/boost/leaf/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ namespace boost { namespace leaf {

class BOOST_LEAF_SYMBOL_VISIBLE error_id;

template <class T>
class BOOST_LEAF_SYMBOL_VISIBLE result;

namespace leaf_detail
{
struct BOOST_LEAF_SYMBOL_VISIBLE tls_tag_id_factory_current_id;
Expand Down Expand Up @@ -384,8 +381,8 @@ namespace leaf_detail
return c;
}

template <class T>
result<T> extract_capture_list() noexcept
template <class LeafResult>
LeafResult extract_capture_list() noexcept
{
#ifndef BOOST_LEAF_NO_EXCEPTIONS
if( std::exception_ptr ex = std::current_exception() )
Expand Down
185 changes: 107 additions & 78 deletions include/boost/leaf/handle_errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,84 +336,6 @@ namespace leaf_detail

////////////////////////////////////////

#if BOOST_LEAF_CFG_CAPTURE

class dynamic_capture
{
template <class T>
friend class ::boost::leaf::result;

dynamic_capture( dynamic_capture const & ) = delete;
dynamic_capture & operator=( dynamic_capture const & ) = delete;

leaf_detail::dynamic_allocator * const da_;
error_id err_;

protected:

dynamic_capture( dynamic_capture && ) = default;

BOOST_LEAF_CONSTEXPR dynamic_capture( leaf_detail::dynamic_allocator * da, error_id err ) noexcept:
da_(da),
err_(err)
{
}

public:

BOOST_LEAF_CONSTEXPR bool empty() const noexcept
{
return !da_ || da_->empty();
}

BOOST_LEAF_CONSTEXPR int size() const noexcept
{
return da_ ? da_->size() : 0;
}

template <class T>
operator result<T>() const noexcept
{
if( da_ )
return da_->extract_capture_list<T>();
else
return { error_id() };
}

template <class CharT, class Traits>
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, dynamic_capture const & x )
{
if( x.da_ )
x.da_->print(os, "Captured error objects:\n", x.err_.value());
return os;
}
};

namespace leaf_detail
{
struct dynamic_capture_: dynamic_capture
{
BOOST_LEAF_CONSTEXPR dynamic_capture_( leaf_detail::dynamic_allocator * da, error_id err ) noexcept:
dynamic_capture(da, err)
{
}
};

template <>
struct handler_argument_traits<dynamic_capture const &>: handler_argument_always_available<dynamic_allocator>
{
template <class Tup>
BOOST_LEAF_CONSTEXPR static dynamic_capture_ get( Tup & tup, error_info const & ei ) noexcept
{
return dynamic_capture_(handler_argument_traits_defaults<dynamic_allocator>::check(tup, ei), ei.error());
}
};
}

#endif

////////////////////////////////////////

namespace leaf_detail
{
template <class T, class... List>
Expand Down Expand Up @@ -994,6 +916,113 @@ try_catch( TryBlock && try_block, H && ... h )

#endif

#if BOOST_LEAF_CFG_CAPTURE

namespace leaf_detail
{
template <class LeafResult>
struct try_capture_all_dispatch_non_void
{
using leaf_result = LeafResult;

template <class TryBlock>
inline
static
leaf_result
try_capture_all_( TryBlock && try_block ) noexcept
{
leaf_detail::slot<leaf_detail::dynamic_allocator> sl;
sl.activate();
#ifndef BOOST_LEAF_NO_EXCEPTIONS
try
#endif
{
if( leaf_result r = std::forward<TryBlock>(try_block)() )
{
sl.deactivate();
return r;
}
else
{
sl.deactivate();
return leaf_result(sl.value(error_id(r.error()).value()).template extract_capture_list<leaf_result>());
}
}
#ifndef BOOST_LEAF_NO_EXCEPTIONS
catch( std::exception & ex )
{
sl.deactivate();
return sl.value(error_info(&ex).error().value()).template extract_capture_list<leaf_result>();
}
catch(...)
{
sl.deactivate();
return sl.value(error_info(nullptr).error().value()).template extract_capture_list<leaf_result>();
}
#endif
}
};

template <class R, bool IsVoid = std::is_same<void, R>::value, bool IsResultType = is_result_type<R>::value>
struct try_capture_all_dispatch;

template <class R>
struct try_capture_all_dispatch<R, false, true>:
try_capture_all_dispatch_non_void<::boost::leaf::result<typename std::decay<decltype(std::declval<R>().value())>::type>>
{
};

template <class R>
struct try_capture_all_dispatch<R, false, false>:
try_capture_all_dispatch_non_void<::boost::leaf::result<typename std::remove_reference<R>::type>>
{
};

template <class R>
struct try_capture_all_dispatch<R, true, false>
{
using leaf_result = ::boost::leaf::result<R>;

template <class TryBlock>
inline
static
leaf_result
try_capture_all_( TryBlock && try_block ) noexcept
{
leaf_detail::slot<leaf_detail::dynamic_allocator> sl;
sl.activate();
#ifndef BOOST_LEAF_NO_EXCEPTIONS
try
#endif
{
std::forward<TryBlock>(try_block)();
return {};
}
#ifndef BOOST_LEAF_NO_EXCEPTIONS
catch( std::exception & ex )
{
sl.deactivate();
return sl.value(error_info(&ex).error().value()).template extract_capture_list<leaf_result>();
}
catch(...)
{
sl.deactivate();
return sl.value(error_info(nullptr).error().value()).template extract_capture_list<leaf_result>();
}
#endif
}
};
}

template <class TryBlock>
inline
typename leaf_detail::try_capture_all_dispatch<decltype(std::declval<TryBlock>()())>::leaf_result
try_capture_all( TryBlock && try_block ) noexcept
{
return leaf_detail::try_capture_all_dispatch<decltype(std::declval<TryBlock>()())>::try_capture_all_(std::forward<TryBlock>(try_block));
}
#endif

} }

// Boost Exception Integration
Expand Down
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ if option_enable_unit_tests
'diagnostic_info_test3',
'diagnostic_info_test4',
'diagnostic_info_test5',
'dynamic_capture_print_test',
'dynamic_capture_test',
'e_errno_test',
'e_LastError_test',
Expand Down
10 changes: 3 additions & 7 deletions test/capture_exception_async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ std::vector<fut_info> launch_tasks( int task_count, F f )
return fut_info { a, b, res, std::async( std::launch::async,
[=]
{
return leaf::try_handle_some(
[&]() -> leaf::result<int>
return leaf::try_capture_all(
[&]
{
return f(a, b, res);
},
[]( leaf::dynamic_capture const & cap ) -> leaf::result<int>
{
return cap;
} );
} ) };
} );
Expand All @@ -78,7 +74,7 @@ int main()
int received_a, received_b;

auto task =
[]( int a, int b, int res ) -> leaf::result<int>
[]( int a, int b, int res )
{
if( res >= 0 )
return res;
Expand Down
12 changes: 4 additions & 8 deletions test/capture_exception_result_async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ std::vector<fut_info> launch_tasks( int task_count, F f )
return fut_info { a, b, res, std::async( std::launch::async,
[=]
{
return leaf::try_handle_some(
[&]() -> leaf::result<int>
return leaf::try_capture_all(
[&]
{
return f(a, b, res);
},
[]( leaf::dynamic_capture const & cap ) -> leaf::result<int>
{
return cap;
} );
} ) };
} );
Expand All @@ -82,7 +78,7 @@ int main()
if( res >= 0 )
return res;
else
return leaf::new_error( info<1>{a}, info<2>{b}, info<3>{} );
return leaf::new_error( info<1>{a}, info<2>{b}, info<3>{} );
};

auto error_handlers = std::make_tuple(
Expand Down Expand Up @@ -130,7 +126,7 @@ int main()
f.fut.wait();
received_a = received_b = 0;
int r = leaf::try_handle_all(
[&]() -> leaf::result<int>
[&]
{
auto load = leaf::on_error( info<4>{} );

Expand Down
Loading

0 comments on commit 92e8143

Please sign in to comment.