|
| 1 | +// |
| 2 | +// associated_executor.hpp |
| 3 | +// ~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +// |
| 5 | +// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
| 6 | +// |
| 7 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 8 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 9 | +// |
| 10 | + |
| 11 | +#ifndef ASIO_ASSOCIATED_EXECUTOR_HPP |
| 12 | +#define ASIO_ASSOCIATED_EXECUTOR_HPP |
| 13 | + |
| 14 | +#if defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 15 | +# pragma once |
| 16 | +#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 17 | + |
| 18 | +#include "asio/detail/config.hpp" |
| 19 | +#include "asio/detail/type_traits.hpp" |
| 20 | +#include "asio/is_executor.hpp" |
| 21 | +#include "asio/system_executor.hpp" |
| 22 | + |
| 23 | +#include "asio/detail/push_options.hpp" |
| 24 | + |
| 25 | +namespace asio { |
| 26 | +namespace detail { |
| 27 | + |
| 28 | +template <typename> |
| 29 | +struct associated_executor_check |
| 30 | +{ |
| 31 | + typedef void type; |
| 32 | +}; |
| 33 | + |
| 34 | +template <typename T, typename E, typename = void> |
| 35 | +struct associated_executor_impl |
| 36 | +{ |
| 37 | + typedef E type; |
| 38 | + |
| 39 | + static type get(const T&, const E& e) ASIO_NOEXCEPT |
| 40 | + { |
| 41 | + return e; |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +template <typename T, typename E> |
| 46 | +struct associated_executor_impl<T, E, |
| 47 | + typename associated_executor_check<typename T::executor_type>::type> |
| 48 | +{ |
| 49 | + typedef typename T::executor_type type; |
| 50 | + |
| 51 | + static type get(const T& t, const E&) ASIO_NOEXCEPT |
| 52 | + { |
| 53 | + return t.get_executor(); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +} // namespace detail |
| 58 | + |
| 59 | +/// Traits type used to obtain the executor associated with an object. |
| 60 | +/** |
| 61 | + * A program may specialise this traits type if the @c T template parameter in |
| 62 | + * the specialisation is a user-defined type. The template parameter @c |
| 63 | + * Executor shall be a type meeting the Executor requirements. |
| 64 | + * |
| 65 | + * Specialisations shall meet the following requirements, where @c t is a const |
| 66 | + * reference to an object of type @c T, and @c e is an object of type @c |
| 67 | + * Executor. |
| 68 | + * |
| 69 | + * @li Provide a nested typedef @c type that identifies a type meeting the |
| 70 | + * Executor requirements. |
| 71 | + * |
| 72 | + * @li Provide a noexcept static member function named @c get, callable as @c |
| 73 | + * get(t) and with return type @c type. |
| 74 | + * |
| 75 | + * @li Provide a noexcept static member function named @c get, callable as @c |
| 76 | + * get(t,e) and with return type @c type. |
| 77 | + */ |
| 78 | +template <typename T, typename Executor = system_executor> |
| 79 | +struct associated_executor |
| 80 | +{ |
| 81 | + /// If @c T has a nested type @c executor_type, <tt>T::executor_type</tt>. |
| 82 | + /// Otherwise @c Executor. |
| 83 | +#if defined(GENERATING_DOCUMENTATION) |
| 84 | + typedef see_below type; |
| 85 | +#else // defined(GENERATING_DOCUMENTATION) |
| 86 | + typedef typename detail::associated_executor_impl<T, Executor>::type type; |
| 87 | +#endif // defined(GENERATING_DOCUMENTATION) |
| 88 | + |
| 89 | + /// If @c T has a nested type @c executor_type, returns |
| 90 | + /// <tt>t.get_executor()</tt>. Otherwise returns @c ex. |
| 91 | + static type get(const T& t, |
| 92 | + const Executor& ex = Executor()) ASIO_NOEXCEPT |
| 93 | + { |
| 94 | + return detail::associated_executor_impl<T, Executor>::get(t, ex); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +/// Helper function to obtain an object's associated executor. |
| 99 | +/** |
| 100 | + * @returns <tt>associated_executor<T>::get(t)</tt> |
| 101 | + */ |
| 102 | +template <typename T> |
| 103 | +inline typename associated_executor<T>::type |
| 104 | +get_associated_executor(const T& t) ASIO_NOEXCEPT |
| 105 | +{ |
| 106 | + return associated_executor<T>::get(t); |
| 107 | +} |
| 108 | + |
| 109 | +/// Helper function to obtain an object's associated executor. |
| 110 | +/** |
| 111 | + * @returns <tt>associated_executor<T, Executor>::get(t, ex)</tt> |
| 112 | + */ |
| 113 | +template <typename T, typename Executor> |
| 114 | +inline typename associated_executor<T, Executor>::type |
| 115 | +get_associated_executor(const T& t, const Executor& ex, |
| 116 | + typename enable_if<is_executor< |
| 117 | + Executor>::value>::type* = 0) ASIO_NOEXCEPT |
| 118 | +{ |
| 119 | + return associated_executor<T, Executor>::get(t, ex); |
| 120 | +} |
| 121 | + |
| 122 | +/// Helper function to obtain an object's associated executor. |
| 123 | +/** |
| 124 | + * @returns <tt>associated_executor<T, typename |
| 125 | + * ExecutionContext::executor_type>::get(t, ctx.get_executor())</tt> |
| 126 | + */ |
| 127 | +template <typename T, typename ExecutionContext> |
| 128 | +inline typename associated_executor<T, |
| 129 | + typename ExecutionContext::executor_type>::type |
| 130 | +get_associated_executor(const T& t, ExecutionContext& ctx, |
| 131 | + typename enable_if<is_convertible<ExecutionContext&, |
| 132 | + execution_context&>::value>::type* = 0) ASIO_NOEXCEPT |
| 133 | +{ |
| 134 | + return associated_executor<T, |
| 135 | + typename ExecutionContext::executor_type>::get(t, ctx.get_executor()); |
| 136 | +} |
| 137 | + |
| 138 | +#if defined(ASIO_HAS_ALIAS_TEMPLATES) |
| 139 | + |
| 140 | +template <typename T, typename Executor = system_executor> |
| 141 | +using associated_executor_t = typename associated_executor<T, Executor>::type; |
| 142 | + |
| 143 | +#endif // defined(ASIO_HAS_ALIAS_TEMPLATES) |
| 144 | + |
| 145 | +} // namespace asio |
| 146 | + |
| 147 | +#include "asio/detail/pop_options.hpp" |
| 148 | + |
| 149 | +#endif // ASIO_ASSOCIATED_EXECUTOR_HPP |
0 commit comments