feat support void function in timer callback
This commit is contained in:
parent
4b87ce5219
commit
b500dbfcc5
@ -54,16 +54,6 @@ private:
|
|||||||
scoped_refptr<PendingTaskSafetyFlag> flag_;
|
scoped_refptr<PendingTaskSafetyFlag> flag_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename F, typename... Args, typename = EnableIfT<!std::is_void<InvokeResultT<F, Args...>>::value>>
|
|
||||||
std::function<InvokeResultT<F, Args...>(Args &&...)>
|
|
||||||
SafeTask(scoped_refptr<PendingTaskSafetyFlag> flag, F &&f)
|
|
||||||
{
|
|
||||||
return [flag, f](Args &&...args) mutable -> InvokeResultT<F, Args...> {
|
|
||||||
if (flag->alive()) { return f(std::forward<Args>(args)...); }
|
|
||||||
return {};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename F, typename... Args, typename = EnableIfT<std::is_void<InvokeResultT<F, Args...>>::value>>
|
template<typename F, typename... Args, typename = EnableIfT<std::is_void<InvokeResultT<F, Args...>>::value>>
|
||||||
std::function<void(Args &&...)>
|
std::function<void(Args &&...)>
|
||||||
SafeTask(scoped_refptr<PendingTaskSafetyFlag> flag, F &&f)
|
SafeTask(scoped_refptr<PendingTaskSafetyFlag> flag, F &&f)
|
||||||
|
@ -109,7 +109,7 @@ Timer::Trigger(TimerGeneration generation)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Timer, TimerThreadDeleter>
|
Timer::Ptr
|
||||||
TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner)
|
TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner)
|
||||||
{
|
{
|
||||||
next_id_ = TimerID(next_id_ + 1);
|
next_id_ = TimerID(next_id_ + 1);
|
||||||
@ -128,6 +128,17 @@ TimerManager::CreateTimer(const std::string &name, Timer::OnExpired on_expired,
|
|||||||
return unique_timer;
|
return unique_timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Timer::Ptr
|
||||||
|
TimerManager::CreateTimer(const std::string &name, Timer::OnVoidExpired on_void_expired, TaskQueueBase *owner)
|
||||||
|
{
|
||||||
|
Timer::OnExpired on_expired = [on_void_expired]() {
|
||||||
|
on_void_expired();
|
||||||
|
return sled::nullopt;
|
||||||
|
};
|
||||||
|
|
||||||
|
return CreateTimer(name, on_expired, owner);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
TimerManager::HandleTimeout(TimeoutID id)
|
TimerManager::HandleTimeout(TimeoutID id)
|
||||||
{
|
{
|
||||||
|
@ -30,6 +30,7 @@ public:
|
|||||||
using Ptr = std::unique_ptr<Timer, TimerThreadDeleter>;
|
using Ptr = std::unique_ptr<Timer, TimerThreadDeleter>;
|
||||||
|
|
||||||
using OnExpired = std::function<sled::optional<DurationMs>()>;
|
using OnExpired = std::function<sled::optional<DurationMs>()>;
|
||||||
|
using OnVoidExpired = std::function<void()>;
|
||||||
Timer(const Timer &) = delete;
|
Timer(const Timer &) = delete;
|
||||||
Timer &operator=(const Timer &) = delete;
|
Timer &operator=(const Timer &) = delete;
|
||||||
~Timer();
|
~Timer();
|
||||||
@ -78,6 +79,8 @@ public:
|
|||||||
|
|
||||||
std::unique_ptr<Timer, TimerThreadDeleter>
|
std::unique_ptr<Timer, TimerThreadDeleter>
|
||||||
CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner = nullptr);
|
CreateTimer(const std::string &name, Timer::OnExpired on_expired, TaskQueueBase *owner = nullptr);
|
||||||
|
std::unique_ptr<Timer, TimerThreadDeleter>
|
||||||
|
CreateTimer(const std::string &name, Timer::OnVoidExpired on_void_expired, TaskQueueBase *owner = nullptr);
|
||||||
void HandleTimeout(TimeoutID timeout_id);
|
void HandleTimeout(TimeoutID timeout_id);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user