mqtt_cpp
null_strand.hpp
Go to the documentation of this file.
1 // Copyright Takatoshi Kondo 2016
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(MQTT_NULL_STRAND_HPP)
8 #define MQTT_NULL_STRAND_HPP
9 
10 #include <utility>
11 
12 #include <boost/asio.hpp>
13 
14 #include <mqtt/namespace.hpp>
15 
16 namespace MQTT_NS {
17 
18 namespace as = boost::asio;
19 
20 struct null_strand {
21  null_strand(as::io_context& ioc) noexcept : ioc_(ioc) {}
22  template <typename Func, typename Allocator>
23  void post(Func&& f, Allocator) const {
24  as::post(
25  ioc_,
26  [f = std::forward<Func>(f)] () mutable {
27  std::move(f)();
28  }
29  );
30  }
31  template <typename Func, typename Allocator>
32  void defer(Func&& f, Allocator) const {
33  as::defer(
34  ioc_,
35  [f = std::forward<Func>(f)] () mutable {
36  std::move(f)();
37  }
38  );
39  }
40  template <typename Func, typename Allocator>
41  void dispatch(Func&& f, Allocator) const {
42  std::forward<Func>(f)();
43  }
44  void on_work_started() const noexcept {}
45  void on_work_finished() const noexcept {}
46  as::io_context& context() noexcept{ return ioc_; }
47  as::io_context const& context() const noexcept { return ioc_; }
48 private:
49  as::io_context& ioc_;
50 };
51 
52 inline bool operator==(null_strand const& lhs, null_strand const& rhs) {
53  return std::addressof(lhs) == std::addressof(rhs);
54 }
55 
56 inline bool operator!=(null_strand const& lhs, null_strand const& rhs) {
57  return !(lhs == rhs);
58 }
59 
60 } // namespace MQTT_NS
61 
62 namespace boost {
63 namespace asio {
64 
65 template<>
66 struct is_executor<MQTT_NS::null_strand> : std::true_type {
67 };
68 
69 } // namespace asio
70 } // namespace boost
71 
72 #endif // MQTT_NULL_STRAND_HPP
Definition: any.hpp:27
bool operator==(null_strand const &lhs, null_strand const &rhs)
Definition: null_strand.hpp:52
bool operator!=(null_strand const &lhs, null_strand const &rhs)
Definition: null_strand.hpp:56
Definition: buffer.hpp:242
Definition: buffer.hpp:241
Definition: null_strand.hpp:20
void defer(Func &&f, Allocator) const
Definition: null_strand.hpp:32
void dispatch(Func &&f, Allocator) const
Definition: null_strand.hpp:41
void on_work_started() const noexcept
Definition: null_strand.hpp:44
void post(Func &&f, Allocator) const
Definition: null_strand.hpp:23
void on_work_finished() const noexcept
Definition: null_strand.hpp:45
as::io_context & context() noexcept
Definition: null_strand.hpp:46
as::io_context const & context() const noexcept
Definition: null_strand.hpp:47
null_strand(as::io_context &ioc) noexcept
Definition: null_strand.hpp:21