Skip to content

Commit

Permalink
New dynamic capture system: try_capture_all. Previous API based on ma…
Browse files Browse the repository at this point in the history
…ke_shared_contex/capture is deprecated.
  • Loading branch information
zajo committed Jan 7, 2024
1 parent 971ecac commit 971141d
Show file tree
Hide file tree
Showing 27 changed files with 578 additions and 805 deletions.
314 changes: 81 additions & 233 deletions doc/leaf.adoc

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion doc/zajo-dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ a:hover{color:#00cc99}
a:focus{color:#FFFFFF}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Quicksand","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#00cc99;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.4em}
code{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;color:black}
*:not(pre)>code{font-size:1.08em;font-style:normal!important;letter-spacing:0;padding:0 0;word-spacing:-.15em;background-color:transparent;-webkit-border-radius:0;border-radius:0;line-height:1.45;text-rendering:optimizeLegibility;word-wrap:break-word;color:white}
*:not(pre)>code{font-size:1.0em;font-style:normal!important;letter-spacing:0;padding:0 0;word-spacing:-.15em;background-color:transparent;-webkit-border-radius:0;border-radius:0;line-height:1.45;text-rendering:optimizeLegibility;word-wrap:break-word;color:white}
pre,pre>code{line-height:1.45;color:rgba(255,255,255,.67);font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeLegibility;font-size:1.05em;background-color:#101010}
a:not(pre)>code:hover {color:#00cc99}
kbd{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
Expand Down
2 changes: 1 addition & 1 deletion doc/zajo-light.css
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ a:hover{color:#4101a7}
a:focus{color:#000000}
h1,h2,h3,#toctitle,.sidebarblock>.content>.title,h4,h5,h6{font-family:"Quicksand","DejaVu Sans",sans-serif;font-weight:300;font-style:normal;color:#4101a7;text-rendering:optimizeLegibility;margin-top:1em;margin-bottom:.5em;line-height:1.4em}
code{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;color:black}
*:not(pre)>code{font-size:1.08em;font-style:normal!important;letter-spacing:0;padding:0 0;word-spacing:-.15em;background-color:transparent;-webkit-border-radius:0;border-radius:0;line-height:1.45;text-rendering:optimizeLegibility;word-wrap:break-word}
*:not(pre)>code{font-size:1.0em;font-style:normal!important;letter-spacing:0;padding:0 0;word-spacing:-.15em;background-color:transparent;-webkit-border-radius:0;border-radius:0;line-height:1.45;text-rendering:optimizeLegibility;word-wrap:break-word}
pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeLegibility;font-size:1.05em;background-color:#f7f8f7}
a:not(pre)>code:hover {color:#4101a7}
kbd{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
Expand Down
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
121 changes: 121 additions & 0 deletions include/boost/leaf/capture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#ifndef BOOST_LEAF_CAPTURE_HPP_INCLUDED
#define BOOST_LEAF_CAPTURE_HPP_INCLUDED

// Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.

// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/leaf/config.hpp>
#include <boost/leaf/handle_errors.hpp>

#if BOOST_LEAF_CFG_CAPTURE

namespace boost { namespace leaf {

namespace leaf_detail
{
template <class R, bool IsResult = is_result_type<R>::value>
struct is_result_tag;

template <class R>
struct is_result_tag<R, false>
{
};

template <class R>
struct is_result_tag<R, true>
{
};
}

#ifdef BOOST_LEAF_NO_EXCEPTIONS

namespace leaf_detail
{
template <class R, class F, class... A>
inline
decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
capture_impl(is_result_tag<R, false>, F && f, A... a) noexcept
{
return std::forward<F>(f)(std::forward<A>(a)...);
}

template <class R, class Future>
inline
decltype(std::declval<Future>().get())
future_get_impl(is_result_tag<R, false>, Future & fut) noexcept
{
return fut.get();
}
}

#else

namespace leaf_detail
{
// Not defined, no longer supported. Please use try_capture_all instead of make_shared_context/capture.
template <class R, class F, class... A>
decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
capture_impl(is_result_tag<R, false>, F && f, A... a);

// Not defined, no longer supported. Please use try_capture_all instead of make_shared_context/capture.
template <class R, class Future>
decltype(std::declval<Future>().get())
future_get_impl(is_result_tag<R, false>, Future & fut );
}

#endif

namespace leaf_detail
{
template <class R, class F, class... A>
inline
decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
capture_impl(is_result_tag<R, true>, F && f, A... a) noexcept
{
return try_capture_all(
[&]
{
return std::forward<F>(f)(std::forward<A>(a)...);
} );
}

template <class R, class Future>
inline
decltype(std::declval<Future>().get())
future_get_impl(is_result_tag<R, true>, Future & fut) noexcept
{
if( auto r = fut.get() )
return r;
else
{
r.unload();
return r;
}
}
}

template <class F, class... A>
inline
decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))
capture(context_ptr &&, F && f, A... a)
{
using namespace leaf_detail;
return capture_impl(is_result_tag<decltype(std::declval<F>()(std::forward<A>(std::declval<A>())...))>(), std::forward<F>(f), std::forward<A>(a)...);
}

template <class Future>
inline
decltype(std::declval<Future>().get())
future_get( Future & fut )
{
using namespace leaf_detail;
return future_get_impl(is_result_tag<decltype(std::declval<Future>().get())>(), fut);
}

} }

#endif

#endif
10 changes: 10 additions & 0 deletions include/boost/leaf/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@

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

#ifndef BOOST_LEAF_DEPRECATED
# if __cplusplus > 201402L
# define BOOST_LEAF_DEPRECATED(msg) [[deprecated(msg)]]
# else
# define BOOST_LEAF_DEPRECATED(msg)
# endif
#endif

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

#ifndef BOOST_LEAF_NO_EXCEPTIONS
# include <exception>
# if (defined(__cpp_lib_uncaught_exceptions) && __cpp_lib_uncaught_exceptions >= 201411L) || (defined(_MSC_VER) && _MSC_VER >= 1900)
Expand Down
22 changes: 20 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 Expand Up @@ -383,6 +381,26 @@ BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H &&
return { };
}

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

#if BOOST_LEAF_CFG_CAPTURE

template <class...>
BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
inline context_ptr make_shared_context() noexcept
{
return std::make_shared<polymorphic_context>();
}

template <class... H>
BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
inline context_ptr make_shared_context( H && ... ) noexcept
{
return std::make_shared<polymorphic_context>();
}

#endif

} }

#endif
2 changes: 2 additions & 0 deletions include/boost/leaf/detail/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/leaf/config.hpp>
#include <boost/leaf/capture.hpp>
#include <boost/leaf/common.hpp>
#include <boost/leaf/context.hpp>
#include <boost/leaf/error.hpp>
#include <boost/leaf/exception.hpp>
#include <boost/leaf/handle_errors.hpp>
Expand Down
5 changes: 0 additions & 5 deletions include/boost/leaf/detail/capture_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ namespace leaf_detail
} );
}

bool empty() const noexcept
{
return first_ == nullptr;
}

template <class CharT, class Traits>
void print( std::basic_ostream<CharT, Traits> & os, char const * title, int const err_id_to_print ) const
{
Expand Down
31 changes: 14 additions & 17 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 @@ -373,19 +370,8 @@ namespace leaf_detail
} );
}

int size() const noexcept
{
int c = 0;
for_each(
[&]( capture_list::node const & )
{
++c;
} );
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 All @@ -397,7 +383,6 @@ namespace leaf_detail
return { err_id_, capture_list(f) };
}

using capture_list::empty;
using capture_list::print;
};

Expand Down Expand Up @@ -810,6 +795,18 @@ inline error_id current_error() noexcept
return leaf_detail::make_error_id(leaf_detail::current_id());
}

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

class polymorphic_context
{
};

#if BOOST_LEAF_CFG_CAPTURE
using context_ptr = std::shared_ptr<polymorphic_context>;
#endif

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

template <class Ctx>
class context_activator
{
Expand Down
Loading

0 comments on commit 971141d

Please sign in to comment.