async_mqtt 5.0.0
Loading...
Searching...
No Matches
null_strand.hpp
1// Copyright Takatoshi Kondo 2024
2//
3// Distributed under the Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6
7#if !defined(ASYNC_MQTT_NULL_STRAND_HPP)
8#define ASYNC_MQTT_NULL_STRAND_HPP
9
10#include <cstddef>
11#include <chrono>
12
13#include <boost/asio.hpp>
14
15#include <async_mqtt/type.hpp>
16
17namespace async_mqtt {
18
19namespace as = boost::asio;
20
21template <typename Executor>
22class null_strand {
23public:
24 using inner_executor_type = Executor;
25
26 null_strand(Executor exe) noexcept
27 :exe_{std::move(exe)}
28 {
29 }
30 null_strand() noexcept = default;
31 null_strand(null_strand const&) noexcept = default;
32 null_strand(null_strand&&) noexcept = default;
33 template <typename OtherExecutor>
34 null_strand(null_strand<OtherExecutor> const& other) noexcept
35 :exe_{other.exe_}
36 {
37 }
38 template <typename OtherExecutor>
39 null_strand(null_strand<OtherExecutor>&& other) noexcept
40 :exe_{std::move(other.exe_)}
41 {
42 }
43
44 null_strand<Executor>& operator=(null_strand const& other) noexcept {
45 exe_ = other.exe_;
46 return *this;
47 }
48 null_strand<Executor>& operator=(null_strand&& other) noexcept {
49 exe_ = std::move(other.exe_);
50 return *this;
51 }
52 template <typename OtherExecutor>
53 null_strand<Executor>& operator=(null_strand<OtherExecutor> const& other) noexcept {
54 exe_ = other.exe_;
55 return *this;
56 }
57 template <typename OtherExecutor>
58 null_strand<Executor>& operator=(null_strand<OtherExecutor>&& other) noexcept {
59 exe_ = std::move(other.exe_);
60 return *this;
61 }
62
63 template<
64 typename Function,
65 typename Allocator
66 >
67 void dispatch(
68 Function&& f,
69 Allocator const& a) const {
70 as::dispatch(
71 as::bind_executor(
72 exe_,
73 as::bind_allocator(
74 a,
75 std::forward<Function>(f)
76 )
77 )
78 );
79 }
80 template<
81 typename Function,
82 typename Allocator
83 >
84 void post(
85 Function&& f,
86 Allocator const& a) const {
87 as::post(
88 as::bind_executor(
89 exe_,
90 as::bind_allocator(
91 a,
92 std::forward<Function>(f)
93 )
94 )
95 );
96 }
97 template<
98 typename Function,
99 typename Allocator
100 >
101 void defer(
102 Function&& f,
103 Allocator const& a) const {
104 as::defer(
105 as::bind_executor(
106 exe_,
107 as::bind_allocator(
108 a,
109 std::forward<Function>(f)
110 )
111 )
112 );
113 }
114
115 void on_work_started() const noexcept {
116 }
117 void on_work_finished() const noexcept {
118 }
119 bool running_in_this_thread() const noexcept {
120 return true;
121 }
122
123 template <typename Function>
124 void execute(Function&& f) const {
125 exe_.execute(std::forward<Function>(f));
126 }
127
128
129 as::execution_context& context() const noexcept {
130 return exe_.context();
131 }
132
133 inner_executor_type get_inner_executor() const noexcept {
134 return exe_;
135 }
136
137 friend bool operator==(
138 null_strand<Executor> const& lhs,
139 null_strand<Executor> const& rhs
140 ) noexcept {
141 return lhs.exe_ == rhs.exe_;
142 }
143 friend bool operator!=(
144 null_strand<Executor> const& lhs,
145 null_strand<Executor> const& rhs
146 ) noexcept {
147 return lhs.exe_ != rhs.exe_;
148 }
149
150private:
151 inner_executor_type exe_;
152};
153
154
155} // namespace async_mqtt
156
157#endif // ASYNC_MQTT_NULL_STRAND_HPP