// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#include "Task.h"
class ThreadPool;
///
/// General purpose task executed using Thread Pool.
///
///
class ThreadPoolTask : public Task
{
friend ThreadPool;
protected:
///
/// Initializes a new instance of the class.
///
ThreadPoolTask()
: Task()
{
}
public:
// [Task]
String ToString() const override;
protected:
// [Task]
void Enqueue() override;
};
///
/// General purpose task executing custom action using Thread Pool.
///
///
///
class ThreadPoolActionTask : public ThreadPoolTask
{
protected:
Function _action1;
Function _action2;
Object* _target;
public:
///
/// Initializes a new instance of the class.
///
/// The action.
/// The target object.
ThreadPoolActionTask(const Function& action, Object* target = nullptr)
: ThreadPoolTask()
, _action1(action)
, _target(target)
{
}
///
/// Initializes a new instance of the class.
///
/// The action.
/// The target object.
ThreadPoolActionTask(Function::Signature action, Object* target = nullptr)
: ThreadPoolTask()
, _action1(action)
, _target(target)
{
}
///
/// Initializes a new instance of the class.
///
/// The action.
/// The target object.
ThreadPoolActionTask(const Function& action, Object* target = nullptr)
: ThreadPoolTask()
, _action2(action)
, _target(target)
{
}
///
/// Initializes a new instance of the class.
///
/// The action.
/// The target object.
ThreadPoolActionTask(Function::Signature action, Object* target = nullptr)
: ThreadPoolTask()
, _action2(action)
, _target(target)
{
}
public:
// [ThreadPoolTask]
bool HasReference(Object* obj) const override
{
return obj == _target;
}
protected:
// [ThreadPoolTask]
bool Run() override
{
if (_action1.IsBinded())
{
_action1();
return false;
}
if (_action2.IsBinded())
{
return _action2();
}
return false;
}
};