1#ifndef NEFORCE_CORE_ASYNC_SIGNALS_HPP__
2#define NEFORCE_CORE_ASYNC_SIGNALS_HPP__
18NEFORCE_BEGIN_NAMESPACE__
45 constexpr oneshot_t()
noexcept =
default;
97 NEFORCE_NODISCARD
bool connected() const noexcept {
return connected_ && *connected_; }
128 conn_(_NEFORCE
move(conn)) {}
143 conn_(_NEFORCE
move(other.conn_)) {}
155 conn_ = _NEFORCE
move(other.conn_);
168 NEFORCE_NODISCARD
bool connected() const noexcept {
return conn_.connected(); }
180template <
typename... Types>
189template <
typename... Types>
194 bool released_ =
false;
202 blocked_flag_(sig.block_flag()) {
203 old_value_ = *blocked_flag_;
204 *blocked_flag_ =
true;
211 if (blocked_flag_ && !released_) {
212 *blocked_flag_ = old_value_;
225 if (blocked_flag_ && !released_) {
226 *blocked_flag_ = old_value_;
240template <
typename... Types>
250 callback_type callback;
255 callback(_NEFORCE
move(cb)),
256 connected_flag(_NEFORCE
move(flag)),
262 mutable mutex mutex_;
264 template <
typename T>
268 template <
typename T>
272 template <
typename T>
273 static T* lock_if_weak(T* self)
noexcept {
277 template <
typename Func>
278 auto with_lock(Func&& func)
const {
283 template <
typename Func>
284 auto with_lock(Func&& func) {
297 template <
typename Self,
typename MemFn>
298 auto bind(Self self, MemFn memfn) {
299 return [self = _NEFORCE
move(self), memfn](Types... args) {
300 auto ptr = signal::lock_if_weak(self);
301 if (ptr ==
nullptr) {
318 template <
typename Self,
typename MemFn>
319 auto bind(Self self, MemFn memfn,
oneshot_t one) {
320 return [self = _NEFORCE
move(self), memfn](Types... args) {
321 auto ptr = signal::lock_if_weak(self);
322 if (ptr ==
nullptr) {
339 template <
typename Self,
typename MemFn>
340 auto bind(Self self, MemFn memfn,
nshot_t n) {
341 return [self = _NEFORCE
move(self), memfn, n =
static_cast<size_t>(n)](Types... args)
mutable {
345 auto ptr = signal::lock_if_weak(self);
346 if (ptr ==
nullptr) {
365 template <
typename Func>
370 auto it = slots_.begin();
371 while (it != slots_.end() && it->priority >=
priority) {
386 template <
typename Func>
391 auto wrapped = [callback = _NEFORCE
move(callback)](Types... args)
mutable {
396 auto it = slots_.begin();
397 while (it != slots_.end() && it->priority >=
priority) {
405 size_t slot_count_unlocked()
const noexcept {
407 for (
const auto& slot: slots_) {
408 if (slot.connected_flag && *slot.connected_flag) {
418 signal(
const signal&) =
delete;
419 signal& operator=(
const signal&) =
delete;
420 signal(signal&&) =
delete;
421 signal& operator=(signal&&) =
delete;
433 template <
typename Self,
typename MemFn,
typename... Tag>
435 return this->
connect(_NEFORCE
move(self), memfn, 0, tag...);
445 template <
typename Func>
447 return this->with_lock([
this, callback = _NEFORCE
move(callback),
priority] {
448 return this->connect_impl(_NEFORCE
move(callback),
priority);
463 template <
typename Self,
typename MemFn,
typename... Tag>
465 static_assert(
sizeof...(Tag) <= 1,
"only zero or one tag is allowed");
467 return this->with_lock([
this, self = _NEFORCE
move(self), memfn = _NEFORCE
move(memfn),
priority, tag...] {
470 auto it = slots_.begin();
471 while (it != slots_.end() && it->priority >=
priority) {
474 slots_.emplace(it, this->bind(_NEFORCE
move(self), memfn, tag...), conn.flag(),
priority);
497 for (
auto& slot: snapshot) {
498 if (!slot.connected_flag || !(*slot.connected_flag)) {
504 *slot.connected_flag =
false;
511 _NEFORCE
remove_if(slots_.begin(), slots_.end(),
512 [](
const slot_entry& s) { return !s.connected_flag || !(*s.connected_flag); }),
525 template <
typename Executor>
530 executor.post([
this, weak_flag = _NEFORCE
move(weak_flag), args_tuple = _NEFORCE
move(args_tuple)]()
mutable {
531 if (weak_flag.expired()) {
535 _NEFORCE
apply([
this](
auto&&... a) { this->
emit(_NEFORCE
forward<
decltype(a)>(a)...); },
536 _NEFORCE
move(args_tuple));
544 template <
typename... Args>
553 this->with_lock([
this] { slots_.clear(); });
566 [other_ptr = &other](Types... args) {
567 other_ptr->emit(args...);
568 return callback_result::keep;
583 [other](Types... args) {
585 other->emit(args...);
603 template <
typename Func,
typename Predicate>
609 [callback = _NEFORCE
move(callback), pred = _NEFORCE
move(pred)](Types... args)
mutable {
611 callback(_NEFORCE forward<Types>(args)...);
631 template <
typename Self,
typename MemFn,
typename Predicate,
typename... Tag>
635 static_assert(
sizeof...(Tag) <= 1,
"only zero or one tag is allowed");
637 auto bound = this->bind(_NEFORCE
move(self), memfn, tag...);
640 [bound = _NEFORCE
move(bound), pred = _NEFORCE
move(pred)](Types... args)
mutable {
642 return bound(_NEFORCE forward<Types>(args)...);
660 template <
typename Func,
typename Filter>
663 [callback = _NEFORCE
move(callback), filter = _NEFORCE
move(filter)](Types... args)
mutable {
664 auto filtered = filter(args...);
666 auto filtered_tuple = _NEFORCE make_tuple(_NEFORCE move(*filtered));
668 [&callback](auto&&... filtered_args) {
669 callback(_NEFORCE forward<decltype(filtered_args)>(filtered_args)...);
671 _NEFORCE move(filtered_tuple));
689 template <
typename Func,
typename Transform>
691 static_assert(
is_invocable_v<Transform, Types...>,
"only function inputs are allowed");
694 [callback = _NEFORCE
move(callback), transform = _NEFORCE
move(transform)](Types... args)
mutable {
695 callback(transform(_NEFORCE forward<Types>(args)...));
696 return callback_result::keep;
711 bool is_blocked() const noexcept {
return blocked_flag_ && *blocked_flag_; }
718 return this->with_lock([
this] {
return slot_count_unlocked(); });
725 NEFORCE_NODISCARD
bool empty() const noexcept {
726 return this->with_lock([
this] {
return slot_count_unlocked() == 0; });
732NEFORCE_END_NAMESPACE__
bool connected() const noexcept
检查连接是否有效
connection()
默认构造函数,创建处于已连接状态的对象
void disconnect() noexcept
断开连接
shared_ptr< bool > flag() const noexcept
获取连接标志的内部指针
scoped_connection(connection conn) noexcept
从连接构造
connection release() noexcept
释放连接所有权
bool connected() const noexcept
检查连接是否有效
void disconnect() noexcept
手动断开连接
scoped_connection(scoped_connection &&other) noexcept
移动构造函数
scoped_connection & operator=(scoped_connection &&other) noexcept
移动赋值运算符
scoped_connection()=default
默认构造函数
~scoped_connection()
析构函数,自动断开连接
~signal_blocker()
析构函数,恢复原状态
void unblock() noexcept
手动解除阻塞
signal_blocker(signal< Types... > &sig) noexcept
构造函数,阻塞指定信号
shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr bool is_boolean_v
is_boolean的便捷变量模板
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
constexpr bool is_invocable_v
is_invocable的便捷变量模板
constexpr bool is_invocable_r_v
is_invocable_r的便捷变量模板
typename inner::__invoke_result_aux< F, Args... >::type invoke_result_t
invoke_result的便捷别名
constexpr Iterator remove_if(Iterator first, Iterator last, Predicate pred)
移除范围中满足谓词的元素
constexpr size_t erase(Container &cont, const U &value)
从容器中删除所有等于指定值的元素
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)))
移动范围元素
constexpr oneshot_t oneshot
一次性连接标签实例
int priority() noexcept
获取线程优先级
constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
constexpr decltype(auto) apply(Func &&f, Tuple &&t) noexcept(inner::__apply_unpack_tuple< _NEFORCE is_nothrow_invocable, Func, Tuple >::value)
将元组元素解包作为参数调用函数
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
connection connect_transformed(Func callback, Transform transform, int priority=0)
变换连接
void disconnect_all()
断开所有连接
connection connect_signal(signal &other, int priority=0)
连接另一个信号
void emit(Types... args)
触发信号
connection connect(Self self, MemFn memfn, int priority, Tag... tag)
连接成员函数
connection connect_filtered(Func callback, Filter filter, int priority=0)
过滤连接
bool is_blocked() const noexcept
检查信号是否被阻塞
shared_ptr< bool > block_flag() const noexcept
获取阻塞标志
bool empty() const noexcept
检查信号是否为空(无活跃槽)
connection connect_signal(signal *other, int priority=0)
连接另一个信号(指针版本)
size_t slot_count() const noexcept
获取活跃槽的数量
void operator()(Args &&... args)
函数调用操作符
connection connect_if(Self self, MemFn memfn, Predicate pred, int priority, Tag... tag)
条件连接(成员函数版本)
connection connect_if(Func callback, Predicate pred, int priority=0)
条件连接
connection connect(Func callback, int priority=0)
连接普通函数/lambda
void emit_executor(Executor &executor, Types... args)
执行器触发信号
connection connect(Self self, MemFn memfn, Tag... tag)
连接成员函数(默认优先级0)