wrap.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright Oliver Kowalke 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_COROUTINE2_DETAIL_WRAP_H
  6. #define BOOST_COROUTINE2_DETAIL_WRAP_H
  7. #include <functional>
  8. #include <type_traits>
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  11. #include <boost/context/detail/invoke.hpp>
  12. #endif
  13. #include <boost/context/fiber.hpp>
  14. #include <boost/coroutine2/detail/config.hpp>
  15. #ifdef BOOST_HAS_ABI_HEADERS
  16. # include BOOST_ABI_PREFIX
  17. #endif
  18. namespace boost {
  19. namespace coroutines2 {
  20. namespace detail {
  21. template< typename Fn1, typename Fn2 >
  22. class wrapper {
  23. private:
  24. typename std::decay< Fn1 >::type fn1_;
  25. typename std::decay< Fn2 >::type fn2_;
  26. public:
  27. wrapper( Fn1 && fn1, Fn2 && fn2) :
  28. fn1_( std::move( fn1) ),
  29. fn2_( std::move( fn2) ) {
  30. }
  31. wrapper( wrapper const&) = delete;
  32. wrapper & operator=( wrapper const&) = delete;
  33. wrapper( wrapper && other) = default;
  34. wrapper & operator=( wrapper && other) = default;
  35. boost::context::fiber
  36. operator()( boost::context::fiber && c) {
  37. #if defined(BOOST_NO_CXX17_STD_INVOKE)
  38. return boost::context::detail::invoke(
  39. std::move( fn1_),
  40. fn2_,
  41. std::forward< boost::context::fiber >( c) );
  42. #else
  43. return std::invoke(
  44. std::move( fn1_),
  45. fn2_,
  46. std::forward< boost::context::fiber >( c) );
  47. #endif
  48. }
  49. };
  50. template< typename Fn1, typename Fn2 >
  51. wrapper< Fn1, Fn2 >
  52. wrap( Fn1 && fn1, Fn2 && fn2) {
  53. return wrapper< Fn1, Fn2 >(
  54. std::forward< Fn1 >( fn1),
  55. std::forward< Fn2 >( fn2) );
  56. }
  57. }}}
  58. #ifdef BOOST_HAS_ABI_HEADERS
  59. #include BOOST_ABI_SUFFIX
  60. #endif
  61. #endif // BOOST_COROUTINE2_DETAIL_WRAP_H