1//
2// Copyright (c) 2020 Richard Hodges (hodges.r@gmail.com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10#ifndef BOOST_BEAST_CORE_DETAIL_WORK_GUARD_HPP
11#define BOOST_BEAST_CORE_DETAIL_WORK_GUARD_HPP
12
13#include <boost/asio/executor_work_guard.hpp>
14#include <boost/asio/execution.hpp>
15#include <boost/optional.hpp>
16
17namespace boost {
18namespace beast {
19namespace detail {
20
21template<class Executor, class Enable = void>
22struct select_work_guard;
23
24template<class Executor>
25using select_work_guard_t = typename
26 select_work_guard<Executor>::type;
27
28#if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
29template<class Executor>
30struct select_work_guard
31<
32 Executor,
33 typename std::enable_if
34 <
35 net::is_executor<Executor>::value
36 >::type
37>
38{
39 using type = net::executor_work_guard<Executor>;
40};
41#endif
42
43template<class Executor>
44struct execution_work_guard
45{
46 using executor_type = typename std::decay<decltype(
47 net::prefer(std::declval<Executor const&>(),
48 net::execution::outstanding_work.tracked))>::type;
49
50 execution_work_guard(Executor const& exec)
51 : ex_(net::prefer(exec, net::execution::outstanding_work.tracked))
52 {
53
54 }
55
56 executor_type
57 get_executor() const noexcept
58 {
59 BOOST_ASSERT(ex_.has_value());
60 return *ex_;
61 }
62
63 void reset() noexcept
64 {
65 ex_.reset();
66 }
67
68private:
69
70 boost::optional<executor_type> ex_;
71};
72
73template<class Executor>
74struct select_work_guard
75<
76 Executor,
77 typename std::enable_if
78 <
79 net::execution::is_executor<Executor>::value
80#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
81 || net::is_executor<Executor>::value
82#else
83 && !net::is_executor<Executor>::value
84#endif
85 >::type
86>
87{
88 using type = execution_work_guard<Executor>;
89};
90
91template<class Executor>
92select_work_guard_t<Executor>
93make_work_guard(Executor const& exec) noexcept
94{
95 return select_work_guard_t<Executor>(exec);
96}
97
98}
99}
100}
101
102#endif // BOOST_BEAST_CORE_DETAIL_WORK_GUARD_HPP
103

source code of boost/libs/beast/include/boost/beast/core/detail/work_guard.hpp