packaged_task.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // packaged_task.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. #ifndef BOOST_ASIO_PACKAGED_TASK_HPP
  11. #define BOOST_ASIO_PACKAGED_TASK_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/future.hpp>
  17. #if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/detail/type_traits.hpp>
  21. #include <boost/asio/detail/variadic_templates.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) \
  26. || defined(GENERATING_DOCUMENTATION)
  27. /// Partial specialisation of @c async_result for @c std::packaged_task.
  28. template <typename Result, typename... Args, typename Signature>
  29. class async_result<std::packaged_task<Result(Args...)>, Signature>
  30. {
  31. public:
  32. /// The packaged task is the concrete completion handler type.
  33. typedef std::packaged_task<Result(Args...)> completion_handler_type;
  34. /// The return type of the initiating function is the future obtained from
  35. /// the packaged task.
  36. typedef std::future<Result> return_type;
  37. /// The constructor extracts the future from the packaged task.
  38. explicit async_result(completion_handler_type& h)
  39. : future_(h.get_future())
  40. {
  41. }
  42. /// Returns the packaged task's future.
  43. return_type get()
  44. {
  45. return std::move(future_);
  46. }
  47. private:
  48. return_type future_;
  49. };
  50. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  51. // || defined(GENERATING_DOCUMENTATION)
  52. template <typename Result, typename Signature>
  53. struct async_result<std::packaged_task<Result()>, Signature>
  54. {
  55. typedef std::packaged_task<Result()> completion_handler_type;
  56. typedef std::future<Result> return_type;
  57. explicit async_result(completion_handler_type& h)
  58. : future_(h.get_future())
  59. {
  60. }
  61. return_type get()
  62. {
  63. return std::move(future_);
  64. }
  65. private:
  66. return_type future_;
  67. };
  68. #define BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
  69. template <typename Result, \
  70. BOOST_ASIO_VARIADIC_TPARAMS(n), typename Signature> \
  71. class async_result< \
  72. std::packaged_task<Result(BOOST_ASIO_VARIADIC_TARGS(n))>, Signature> \
  73. { \
  74. public: \
  75. typedef std::packaged_task< \
  76. Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
  77. completion_handler_type; \
  78. \
  79. typedef std::future<Result> return_type; \
  80. \
  81. explicit async_result(completion_handler_type& h) \
  82. : future_(h.get_future()) \
  83. { \
  84. } \
  85. \
  86. return_type get() \
  87. { \
  88. return std::move(future_); \
  89. } \
  90. \
  91. private: \
  92. return_type future_; \
  93. }; \
  94. /**/
  95. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF)
  96. #undef BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF
  97. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  98. // || defined(GENERATING_DOCUMENTATION)
  99. } // namespace asio
  100. } // namespace boost
  101. #include <boost/asio/detail/pop_options.hpp>
  102. #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
  103. // || defined(GENERATING_DOCUMENTATION)
  104. #endif // BOOST_ASIO_PACKAGED_TASK_HPP