use_future.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // use_future.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_USE_FUTURE_HPP
  11. #define ASIO_USE_FUTURE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <memory>
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. /// Class used to specify that an asynchronous operation should return a future.
  20. /**
  21. * The use_future_t class is used to indicate that an asynchronous operation
  22. * should return a std::future object. A use_future_t object may be passed as a
  23. * handler to an asynchronous operation, typically using the special value @c
  24. * asio::use_future. For example:
  25. *
  26. * @code std::future<std::size_t> my_future
  27. * = my_socket.async_read_some(my_buffer, asio::use_future); @endcode
  28. *
  29. * The initiating function (async_read_some in the above example) returns a
  30. * future that will receive the result of the operation. If the operation
  31. * completes with an error_code indicating failure, it is converted into a
  32. * system_error and passed back to the caller via the future.
  33. */
  34. template <typename Allocator = std::allocator<void> >
  35. class use_future_t
  36. {
  37. public:
  38. /// The allocator type. The allocator is used when constructing the
  39. /// @c std::promise object for a given asynchronous operation.
  40. typedef Allocator allocator_type;
  41. /// Construct using default-constructed allocator.
  42. ASIO_CONSTEXPR use_future_t()
  43. {
  44. }
  45. /// Construct using specified allocator.
  46. explicit use_future_t(const Allocator& allocator)
  47. : allocator_(allocator)
  48. {
  49. }
  50. /// Specify an alternate allocator.
  51. template <typename OtherAllocator>
  52. use_future_t<OtherAllocator> operator[](const OtherAllocator& allocator) const
  53. {
  54. return use_future_t<OtherAllocator>(allocator);
  55. }
  56. /// Obtain allocator.
  57. allocator_type get_allocator() const
  58. {
  59. return allocator_;
  60. }
  61. private:
  62. Allocator allocator_;
  63. };
  64. /// A special value, similar to std::nothrow.
  65. /**
  66. * See the documentation for asio::use_future_t for a usage example.
  67. */
  68. #if defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  69. constexpr use_future_t<> use_future;
  70. #elif defined(ASIO_MSVC)
  71. __declspec(selectany) use_future_t<> use_future;
  72. #endif
  73. } // namespace asio
  74. #include "asio/detail/pop_options.hpp"
  75. #include "asio/impl/use_future.hpp"
  76. #endif // ASIO_USE_FUTURE_HPP