1#ifndef NEFORCE_CORE_ASYNC_SIGNALS_HPP__
2#define NEFORCE_CORE_ASYNC_SIGNALS_HPP__
17NEFORCE_BEGIN_NAMESPACE__
44 constexpr oneshot_t()
noexcept =
default;
96 bool connected() const noexcept {
return connected_ && *connected_; }
127 conn_(_NEFORCE
move(conn)) {}
142 conn_(_NEFORCE
move(other.conn_)) {}
154 conn_ = _NEFORCE
move(other.conn_);
167 bool connected() const noexcept {
return conn_.connected(); }
179template <
typename... Types>
188template <
typename... Types>
193 bool released_ =
false;
201 blocked_flag_(sig.block_flag()) {
202 old_value_ = *blocked_flag_;
203 *blocked_flag_ =
true;
210 if (blocked_flag_ && !released_) {
211 *blocked_flag_ = old_value_;
224 if (blocked_flag_ && !released_) {
225 *blocked_flag_ = old_value_;
239template <
typename... Types>
249 callback_type callback;
254 callback(_NEFORCE
move(cb)),
255 connected_flag(_NEFORCE
move(flag)),
261 mutable mutex mutex_;
263 template <
typename T>
267 template <
typename T>
271 template <
typename T>
272 static T* lock_if_weak(T* self)
noexcept {
276 template <
typename Func>
277 auto with_lock(Func&& func)
const {
282 template <
typename Func>
283 auto with_lock(Func&& func) {
296 template <
typename Self,
typename MemFn>
297 auto bind(Self self, MemFn memfn) {
298 return [self = _NEFORCE
move(self), memfn](Types... args) {
299 auto ptr = signal::lock_if_weak(self);
300 if (ptr ==
nullptr) {
317 template <
typename Self,
typename MemFn>
318 auto bind(Self self, MemFn memfn,
oneshot_t one) {
319 return [self = _NEFORCE
move(self), memfn](Types... args) {
320 auto ptr = signal::lock_if_weak(self);
321 if (ptr ==
nullptr) {
338 template <
typename Self,
typename MemFn>
339 auto bind(Self self, MemFn memfn,
nshot_t n) {
340 return [self = _NEFORCE
move(self), memfn, n =
static_cast<size_t>(n)](Types... args)
mutable {
344 auto ptr = signal::lock_if_weak(self);
345 if (ptr ==
nullptr) {
364 template <
typename Func>
369 auto it = slots_.begin();
370 while (it != slots_.end() && it->priority >=
priority) {
385 template <
typename Func>
390 auto wrapped = [callback = _NEFORCE
move(callback)](Types... args)
mutable {
395 auto it = slots_.begin();
396 while (it != slots_.end() && it->priority >=
priority) {
404 size_t slot_count_unlocked()
const noexcept {
406 for (
const auto& slot: slots_) {
407 if (slot.connected_flag && *slot.connected_flag) {
416 signal(
const signal&) =
delete;
417 signal& operator=(
const signal&) =
delete;
418 signal(signal&&) =
default;
419 signal& operator=(signal&&) =
default;
431 template <
typename Self,
typename MemFn,
typename... Tag>
433 return this->
connect(_NEFORCE
move(self), memfn, 0, tag...);
443 template <
typename Func>
445 return this->with_lock([
this, callback = _NEFORCE
move(callback),
priority] {
446 return this->connect_impl(_NEFORCE
move(callback),
priority);
461 template <
typename Self,
typename MemFn,
typename... Tag>
463 static_assert(
sizeof...(Tag) <= 1,
"only zero or one tag is allowed");
465 return this->with_lock([
this, self = _NEFORCE
move(self), memfn = _NEFORCE
move(memfn),
priority, tag...] {
468 auto it = slots_.begin();
469 while (it != slots_.end() && it->priority >=
priority) {
472 slots_.emplace(it, this->bind(_NEFORCE
move(self), memfn, tag...), conn.flag(),
priority);
495 for (
auto& slot: snapshot) {
496 if (!slot.connected_flag || !(*slot.connected_flag)) {
502 *slot.connected_flag =
false;
509 _NEFORCE
remove_if(slots_.begin(), slots_.end(),
510 [](
const slot_entry& s) { return !s.connected_flag || !(*s.connected_flag); }),
523 template <
typename Executor>
528 executor.post([
this, weak_flag = _NEFORCE
move(weak_flag), args_tuple = _NEFORCE
move(args_tuple)]()
mutable {
529 if (weak_flag.expired()) {
533 _NEFORCE
apply([
this](
auto&&... a) { this->
emit(_NEFORCE
forward<
decltype(a)>(a)...); },
534 _NEFORCE
move(args_tuple));
542 template <
typename... Args>
551 this->with_lock([
this] { slots_.clear(); });
564 [other_ptr = &other](Types... args) {
565 other_ptr->emit(args...);
566 return callback_result::keep;
581 [other](Types... args) {
583 other->emit(args...);
601 template <
typename Func,
typename Predicate>
607 [callback = _NEFORCE
move(callback), pred = _NEFORCE
move(pred)](Types... args)
mutable {
609 callback(_NEFORCE forward<Types>(args)...);
629 template <
typename Self,
typename MemFn,
typename Predicate,
typename... Tag>
633 static_assert(
sizeof...(Tag) <= 1,
"only zero or one tag is allowed");
635 auto bound = this->bind(_NEFORCE
move(self), memfn, tag...);
638 [bound = _NEFORCE
move(bound), pred = _NEFORCE
move(pred)](Types... args)
mutable {
640 return bound(_NEFORCE forward<Types>(args)...);
658 template <
typename Func,
typename Filter>
661 static_assert(is_optional_v<result_type>,
"only optional results are allowed");
664 [callback = _NEFORCE
move(callback), filter = _NEFORCE
move(filter)](Types... args)
mutable {
665 auto filtered = filter(args...);
668 [&callback](auto&&... filtered_args) {
669 callback(_NEFORCE forward<decltype(filtered_args)>(filtered_args)...);
671 _NEFORCE move(*filtered));
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
构造函数,阻塞指定信号
NEFORCE_NODISCARD shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_INLINE17 constexpr bool is_boolean_v
is_boolean的便捷变量模板
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
NEFORCE_INLINE17 constexpr bool is_invocable_v
is_invocable的便捷变量模板
NEFORCE_INLINE17 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)))
移动范围元素
NEFORCE_INLINE17 constexpr oneshot_t oneshot
一次性连接标签实例
bool NEFORCE_API priority(int priority) noexcept
设置线程优先级
NEFORCE_NODISCARD 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
获取阻塞标志
NEFORCE_NODISCARD 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)