NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
executor.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_EXECUTOR_HPP__
2#define NEFORCE_CORE_ASYNC_EXECUTOR_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15
21
27
36class executor {
37public:
39 using handler_type = function<void()>;
40
45 virtual ~executor_base() = default;
46 virtual void execute(handler_type handler) = 0;
47 };
48
49private:
51
52public:
53 executor() = default;
54
60 template <typename Exec, enable_if_t<!is_same_v<decay_t<Exec>, executor>, int> = 0>
61 executor(Exec exec) {
62 struct wrapper final : executor_base {
63 Exec exec_;
64 explicit wrapper(Exec e) :
65 exec_(move(e)) {}
66 void execute(handler_type handler) override { exec_.execute(move(handler)); }
67 };
68 exec_ = _NEFORCE make_shared<wrapper>(_NEFORCE move(exec));
69 }
70
75 void execute(handler_type handler) const {
76 if (exec_) {
77 exec_->execute(move(handler));
78 }
79 }
80
85 void post(handler_type handler) const { execute(move(handler)); }
86
90 explicit operator bool() const noexcept { return static_cast<bool>(exec_); }
91
95 NEFORCE_NODISCARD bool operator==(const executor& other) const noexcept { return exec_ == other.exec_; }
96
100 NEFORCE_NODISCARD bool operator!=(const executor& other) const noexcept { return exec_ != other.exec_; }
101};
102
111template <typename Executor>
112void post(Executor& ex, function<void()> handler) {
113 ex.execute(move(handler));
114}
115 // Executor
117 // AsyncComponents
119
120NEFORCE_END_NAMESPACE__
121#endif // NEFORCE_CORE_ASYNC_EXECUTOR_HPP__
executor(Exec exec)
从任意满足 executor 概念的类型构造
bool operator==(const executor &other) const noexcept
相等比较
void post(handler_type handler) const
投递 handler 到执行器
void execute(handler_type handler) const
投递 handler 到执行器
bool operator!=(const executor &other) const noexcept
不等比较
function< void()> handler_type
handler 类型
函数包装器主模板声明
共享智能指针类模板
通用函数包装器
void post(Executor &ex, function< void()> handler)
向执行器投递 handler
enable_if_t<!is_unbounded_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > make_shared(Args &&... args)
融合分配创建共享指针
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
共享智能指针实现