Skip to content

Commit

Permalink
Introduced move_only_function.
Browse files Browse the repository at this point in the history
  • Loading branch information
redboltz committed Oct 7, 2022
1 parent 0d9e718 commit b40edb8
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 192 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "external/function2"]
path = external/function2
url = https://github.com/Naios/function2
1 change: 1 addition & 0 deletions external/function2
Submodule function2 added at 2d3a87
97 changes: 49 additions & 48 deletions include/mqtt/callable_overlay.hpp

Large diffs are not rendered by default.

159 changes: 82 additions & 77 deletions include/mqtt/endpoint.hpp

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions include/mqtt/move_only_function.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright Takatoshi Kondo 2022
//
// 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)

#if !defined(MQTT_MOVE_ONLY_FUNCTION_HPP)
#define MQTT_MOVE_ONLY_FUNCTION_HPP

#include <mqtt/external/function2.hpp>

#include <mqtt/namespace.hpp>

namespace MQTT_NS {

template <typename... Params>
using move_only_function = fu2::unique_function<Params...>;

} // namespace MQTT_NS

#endif // MQTT_MOVE_ONLY_FUNCTION_HPP
72 changes: 70 additions & 2 deletions include/mqtt/null_strand.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,77 @@ namespace MQTT_NS {

namespace as = boost::asio;

// Using standard executor style null_strand / simple executor
using null_strand = as::io_context::executor_type;
namespace detail {

struct null_strand {
using inner_executor_type = as::io_context::executor_type;
template <typename Executor>
explicit null_strand(Executor e) noexcept
: exe_{force_move(e)}
{}

template <typename Func, typename Allocator>
void defer(Func&& f, Allocator) const {
as::defer(
exe_,
[f = std::forward<Func>(f)] () mutable {
std::move(f)();
}
);
}
template <typename Func, typename Allocator>
void dispatch(Func&& f, Allocator) const {
as::dispatch(
exe_,
[f = std::forward<Func>(f)] () mutable {
std::move(f)();
}
);
}
template <typename Func, typename Allocator>
void post(Func&& f, Allocator) const {
as::post(
exe_,
[f = std::forward<Func>(f)] () mutable {
std::move(f)();
}
);
}
as::any_io_executor get_inner_executor() const {
return exe_;
}
void on_work_started() const noexcept {}
void on_work_finished() const noexcept {}
bool running_in_this_thread() const noexcept { return true; }
operator as::any_io_executor() const {
return exe_;
}
private:
as::io_context::executor_type exe_;
};

} // namespace detail

using null_strand = detail::null_strand;

inline bool operator==(null_strand const& lhs, null_strand const& rhs) {
return std::addressof(lhs) == std::addressof(rhs);
}

inline bool operator!=(null_strand const& lhs, null_strand const& rhs) {
return !(lhs == rhs);
}

} // namespace MQTT_NS

namespace boost {
namespace asio {

template<>
struct is_executor<MQTT_NS::null_strand> : std::true_type {
};

} // namespace asio
} // namespace boost

#endif // MQTT_NULL_STRAND_HPP
Loading

0 comments on commit b40edb8

Please sign in to comment.