diff options
Diffstat (limited to '3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp')
-rw-r--r-- | 3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp | 69 |
1 files changed, 39 insertions, 30 deletions
diff --git a/3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp b/3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp index 73897946966..602d7b6f010 100644 --- a/3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp +++ b/3rdparty/asio/src/examples/cpp03/invocation/prioritised_handlers.cpp @@ -2,7 +2,7 @@ // prioritised_handlers.cpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // -// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// Copyright (c) 2003-2021 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) @@ -15,7 +15,7 @@ using asio::ip::tcp; -class handler_priority_queue +class handler_priority_queue : public asio::execution_context { public: void add(int priority, boost::function<void()> function) @@ -33,43 +33,60 @@ public: } } - // A generic wrapper class for handlers to allow the invocation to be hooked. - template <typename Handler> - class wrapped_handler + class executor { public: - wrapped_handler(handler_priority_queue& q, int p, Handler h) - : queue_(q), priority_(p), handler_(h) + executor(handler_priority_queue& q, int p) + : context_(q), priority_(p) + { + } + + handler_priority_queue& context() const { + return context_; } - void operator()() + template <typename Function, typename Allocator> + void dispatch(const Function& f, const Allocator&) const { - handler_(); + context_.add(priority_, f); } - template <typename Arg1> - void operator()(Arg1 arg1) + template <typename Function, typename Allocator> + void post(const Function& f, const Allocator&) const { - handler_(arg1); + context_.add(priority_, f); } - template <typename Arg1, typename Arg2> - void operator()(Arg1 arg1, Arg2 arg2) + template <typename Function, typename Allocator> + void defer(const Function& f, const Allocator&) const + { + context_.add(priority_, f); + } + + void on_work_started() const {} + void on_work_finished() const {} + + bool operator==(const executor& other) const { - handler_(arg1, arg2); + return &context_ == &other.context_ && priority_ == other.priority_; } - //private: - handler_priority_queue& queue_; + bool operator!=(const executor& other) const + { + return !operator==(other); + } + + private: + handler_priority_queue& context_; int priority_; - Handler handler_; }; template <typename Handler> - wrapped_handler<Handler> wrap(int priority, Handler handler) + asio::executor_binder<Handler, executor> + wrap(int priority, Handler handler) { - return wrapped_handler<Handler>(*this, priority, handler); + return asio::bind_executor(executor(*this, priority), handler); } private: @@ -100,14 +117,6 @@ private: std::priority_queue<queued_handler> handlers_; }; -// Custom invocation hook for wrapped handlers. -template <typename Function, typename Handler> -void asio_handler_invoke(Function f, - handler_priority_queue::wrapped_handler<Handler>* h) -{ - h->queue_.add(h->priority_, f); -} - //---------------------------------------------------------------------- void high_priority_handler(const asio::error_code& /*ec*/) @@ -144,8 +153,8 @@ int main() client_socket.connect(acceptor.local_endpoint()); // Set a deadline timer to expire immediately. - asio::deadline_timer timer(io_context); - timer.expires_at(boost::posix_time::neg_infin); + asio::steady_timer timer(io_context); + timer.expires_at(asio::steady_timer::time_point::min()); timer.async_wait(pri_queue.wrap(42, middle_priority_handler)); while (io_context.run_one()) |