Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:32

0001 //
0002 // associated_cancellation_slot.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_ASSOCIATED_CANCELLATION_SLOT_HPP
0012 #define BOOST_ASIO_ASSOCIATED_CANCELLATION_SLOT_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/associator.hpp>
0020 #include <boost/asio/cancellation_signal.hpp>
0021 #include <boost/asio/detail/functional.hpp>
0022 #include <boost/asio/detail/type_traits.hpp>
0023 
0024 #include <boost/asio/detail/push_options.hpp>
0025 
0026 namespace boost {
0027 namespace asio {
0028 
0029 template <typename T, typename CancellationSlot>
0030 struct associated_cancellation_slot;
0031 
0032 namespace detail {
0033 
0034 template <typename T, typename = void>
0035 struct has_cancellation_slot_type : false_type
0036 {
0037 };
0038 
0039 template <typename T>
0040 struct has_cancellation_slot_type<T, void_t<typename T::cancellation_slot_type>>
0041   : true_type
0042 {
0043 };
0044 
0045 template <typename T, typename S, typename = void, typename = void>
0046 struct associated_cancellation_slot_impl
0047 {
0048   typedef void asio_associated_cancellation_slot_is_unspecialised;
0049 
0050   typedef S type;
0051 
0052   static type get(const T&) noexcept
0053   {
0054     return type();
0055   }
0056 
0057   static const type& get(const T&, const S& s) noexcept
0058   {
0059     return s;
0060   }
0061 };
0062 
0063 template <typename T, typename S>
0064 struct associated_cancellation_slot_impl<T, S,
0065   void_t<typename T::cancellation_slot_type>>
0066 {
0067   typedef typename T::cancellation_slot_type type;
0068 
0069   static auto get(const T& t) noexcept
0070     -> decltype(t.get_cancellation_slot())
0071   {
0072     return t.get_cancellation_slot();
0073   }
0074 
0075   static auto get(const T& t, const S&) noexcept
0076     -> decltype(t.get_cancellation_slot())
0077   {
0078     return t.get_cancellation_slot();
0079   }
0080 };
0081 
0082 template <typename T, typename S>
0083 struct associated_cancellation_slot_impl<T, S,
0084   enable_if_t<
0085     !has_cancellation_slot_type<T>::value
0086   >,
0087   void_t<
0088     typename associator<associated_cancellation_slot, T, S>::type
0089   >> : associator<associated_cancellation_slot, T, S>
0090 {
0091 };
0092 
0093 } // namespace detail
0094 
0095 /// Traits type used to obtain the cancellation_slot associated with an object.
0096 /**
0097  * A program may specialise this traits type if the @c T template parameter in
0098  * the specialisation is a user-defined type. The template parameter @c
0099  * CancellationSlot shall be a type meeting the CancellationSlot requirements.
0100  *
0101  * Specialisations shall meet the following requirements, where @c t is a const
0102  * reference to an object of type @c T, and @c s is an object of type @c
0103  * CancellationSlot.
0104  *
0105  * @li Provide a nested typedef @c type that identifies a type meeting the
0106  * CancellationSlot requirements.
0107  *
0108  * @li Provide a noexcept static member function named @c get, callable as @c
0109  * get(t) and with return type @c type or a (possibly const) reference to @c
0110  * type.
0111  *
0112  * @li Provide a noexcept static member function named @c get, callable as @c
0113  * get(t,s) and with return type @c type or a (possibly const) reference to @c
0114  * type.
0115  */
0116 template <typename T, typename CancellationSlot = cancellation_slot>
0117 struct associated_cancellation_slot
0118 #if !defined(GENERATING_DOCUMENTATION)
0119   : detail::associated_cancellation_slot_impl<T, CancellationSlot>
0120 #endif // !defined(GENERATING_DOCUMENTATION)
0121 {
0122 #if defined(GENERATING_DOCUMENTATION)
0123   /// If @c T has a nested type @c cancellation_slot_type,
0124   /// <tt>T::cancellation_slot_type</tt>. Otherwise
0125   /// @c CancellationSlot.
0126   typedef see_below type;
0127 
0128   /// If @c T has a nested type @c cancellation_slot_type, returns
0129   /// <tt>t.get_cancellation_slot()</tt>. Otherwise returns @c type().
0130   static decltype(auto) get(const T& t) noexcept;
0131 
0132   /// If @c T has a nested type @c cancellation_slot_type, returns
0133   /// <tt>t.get_cancellation_slot()</tt>. Otherwise returns @c s.
0134   static decltype(auto) get(const T& t,
0135       const CancellationSlot& s) noexcept;
0136 #endif // defined(GENERATING_DOCUMENTATION)
0137 };
0138 
0139 /// Helper function to obtain an object's associated cancellation_slot.
0140 /**
0141  * @returns <tt>associated_cancellation_slot<T>::get(t)</tt>
0142  */
0143 template <typename T>
0144 BOOST_ASIO_NODISCARD inline typename associated_cancellation_slot<T>::type
0145 get_associated_cancellation_slot(const T& t) noexcept
0146 {
0147   return associated_cancellation_slot<T>::get(t);
0148 }
0149 
0150 /// Helper function to obtain an object's associated cancellation_slot.
0151 /**
0152  * @returns <tt>associated_cancellation_slot<T,
0153  * CancellationSlot>::get(t, st)</tt>
0154  */
0155 template <typename T, typename CancellationSlot>
0156 BOOST_ASIO_NODISCARD inline auto get_associated_cancellation_slot(
0157     const T& t, const CancellationSlot& st) noexcept
0158   -> decltype(associated_cancellation_slot<T, CancellationSlot>::get(t, st))
0159 {
0160   return associated_cancellation_slot<T, CancellationSlot>::get(t, st);
0161 }
0162 
0163 template <typename T, typename CancellationSlot = cancellation_slot>
0164 using associated_cancellation_slot_t =
0165   typename associated_cancellation_slot<T, CancellationSlot>::type;
0166 
0167 namespace detail {
0168 
0169 template <typename T, typename S, typename = void>
0170 struct associated_cancellation_slot_forwarding_base
0171 {
0172 };
0173 
0174 template <typename T, typename S>
0175 struct associated_cancellation_slot_forwarding_base<T, S,
0176     enable_if_t<
0177       is_same<
0178         typename associated_cancellation_slot<T,
0179           S>::asio_associated_cancellation_slot_is_unspecialised,
0180         void
0181       >::value
0182     >>
0183 {
0184   typedef void asio_associated_cancellation_slot_is_unspecialised;
0185 };
0186 
0187 } // namespace detail
0188 
0189 /// Specialisation of associated_cancellation_slot for @c
0190 /// std::reference_wrapper.
0191 template <typename T, typename CancellationSlot>
0192 struct associated_cancellation_slot<reference_wrapper<T>, CancellationSlot>
0193 #if !defined(GENERATING_DOCUMENTATION)
0194   : detail::associated_cancellation_slot_forwarding_base<T, CancellationSlot>
0195 #endif // !defined(GENERATING_DOCUMENTATION)
0196 {
0197   /// Forwards @c type to the associator specialisation for the unwrapped type
0198   /// @c T.
0199   typedef typename associated_cancellation_slot<T, CancellationSlot>::type type;
0200 
0201   /// Forwards the request to get the cancellation slot to the associator
0202   /// specialisation for the unwrapped type @c T.
0203   static type get(reference_wrapper<T> t) noexcept
0204   {
0205     return associated_cancellation_slot<T, CancellationSlot>::get(t.get());
0206   }
0207 
0208   /// Forwards the request to get the cancellation slot to the associator
0209   /// specialisation for the unwrapped type @c T.
0210   static auto get(reference_wrapper<T> t, const CancellationSlot& s) noexcept
0211     -> decltype(
0212       associated_cancellation_slot<T, CancellationSlot>::get(t.get(), s))
0213   {
0214     return associated_cancellation_slot<T, CancellationSlot>::get(t.get(), s);
0215   }
0216 };
0217 
0218 } // namespace asio
0219 } // namespace boost
0220 
0221 #include <boost/asio/detail/pop_options.hpp>
0222 
0223 #endif // BOOST_ASIO_ASSOCIATED_CANCELLATION_SLOT_HPP