1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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_AWAIT_SUSPEND_HELPER_HPP
10  
#ifndef BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
11  
#define BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
11  
#define BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
12  

12  

13  
#include <boost/capy/coro.hpp>
13  
#include <boost/capy/coro.hpp>
14  
#include <boost/capy/ex/executor_ref.hpp>
14  
#include <boost/capy/ex/executor_ref.hpp>
15  

15  

16  
#include <stop_token>
16  
#include <stop_token>
17  
#include <type_traits>
17  
#include <type_traits>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace capy {
20  
namespace capy {
21  
namespace detail {
21  
namespace detail {
22  

22  

23  
// Helper to normalize await_suspend return types to coro
23  
// Helper to normalize await_suspend return types to coro
24  
template<typename Awaitable>
24  
template<typename Awaitable>
25  
coro call_await_suspend(
25  
coro call_await_suspend(
26  
    Awaitable* a,
26  
    Awaitable* a,
27  
    coro h,
27  
    coro h,
28  
    executor_ref ex,
28  
    executor_ref ex,
29  
    std::stop_token token)
29  
    std::stop_token token)
30  
{
30  
{
31  
    using R = decltype(a->await_suspend(h, ex, token));
31  
    using R = decltype(a->await_suspend(h, ex, token));
32  
    if constexpr (std::is_void_v<R>)
32  
    if constexpr (std::is_void_v<R>)
33  
    {
33  
    {
34  
        a->await_suspend(h, ex, token);
34  
        a->await_suspend(h, ex, token);
35  
        return std::noop_coroutine();
35  
        return std::noop_coroutine();
36  
    }
36  
    }
37  
    else if constexpr (std::is_same_v<R, bool>)
37  
    else if constexpr (std::is_same_v<R, bool>)
38  
    {
38  
    {
39  
        if(a->await_suspend(h, ex, token))
39  
        if(a->await_suspend(h, ex, token))
40  
            return std::noop_coroutine();
40  
            return std::noop_coroutine();
41  
        return h;
41  
        return h;
42  
    }
42  
    }
43  
    else
43  
    else
44  
    {
44  
    {
45  
        return a->await_suspend(h, ex, token);
45  
        return a->await_suspend(h, ex, token);
46  
    }
46  
    }
47  
}
47  
}
48  

48  

49  
} // namespace detail
49  
} // namespace detail
50  
} // namespace capy
50  
} // namespace capy
51  
} // namespace boost
51  
} // namespace boost
52  

52  

53  
#endif
53  
#endif