From df80f1900d8ffc5f366ede8e19bfbeaeb82c954c Mon Sep 17 00:00:00 2001 From: Naios Date: Wed, 3 Jun 2015 18:24:21 +0200 Subject: Core/Shared: Hide the internal shared_ptr of the TaskContext * use . operator instead of -> to access the TaskContext now. * Add TaskContext::IsExpired to check whether the owner has expired. (cherry picked from commit 151a0f5c5e1133a21c46a8d55c834ff52061c3fa) --- src/server/shared/Utilities/TaskScheduler.cpp | 40 +++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'src/server/shared/Utilities/TaskScheduler.cpp') diff --git a/src/server/shared/Utilities/TaskScheduler.cpp b/src/server/shared/Utilities/TaskScheduler.cpp index c945ad4dab0..4b261413fd9 100644 --- a/src/server/shared/Utilities/TaskScheduler.cpp +++ b/src/server/shared/Utilities/TaskScheduler.cpp @@ -62,7 +62,7 @@ TaskScheduler& TaskScheduler::CancelGroupsOf(std::vector const& groups) TaskScheduler& TaskScheduler::InsertTask(TaskContainer task) { - _task_holder.Push(std::forward(task)); + _task_holder.Push(std::move(task)); return *this; } @@ -82,11 +82,10 @@ void TaskScheduler::Dispatch() // Perfect forward the context to the handler // Use weak references to catch destruction before callbacks. - TaskContext context(new TaskContextInstance(_task_holder.Pop(), - std::weak_ptr(self_reference))); + TaskContext context(_task_holder.Pop(), std::weak_ptr(self_reference)); // Invoke the context - context->Invoke(); + context.Invoke(); } } @@ -141,7 +140,7 @@ bool TaskScheduler::TaskQueue::IsEmpty() const return container.empty(); } -TaskContextInstance& TaskContextInstance::Dispatch(std::function const& apply) +TaskContext& TaskContext::Dispatch(std::function const& apply) { if (auto const owner = _owner.lock()) apply(*owner); @@ -149,56 +148,61 @@ TaskContextInstance& TaskContextInstance::Dispatch(std::functionIsInGroup(group); } -TaskContextInstance& TaskContextInstance::SetGroup(TaskScheduler::group_t const group) +TaskContext& TaskContext::SetGroup(TaskScheduler::group_t const group) { _task->_group = group; return *this; } -TaskContextInstance& TaskContextInstance::ClearGroup() +TaskContext& TaskContext::ClearGroup() { _task->_group = boost::none; return *this; } -TaskScheduler::repeated_t TaskContextInstance::GetRepeatCounter() const +TaskScheduler::repeated_t TaskContext::GetRepeatCounter() const { return _task->_repeated; } -TaskContextInstance& TaskContextInstance::Async(std::function const& callable) +TaskContext& TaskContext::Async(std::function const& callable) { return Dispatch(std::bind(&TaskScheduler::Async, std::placeholders::_1, callable)); } -TaskContextInstance& TaskContextInstance::CancelAll() +TaskContext& TaskContext::CancelAll() { return Dispatch(std::mem_fn(&TaskScheduler::CancelAll)); } -TaskContextInstance& TaskContextInstance::CancelGroup(TaskScheduler::group_t const group) +TaskContext& TaskContext::CancelGroup(TaskScheduler::group_t const group) { return Dispatch(std::bind(&TaskScheduler::CancelGroup, std::placeholders::_1, group)); } -TaskContextInstance& TaskContextInstance::CancelGroupsOf(std::vector const& groups) +TaskContext& TaskContext::CancelGroupsOf(std::vector const& groups) { - return Dispatch(std::bind(&TaskScheduler::CancelGroupsOf, std::placeholders::_1, groups)); + return Dispatch(std::bind(&TaskScheduler::CancelGroupsOf, std::placeholders::_1, std::cref(groups))); } -void TaskContextInstance::AssertOnConsumed() +void TaskContext::AssertOnConsumed() const { // This was adapted to TC to prevent static analysis tools from complaining. // If you encounter this assertion check if you repeat a TaskContext more then 1 time! - ASSERT(_task && "Bad task logic, task context was consumed already!"); + ASSERT(!(*_consumed) && "Bad task logic, task context was consumed already!"); } -void TaskContextInstance::Invoke() +void TaskContext::Invoke() { - _task->_task(shared_from_this()); + _task->_task(*this); } -- cgit v1.2.3