| 1 | // |
| 2 | // Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net) |
| 3 | // |
| 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) |
| 6 | // |
| 7 | |
| 8 | #ifndef BOOST_COBALT_DETAIL_THIS_THREAD_HPP |
| 9 | #define BOOST_COBALT_DETAIL_THIS_THREAD_HPP |
| 10 | |
| 11 | #include <boost/cobalt/this_thread.hpp> |
| 12 | |
| 13 | #include <boost/asio/uses_executor.hpp> |
| 14 | #include <boost/mp11/algorithm.hpp> |
| 15 | |
| 16 | namespace boost::cobalt::detail |
| 17 | { |
| 18 | |
| 19 | inline executor |
| 20 | (executor exec) { return exec; } |
| 21 | |
| 22 | #if defined(BOOST_COBALT_CUSTOM_EXECUTOR) || defined(BOOST_COBALT_USE_IO_CONTEXT) |
| 23 | BOOST_COBALT_DECL executor |
| 24 | extract_executor(asio::any_io_executor exec); |
| 25 | #endif |
| 26 | |
| 27 | template<typename ... Args> |
| 28 | executor get_executor_from_args(Args &&... args) |
| 29 | { |
| 30 | using args_type = mp11::mp_list<std::decay_t<Args>...>; |
| 31 | constexpr static auto I = mp11::mp_find<args_type, asio::executor_arg_t>::value; |
| 32 | if constexpr (sizeof...(Args) == I) |
| 33 | return this_thread::get_executor(); |
| 34 | else // |
| 35 | return extract_executor(std::get<I + 1u>(std::tie(args...))); |
| 36 | } |
| 37 | |
| 38 | #if !defined(BOOST_COBALT_NO_PMR) |
| 39 | template<typename ... Args> |
| 40 | pmr::memory_resource * get_memory_resource_from_args(Args &&... args) |
| 41 | { |
| 42 | using args_type = mp11::mp_list<std::decay_t<Args>...>; |
| 43 | constexpr static auto I = mp11::mp_find<args_type, std::allocator_arg_t>::value; |
| 44 | if constexpr (sizeof...(Args) == I) |
| 45 | return this_thread::get_default_resource(); |
| 46 | else // |
| 47 | return std::get<I + 1u>(std::tie(args...)).resource(); |
| 48 | } |
| 49 | |
| 50 | template<typename ... Args> |
| 51 | pmr::memory_resource * get_memory_resource_from_args_global(Args &&... args) |
| 52 | { |
| 53 | using args_type = mp11::mp_list<std::decay_t<Args>...>; |
| 54 | constexpr static auto I = mp11::mp_find<args_type, std::allocator_arg_t>::value; |
| 55 | if constexpr (sizeof...(Args) == I) |
| 56 | return pmr::get_default_resource(); |
| 57 | else // |
| 58 | return std::get<I + 1u>(std::tie(args...)).resource(); |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | } |
| 63 | |
| 64 | #endif //BOOST_COBALT_DETAIL_THIS_THREAD_HPP |
| 65 | |