100.00% Lines (29/29) 100.00% Functions (9/9)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #ifndef BOOST_CAPY_DETAIL_RUN_CALLBACKS_HPP 10   #ifndef BOOST_CAPY_DETAIL_RUN_CALLBACKS_HPP
11   #define BOOST_CAPY_DETAIL_RUN_CALLBACKS_HPP 11   #define BOOST_CAPY_DETAIL_RUN_CALLBACKS_HPP
12   12  
13   #include <boost/capy/detail/config.hpp> 13   #include <boost/capy/detail/config.hpp>
14   #include <boost/capy/detail/stop_requested_exception.hpp> 14   #include <boost/capy/detail/stop_requested_exception.hpp>
15   15  
16   #include <concepts> 16   #include <concepts>
17   #include <exception> 17   #include <exception>
18   #include <type_traits> 18   #include <type_traits>
19   #include <utility> 19   #include <utility>
20   20  
21   namespace boost { 21   namespace boost {
22   namespace capy { 22   namespace capy {
23   namespace detail { 23   namespace detail {
24   24  
25   struct default_handler 25   struct default_handler
26   { 26   {
27   template<class T> 27   template<class T>
HITCBC 28   3 void operator()(T&&) const noexcept 28   3 void operator()(T&&) const noexcept
29   { 29   {
HITCBC 30   3 } 30   3 }
31   31  
HITCBC 32   1847 void operator()() const noexcept 32   1853 void operator()() const noexcept
33   { 33   {
HITCBC 34   1847 } 34   1853 }
35   35  
HITCBC 36   1034 void operator()(std::exception_ptr ep) const 36   1034 void operator()(std::exception_ptr ep) const
37   { 37   {
HITCBC 38   1034 if(!ep) 38   1034 if(!ep)
HITCBC 39   1 return; 39   1 return;
40   try 40   try
41   { 41   {
HITCBC 42   2066 std::rethrow_exception(ep); 42   2066 std::rethrow_exception(ep);
43   } 43   }
HITCBC 44   1033 catch(stop_requested_exception const&) 44   1033 catch(stop_requested_exception const&)
45   { 45   {
46   // Cancellation is a normal completion, not an error. 46   // Cancellation is a normal completion, not an error.
HITCBC 47   5 } 47   5 }
48   // A real unhandled exception propagates to the trampoline's 48   // A real unhandled exception propagates to the trampoline's
49   // unhandled_exception, which terminates. 49   // unhandled_exception, which terminates.
50   } 50   }
51   }; 51   };
52   52  
53   template<class H1, class H2> 53   template<class H1, class H2>
54   struct handler_pair 54   struct handler_pair
55   { 55   {
56   static_assert( 56   static_assert(
57   std::is_nothrow_move_constructible_v<H1> && 57   std::is_nothrow_move_constructible_v<H1> &&
58   std::is_nothrow_move_constructible_v<H2>, 58   std::is_nothrow_move_constructible_v<H2>,
59   "Handlers must be nothrow move constructible"); 59   "Handlers must be nothrow move constructible");
60   60  
61   H1 h1_; 61   H1 h1_;
62   H2 h2_; 62   H2 h2_;
63   63  
64   template<class T> 64   template<class T>
HITCBC 65   80 void operator()(T&& v) 65   80 void operator()(T&& v)
66   { 66   {
HITCBC 67   80 h1_(std::forward<T>(v)); 67   80 h1_(std::forward<T>(v));
HITCBC 68   80 } 68   80 }
69   69  
HITCBC 70   21 void operator()() 70   21 void operator()()
71   { 71   {
HITCBC 72   21 h1_(); 72   21 h1_();
HITCBC 73   21 } 73   21 }
74   74  
HITCBC 75   31 void operator()(std::exception_ptr ep) 75   31 void operator()(std::exception_ptr ep)
76   { 76   {
HITCBC 77   31 h2_(ep); 77   31 h2_(ep);
HITCBC 78   31 } 78   31 }
79   }; 79   };
80   80  
81   template<class H1> 81   template<class H1>
82   struct handler_pair<H1, default_handler> 82   struct handler_pair<H1, default_handler>
83   { 83   {
84   static_assert( 84   static_assert(
85   std::is_nothrow_move_constructible_v<H1>, 85   std::is_nothrow_move_constructible_v<H1>,
86   "Handler must be nothrow move constructible"); 86   "Handler must be nothrow move constructible");
87   87  
88   H1 h1_; 88   H1 h1_;
89   89  
90   template<class T> 90   template<class T>
HITCBC 91   137 void operator()(T&& v) 91   137 void operator()(T&& v)
92   { 92   {
HITCBC 93   137 h1_(std::forward<T>(v)); 93   137 h1_(std::forward<T>(v));
HITCBC 94   137 } 94   137 }
95   95  
HITCBC 96   3575 void operator()() 96   3575 void operator()()
97   { 97   {
HITCBC 98   3575 h1_(); 98   3575 h1_();
HITCBC 99   3575 } 99   3575 }
100   100  
HITCBC 101   2059 void operator()(std::exception_ptr ep) 101   2059 void operator()(std::exception_ptr ep)
102   { 102   {
103   if constexpr(std::invocable<H1, std::exception_ptr>) 103   if constexpr(std::invocable<H1, std::exception_ptr>)
HITCBC 104   2058 h1_(ep); 104   2058 h1_(ep);
105   else 105   else
HITCBC 106   2 default_handler{}(ep); 106   2 default_handler{}(ep);
HITCBC 107   1032 } 107   1032 }
108   }; 108   };
109   109  
110   } // namespace detail 110   } // namespace detail
111   } // namespace capy 111   } // namespace capy
112   } // namespace boost 112   } // namespace boost
113   113  
114   #endif 114   #endif