TrinityCore
Loading...
Searching...
No Matches
IoContext.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_IO_CONTEXT_H
19#define TRINITYCORE_IO_CONTEXT_H
20
21#include <boost/asio/bind_executor.hpp>
22#include <boost/asio/io_context.hpp>
23#include <boost/asio/post.hpp>
24
25namespace Trinity
26{
27 namespace Asio
28 {
29 class IoContextExecutor : public boost::asio::io_context::executor_type
30 {
31 public:
32 using Base = boost::asio::io_context::executor_type;
33
34 using Base::Base;
35
36 IoContextExecutor(boost::asio::io_context::executor_type const& other) noexcept : Base(other) { }
37
38 IoContextExecutor(boost::asio::io_context::executor_type&& other) noexcept : Base(std::move(other)) { }
39
40 IoContextExecutor& operator=(basic_executor_type const& other) noexcept
41 {
42 Base::operator=(other);
43 return *this;
44 }
45
46 IoContextExecutor& operator=(basic_executor_type&& other) noexcept
47 {
48 Base::operator=(std::move(other));
49 return *this;
50 }
51 };
52
54 {
55 public:
57
58 IoContext() : _impl() { }
59 explicit IoContext(int concurrency_hint) : _impl(concurrency_hint) { }
60
61 operator boost::asio::io_context&() { return _impl; }
62 operator boost::asio::io_context const&() const { return _impl; }
63
64 std::size_t run() { return _impl.run(); }
65 std::size_t poll() { return _impl.poll(); }
66 void stop() { _impl.stop(); }
67
68 bool stopped() const { return _impl.stopped(); }
69 void restart() { return _impl.restart(); }
70
71 executor_type get_executor() noexcept { return _impl.get_executor(); }
72
73 private:
74 boost::asio::io_context _impl;
75 };
76
77 template<typename T>
78 inline decltype(auto) post(IoContext& ioContext, T&& t)
79 {
80 return boost::asio::post(ioContext, std::forward<T>(t));
81 }
82
83 using boost::asio::bind_executor;
84
85 template<typename T>
86 inline decltype(auto) post(IoContextExecutor const& executor, T&& t)
87 {
88 return boost::asio::post(executor.context(), boost::asio::bind_executor(executor, std::forward<T>(t)));
89 }
90
91 template<typename T>
92 inline decltype(auto) get_io_context(T&& ioObject)
93 {
94 return std::forward<T>(ioObject).get_executor().context();
95 }
96 }
97}
98
99#endif // TRINITYCORE_IO_CONTEXT_H
IoContextExecutor(boost::asio::io_context::executor_type const &other) noexcept
Definition IoContext.h:36
IoContextExecutor & operator=(basic_executor_type const &other) noexcept
Definition IoContext.h:40
IoContextExecutor(boost::asio::io_context::executor_type &&other) noexcept
Definition IoContext.h:38
boost::asio::io_context::executor_type Base
Definition IoContext.h:32
IoContextExecutor & operator=(basic_executor_type &&other) noexcept
Definition IoContext.h:46
boost::asio::io_context _impl
Definition IoContext.h:74
IoContext(int concurrency_hint)
Definition IoContext.h:59
executor_type get_executor() noexcept
Definition IoContext.h:71
decltype(auto) get_io_context(T &&ioObject)
Definition IoContext.h:92
decltype(auto) post(IoContext &ioContext, T &&t)
Definition IoContext.h:78