packaged_task.qbk 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [/
  2. Copyright Oliver Kowalke 2013.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt
  6. ]
  7. [#class_packaged_task]
  8. [section:packaged_task Template `packaged_task<>`]
  9. A __packaged_task__ wraps a callable target that returns a value so that the
  10. return value can be computed asynchronously.
  11. Conventional usage of `packaged_task<>` is like this:
  12. # Instantiate `packaged_task<>` with template arguments matching the signature
  13. of the callable. Pass the callable to the [link packaged_task_packaged_task
  14. constructor].
  15. # Call [member_link packaged_task..get_future] and capture the returned
  16. [template_link future] instance.
  17. # Launch a [class_link fiber] to run the new `packaged_task<>`, passing any
  18. arguments required by the original callable.
  19. # Call [member_link fiber..detach] on the newly-launched `fiber`.
  20. # At some later point, retrieve the result from the `future<>`.
  21. This is, in fact, pretty much what [ns_function_link fibers..async]
  22. encapsulates.
  23. #include <boost/fiber/future/packaged_task.hpp>
  24. namespace boost {
  25. namespace fibers {
  26. template< class R, typename ... Args >
  27. class packaged_task< R( Args ... ) > {
  28. public:
  29. packaged_task() noexcept;
  30. template< typename Fn >
  31. explicit packaged_task( Fn &&);
  32. template< typename Fn, typename __Allocator__ >
  33. packaged_task( __allocator_arg_t__, Allocator const&, Fn &&);
  34. packaged_task( packaged_task &&) noexcept;
  35. packaged_task & operator=( packaged_task &&) noexcept;
  36. packaged_task( packaged_task const&) = delete;
  37. packaged_task & operator=( packaged_task const&) = delete;
  38. ~packaged_task();
  39. void swap( packaged_task &) noexcept;
  40. bool valid() const noexcept;
  41. future< R > get_future();
  42. void operator()( Args ...);
  43. void reset();
  44. };
  45. template< typename Signature >
  46. void swap( packaged_task< Signature > &, packaged_task< Signature > &) noexcept;
  47. }}
  48. [heading Default constructor `packaged_task()`]
  49. packaged_task() noexcept;
  50. [variablelist
  51. [[Effects:] [Constructs an object of class `packaged_task` with no [link
  52. shared_state shared state].]]
  53. [[Throws:] [Nothing.]]
  54. ]
  55. [#packaged_task_packaged_task]
  56. [heading Templated constructor `packaged_task()`]
  57. template< typename Fn >
  58. explicit packaged_task( Fn && fn);
  59. template< typename Fn, typename __Allocator__ >
  60. packaged_task( __allocator_arg_t__, Allocator const& alloc, Fn && fn);
  61. [variablelist
  62. [[Effects:] [Constructs an object of class `packaged_task` with a [link
  63. shared_state shared state] and copies or moves the callable target `fn` to
  64. internal storage.]]
  65. [[Throws:] [Exceptions caused by memory allocation.]]
  66. [[Note:] [The signature of `Fn` should have a return type convertible to `R`.]]
  67. [[See also:] [__allocator_arg_t__]]
  68. ]
  69. [heading Move constructor]
  70. packaged_task( packaged_task && other) noexcept;
  71. [variablelist
  72. [[Effects:] [Creates a packaged_task by moving the [link shared_state shared
  73. state] from `other`.]]
  74. [[Postcondition:] [`other` contains no valid shared state.]]
  75. [[Throws:] [Nothing.]]
  76. ]
  77. [heading Destructor]
  78. ~packaged_task();
  79. [variablelist
  80. [[Effects:] [Destroys `*this` and abandons the [link shared_state shared
  81. state] if shared state is ready; otherwise stores __future_error__ with error
  82. condition __broken_promise__ as if by [member_link promise..set_exception]:
  83. the shared state is set ready.]]
  84. ]
  85. [operator_heading packaged_task..operator_assign..operator=]
  86. packaged_task & operator=( packaged_task && other) noexcept;
  87. [variablelist
  88. [[Effects:] [Transfers the ownership of [link shared_state shared state] to
  89. `*this`.]]
  90. [[Postcondition:] [`other` contains no valid shared state.]]
  91. [[Throws:] [Nothing.]]
  92. ]
  93. [member_heading packaged_task..swap]
  94. void swap( packaged_task & other) noexcept;
  95. [variablelist
  96. [[Effects:] [Swaps the [link shared_state shared state] between other and
  97. `*this`.]]
  98. [[Throws:] [Nothing.]]
  99. ]
  100. [member_heading packaged_task..valid]
  101. bool valid() const noexcept;
  102. [variablelist
  103. [[Effects:] [Returns `true` if `*this` contains a [link shared_state shared
  104. state].]]
  105. [[Throws:] [Nothing.]]
  106. ]
  107. [member_heading packaged_task..get_future]
  108. future< R > get_future();
  109. [variablelist
  110. [[Returns:] [A __future__ with the same [link shared_state shared state].]]
  111. [[Throws:] [__future_error__ with __already_retrieved__ or __no_state__.]]
  112. ]
  113. [operator_heading packaged_task..operator_apply..operator()]
  114. void operator()( Args && ... args);
  115. [variablelist
  116. [[Effects:] [Invokes the stored callable target. Any exception thrown by the
  117. callable target `fn` is stored in the [link shared_state shared state] as if
  118. by [member_link promise..set_exception]. Otherwise, the value returned by `fn`
  119. is stored in the shared state as if by [member_link promise..set_value].]]
  120. [[Throws:] [__future_error__ with __no_state__.]]
  121. ]
  122. [member_heading packaged_task..reset]
  123. void reset();
  124. [variablelist
  125. [[Effects:] [Resets the [link shared_state shared state] and abandons the
  126. result of previous executions. A new shared state is constructed.]]
  127. [[Throws:] [__future_error__ with __no_state__.]]
  128. ]
  129. [function_heading_for swap..packaged_task]
  130. template< typename Signature >
  131. void swap( packaged_task< Signature > & l, packaged_task< Signature > & r) noexcept;
  132. [variablelist
  133. [[Effects:] [Same as `l.swap( r)`.]]
  134. ]
  135. [endsect]