From 3d95aba2b3d58057f3eeed6d4dd6e51ea49b77f6 Mon Sep 17 00:00:00 2001 From: Naios Date: Sun, 21 Jun 2015 18:04:47 +0200 Subject: Core/Utilities: TaskScheduler: add a task validator and on update success callback. * makes it possible to block tasks if there is an active spellcast. * requested by @joschiwald --- src/server/shared/Utilities/TaskScheduler.h | 48 ++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'src/server/shared/Utilities/TaskScheduler.h') diff --git a/src/server/shared/Utilities/TaskScheduler.h b/src/server/shared/Utilities/TaskScheduler.h index 98e210e55b1..d45835b5f17 100644 --- a/src/server/shared/Utilities/TaskScheduler.h +++ b/src/server/shared/Utilities/TaskScheduler.h @@ -60,6 +60,10 @@ class TaskScheduler typedef uint32 repeated_t; // Task handle type typedef std::function task_handler_t; + // Predicate type + typedef std::function predicate_t; + // Success handle type + typedef std::function success_t; class Task { @@ -163,27 +167,57 @@ class TaskScheduler /// the next update tick. AsyncHolder _asyncHolder; + predicate_t _predicate; + + static bool EmptyValidator() + { + return true; + } + + static void EmptyCallback() + { + } + public: - TaskScheduler() : self_reference(this, [](TaskScheduler const*) { }), - _now(clock_t::now()) { } + TaskScheduler() + : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(EmptyValidator) { } + + template + TaskScheduler(P&& predicate) + : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward

(predicate)) { } TaskScheduler(TaskScheduler const&) = delete; TaskScheduler(TaskScheduler&&) = delete; TaskScheduler& operator= (TaskScheduler const&) = delete; TaskScheduler& operator= (TaskScheduler&&) = delete; + /// Sets a validator which is asked if tasks are allowed to be executed. + template + TaskScheduler& SetValidator(P&& predicate) + { + _predicate = std::forward

(predicate); + return *this; + } + + /// Clears the validator which is asked if tasks are allowed to be executed. + TaskScheduler& ClearValidator(); + /// Update the scheduler to the current time. - TaskScheduler& Update(); + /// Calls the optional callback on successfully finish. + TaskScheduler& Update(success_t const& callback = EmptyCallback); /// Update the scheduler with a difftime in ms. - TaskScheduler& Update(size_t const milliseconds); + /// Calls the optional callback on successfully finish. + TaskScheduler& Update(size_t const milliseconds, success_t const& callback = EmptyCallback); /// Update the scheduler with a difftime. + /// Calls the optional callback on successfully finish. template - TaskScheduler& Update(std::chrono::duration<_Rep, _Period> const& difftime) + TaskScheduler& Update(std::chrono::duration<_Rep, _Period> const& difftime, + success_t const& callback = EmptyCallback) { _now += difftime; - Dispatch(); + Dispatch(callback); return *this; } @@ -370,7 +404,7 @@ private: } /// Dispatch remaining tasks - void Dispatch(); + void Dispatch(success_t const& callback); }; class TaskContext -- cgit v1.2.3