summaryrefslogtreecommitdiff
path: root/boost/fiber/detail/wrap.hpp
blob: 558de6bd940a4402fc67072125533dd0017a4d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

//          Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_FIBER_DETAIL_WRAP_H
#define BOOST_FIBER_DETAIL_WRAP_H

#include <type_traits>

#include <boost/config.hpp>
#if defined(BOOST_NO_CXX17_STD_INVOKE)
#include <boost/context/detail/invoke.hpp>
#endif
#if (BOOST_EXECUTION_CONTEXT==1)
# include <boost/context/execution_context.hpp>
#else
# include <boost/context/continuation.hpp>
#endif

#include <boost/fiber/detail/config.hpp>
#include <boost/fiber/detail/data.hpp>

#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif

namespace boost {
namespace fibers {
namespace detail {

#if (BOOST_EXECUTION_CONTEXT==1)
template< typename Fn1, typename Fn2, typename Tpl  >
class wrapper {
private:
    typename std::decay< Fn1 >::type    fn1_;
    typename std::decay< Fn2 >::type    fn2_;
    typename std::decay< Tpl >::type    tpl_;
    boost::context::execution_context   ctx_;

public:
    wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl,
             boost::context::execution_context const& ctx) :
        fn1_{ std::move( fn1) },
        fn2_{ std::move( fn2) },
        tpl_{ std::move( tpl) },
        ctx_{ ctx } {
    }

    wrapper( wrapper const&) = delete;
    wrapper & operator=( wrapper const&) = delete;

    wrapper( wrapper && other) = default;
    wrapper & operator=( wrapper && other) = default;

    void operator()( void * vp) {
#if defined(BOOST_NO_CXX17_STD_INVOKE)
        boost::context::detail::invoke( std::move( fn1_), fn2_, tpl_, ctx_, vp);
#else
        std::invoke( std::move( fn1_), fn2_, tpl_, ctx_, vp);
#endif
    }
};

template< typename Fn1, typename Fn2, typename Tpl  >
wrapper< Fn1, Fn2, Tpl >
wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl,
      boost::context::execution_context const& ctx) {
    return wrapper< Fn1, Fn2, Tpl >{
            std::forward< Fn1 >( fn1),
            std::forward< Fn2 >( fn2),
            std::forward< Tpl >( tpl),
            ctx };
}
#else
template< typename Fn1, typename Fn2, typename Tpl  >
class wrapper {
private:
    typename std::decay< Fn1 >::type    fn1_;
    typename std::decay< Fn2 >::type    fn2_;
    typename std::decay< Tpl >::type    tpl_;

public:
    wrapper( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) :
        fn1_{ std::move( fn1) },
        fn2_{ std::move( fn2) },
        tpl_{ std::move( tpl) } {
    }

    wrapper( wrapper const&) = delete;
    wrapper & operator=( wrapper const&) = delete;

    wrapper( wrapper && other) = default;
    wrapper & operator=( wrapper && other) = default;

    boost::context::continuation
    operator()( boost::context::continuation && c) {
#if defined(BOOST_NO_CXX17_STD_INVOKE)
        return boost::context::detail::invoke(
                std::move( fn1_),
                fn2_,
                tpl_,
                std::forward< boost::context::continuation >( c) );
#else
        return std::invoke(
                std::move( fn1_),
                fn2_,
                tpl_,
                std::forward< boost::context::continuation >( c) );
#endif
    }
};

template< typename Fn1, typename Fn2, typename Tpl  >
wrapper< Fn1, Fn2, Tpl >
wrap( Fn1 && fn1, Fn2 && fn2, Tpl && tpl) {
    return wrapper< Fn1, Fn2, Tpl >{
            std::forward< Fn1 >( fn1),
            std::forward< Fn2 >( fn2),
            std::forward< Tpl >( tpl) };
}
#endif

}}}

#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif

#endif // BOOST_FIBER_DETAIL_WRAP_H