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.
This commit is contained in:
Naios
2015-06-03 18:24:21 +02:00
parent ed2607f128
commit 151a0f5c5e
2 changed files with 93 additions and 53 deletions

View File

@@ -62,7 +62,7 @@ TaskScheduler& TaskScheduler::CancelGroupsOf(std::vector<group_t> const& groups)
TaskScheduler& TaskScheduler::InsertTask(TaskContainer task)
{
_task_holder.Push(std::forward<TaskContainer>(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<TaskScheduler>(self_reference)));
TaskContext context(_task_holder.Pop(), std::weak_ptr<TaskScheduler>(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<TaskScheduler&(TaskScheduler&)> const& apply)
TaskContext& TaskContext::Dispatch(std::function<TaskScheduler&(TaskScheduler&)> const& apply)
{
if (auto const owner = _owner.lock())
apply(*owner);
@@ -149,56 +148,61 @@ TaskContextInstance& TaskContextInstance::Dispatch(std::function<TaskScheduler&(
return *this;
}
bool TaskContextInstance::IsInGroup(TaskScheduler::group_t const group) const
bool TaskContext::IsExpired() const
{
return _owner.expired();
}
bool TaskContext::IsInGroup(TaskScheduler::group_t const group) const
{
return _task->IsInGroup(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<void()> const& callable)
TaskContext& TaskContext::Async(std::function<void()> 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<TaskScheduler::group_t> const& groups)
TaskContext& TaskContext::CancelGroupsOf(std::vector<TaskScheduler::group_t> 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);
}