// // executor_work_guard.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // 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_ASIO_EXECUTOR_WORK_GUARD_HPP #define BOOST_ASIO_EXECUTOR_WORK_GUARD_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include namespace lslboost { namespace asio { /// An object of type @c executor_work_guard controls ownership of executor work /// within a scope. template class executor_work_guard { public: /// The underlying executor type. typedef Executor executor_type; /// Constructs a @c executor_work_guard object for the specified executor. /** * Stores a copy of @c e and calls on_work_started() on it. */ explicit executor_work_guard(const executor_type& e) BOOST_ASIO_NOEXCEPT : executor_(e), owns_(true) { executor_.on_work_started(); } /// Copy constructor. executor_work_guard(const executor_work_guard& other) BOOST_ASIO_NOEXCEPT : executor_(other.executor_), owns_(other.owns_) { if (owns_) executor_.on_work_started(); } #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Move constructor. executor_work_guard(executor_work_guard&& other) : executor_(BOOST_ASIO_MOVE_CAST(Executor)(other.executor_)), owns_(other.owns_) { other.owns_ = false; } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Destructor. /** * Unless the object has already been reset, or is in a moved-from state, * calls on_work_finished() on the stored executor. */ ~executor_work_guard() { if (owns_) executor_.on_work_finished(); } /// Obtain the associated executor. executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return executor_; } /// Whether the executor_work_guard object owns some outstanding work. bool owns_work() const BOOST_ASIO_NOEXCEPT { return owns_; } /// Indicate that the work is no longer outstanding. /* * Unless the object has already been reset, or is in a moved-from state, * calls on_work_finished() on the stored executor. */ void reset() BOOST_ASIO_NOEXCEPT { if (owns_) { executor_.on_work_finished(); owns_ = false; } } private: // Disallow assignment. executor_work_guard& operator=(const executor_work_guard&); executor_type executor_; bool owns_; }; /// Create an @ref executor_work_guard object. template inline executor_work_guard make_work_guard(const Executor& ex, typename enable_if::value>::type* = 0) { return executor_work_guard(ex); } /// Create an @ref executor_work_guard object. template inline executor_work_guard make_work_guard(ExecutionContext& ctx, typename enable_if< is_convertible::value>::type* = 0) { return executor_work_guard( ctx.get_executor()); } /// Create an @ref executor_work_guard object. template inline executor_work_guard::type> make_work_guard(const T& t, typename enable_if::value && !is_convertible::value>::type* = 0) { return executor_work_guard::type>( associated_executor::get(t)); } /// Create an @ref executor_work_guard object. template inline executor_work_guard::type> make_work_guard(const T& t, const Executor& ex, typename enable_if::value>::type* = 0) { return executor_work_guard::type>( associated_executor::get(t, ex)); } /// Create an @ref executor_work_guard object. template inline executor_work_guard::type> make_work_guard(const T& t, ExecutionContext& ctx, typename enable_if::value && !is_convertible::value && is_convertible::value>::type* = 0) { return executor_work_guard::type>( associated_executor::get( t, ctx.get_executor())); } } // namespace asio } // namespace lslboost #include #endif // BOOST_ASIO_EXECUTOR_WORK_GUARD_HPP