this_thread.hpp 1.9 KB

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