Chris@16: // Chris@16: // waitable_timer_service.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP Chris@16: #define BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Default service implementation for a timer. Chris@16: template > Chris@16: class waitable_timer_service Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: : public boost::asio::io_service::service Chris@16: #else Chris@16: : public boost::asio::detail::service_base< Chris@16: waitable_timer_service > Chris@16: #endif Chris@16: { Chris@16: public: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// The unique service identifier. Chris@16: static boost::asio::io_service::id id; Chris@16: #endif Chris@16: Chris@16: /// The clock type. Chris@16: typedef Clock clock_type; Chris@16: Chris@16: /// The duration type of the clock. Chris@16: typedef typename clock_type::duration duration; Chris@16: Chris@16: /// The time point type of the clock. Chris@16: typedef typename clock_type::time_point time_point; Chris@16: Chris@16: /// The wait traits type. Chris@16: typedef WaitTraits traits_type; Chris@16: Chris@16: private: Chris@16: // The type of the platform-specific implementation. Chris@16: typedef detail::deadline_timer_service< Chris@16: detail::chrono_time_traits > service_impl_type; Chris@16: Chris@16: public: Chris@16: /// The implementation type of the waitable timer. Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: typedef implementation_defined implementation_type; Chris@16: #else Chris@16: typedef typename service_impl_type::implementation_type implementation_type; Chris@16: #endif Chris@16: Chris@16: /// Construct a new timer service for the specified io_service. Chris@16: explicit waitable_timer_service(boost::asio::io_service& io_service) Chris@16: : boost::asio::detail::service_base< Chris@16: waitable_timer_service >(io_service), Chris@16: service_impl_(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a new timer implementation. Chris@16: void construct(implementation_type& impl) Chris@16: { Chris@16: service_impl_.construct(impl); Chris@16: } Chris@16: Chris@16: /// Destroy a timer implementation. Chris@16: void destroy(implementation_type& impl) Chris@16: { Chris@16: service_impl_.destroy(impl); Chris@16: } Chris@16: Chris@16: /// Cancel any asynchronous wait operations associated with the timer. Chris@16: std::size_t cancel(implementation_type& impl, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.cancel(impl, ec); Chris@16: } Chris@16: Chris@16: /// Cancels one asynchronous wait operation associated with the timer. Chris@16: std::size_t cancel_one(implementation_type& impl, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.cancel_one(impl, ec); Chris@16: } Chris@16: Chris@16: /// Get the expiry time for the timer as an absolute time. Chris@16: time_point expires_at(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.expires_at(impl); Chris@16: } Chris@16: Chris@16: /// Set the expiry time for the timer as an absolute time. Chris@16: std::size_t expires_at(implementation_type& impl, Chris@16: const time_point& expiry_time, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.expires_at(impl, expiry_time, ec); Chris@16: } Chris@16: Chris@16: /// Get the expiry time for the timer relative to now. Chris@16: duration expires_from_now(const implementation_type& impl) const Chris@16: { Chris@16: return service_impl_.expires_from_now(impl); Chris@16: } Chris@16: Chris@16: /// Set the expiry time for the timer relative to now. Chris@16: std::size_t expires_from_now(implementation_type& impl, Chris@16: const duration& expiry_time, boost::system::error_code& ec) Chris@16: { Chris@16: return service_impl_.expires_from_now(impl, expiry_time, ec); Chris@16: } Chris@16: Chris@16: // Perform a blocking wait on the timer. Chris@16: void wait(implementation_type& impl, boost::system::error_code& ec) Chris@16: { Chris@16: service_impl_.wait(impl, ec); Chris@16: } Chris@16: Chris@16: // Start an asynchronous wait on the timer. Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler, Chris@16: void (boost::system::error_code)) Chris@16: async_wait(implementation_type& impl, Chris@16: BOOST_ASIO_MOVE_ARG(WaitHandler) handler) Chris@16: { Chris@16: detail::async_result_init< Chris@16: WaitHandler, void (boost::system::error_code)> init( Chris@16: BOOST_ASIO_MOVE_CAST(WaitHandler)(handler)); Chris@16: Chris@16: service_impl_.async_wait(impl, init.handler); Chris@16: Chris@16: return init.result.get(); Chris@16: } Chris@16: Chris@16: private: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: void shutdown_service() Chris@16: { Chris@16: service_impl_.shutdown_service(); Chris@16: } Chris@16: Chris@16: // The platform-specific implementation. Chris@16: service_impl_type service_impl_; Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP