1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-20 17:17:13 +02:00

Use polymorphism to deliver jobs.

This commit is contained in:
Sandu Liviu Catalin
2020-09-06 23:09:54 +03:00
parent 38b874f444
commit 50a61c69e6
5 changed files with 305 additions and 109 deletions

View File

@ -4,11 +4,56 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(SqBaseJob, _SC("SqBaseJob"))
SQMODE_DECL_TYPENAME(SqEvaluateJob, _SC("SqEvaluateJob"))
SQMODE_DECL_TYPENAME(SqExecuteJob, _SC("SqExecuteJob"))
// ================================================================================================
void Register_Job(HSQUIRRELVM vm)
{
Table jbns(vm);
{
using Type = JobWrapperBase;
jbns.Bind(_SC("Base"),
Class< Type, NoConstructor< Type > >(vm, SqBaseJob::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqBaseJob::Fn)
// Properties
.Prop(_SC("Callback"), &Type::GetCallback, &Type::SetCallback)
// Core Methods
.CbFunc(_SC("SetCallback"), &Type::SetCallback)
);
}
{
using Type = JobWrapper< EvaluateJob >;
jbns.Bind(_SC("Evaluate"),
DerivedClass< Type, JobWrapperBase, NoCopy< Type > >(vm, SqEvaluateJob::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqEvaluateJob::Fn)
// Properties
.Prop(_SC("Code"), &Type::GetCode, &Type::SetCode)
.Prop(_SC("Name"), &Type::GetName, &Type::SetName)
// Core Methods
.CbFunc(_SC("SetCode"), &Type::SetCode)
.FmtFunc(_SC("SetName"), &Type::ApplyName)
);
}
{
using Type = JobWrapper< ExecuteJob >;
jbns.Bind(_SC("Execute"),
DerivedClass< Type, JobWrapperBase, NoCopy< Type > >(vm, SqExecuteJob::Str)
// Meta-methods
.SquirrelFunc(_SC("_typename"), &SqExecuteJob::Fn)
);
}
RootTable(vm).Bind(_SC("SqJob"), jbns);
}

View File

@ -4,70 +4,276 @@
#include "Base/Shared.hpp"
#include "Library/Worker/Parameter.hpp"
// ------------------------------------------------------------------------------------------------
#include <memory>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Handle validation.
*/
#if defined(_DEBUG) || defined(SQMOD_EXCEPTLOC)
#define SQMOD_VALIDATE(x) (x).Validate(__FILE__, __LINE__)
#else
#define SQMOD_VALIDATE(x) (x).Validate()
#endif // _DEBUG
// ------------------------------------------------------------------------------------------------
struct Worker;
/* ------------------------------------------------------------------------------------------------
* Used to represent a job that a worker must do, as well as a reply from the worker with the result.
*/
struct Job
struct BaseJob
{
using Parameters = Parameter::ArrayType;
/* --------------------------------------------------------------------------------------------
* Describe the typeof job a worker must do.
*/
enum class Type : uint8_t
{
Null=0, // Null/non existent job.
Stop, // Inform the worker to stop.
Eval, // Inform the worker to evaluate some code.
Exec // Inform the worker to execute a file.
};
// --------------------------------------------------------------------------------------------
Type mType; // Job type.
String mTarget; // Where to perform the job.
String mPayload; // Where to perform the job.
Function mCallback; // Function to call once completed.
Parameter mResponse; // The value given by the worker.
Parameters mParameters; // Job parameters.
Function mCallback; // Function to call once completed.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Job()
: mType(Type::Null), mTarget(), mPayload(), mCallback(), mResponse(), mParameters()
BaseJob()
: mCallback()
{
}
/* --------------------------------------------------------------------------------------------
* Default constructor.
* Copy constructor (disabled).
*/
Job(Function && callback)
: mType(Type::Null), mTarget(), mPayload()
, mCallback(std::forward< Function >(callback)), mResponse(), mParameters()
{
}
BaseJob(const BaseJob & o) = delete;
/* --------------------------------------------------------------------------------------------
* Copy constructor.
* Move constructor (disabled).
*/
Job(const Job & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Job(Job && o) noexcept = default;
BaseJob(BaseJob && o) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Job() = default;
virtual ~BaseJob() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment.
* Copy assignment operator (disabled).
*/
Job & operator = (const Job & o) = default;
BaseJob & operator = (const BaseJob & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment.
* Move assignment operator (disabled).
*/
Job & operator = (Job && o) = default;
BaseJob & operator = (BaseJob && o) = delete;
/* --------------------------------------------------------------------------------------------
* Create a new job that will replace the specified job.
*/
virtual BaseJob * New(BaseJob & job) = 0;
/* --------------------------------------------------------------------------------------------
* Invoked inside a worker to perform the job.
*/
virtual bool Start(HSQUIRRELVM vm, Worker & worker) = 0;
/* --------------------------------------------------------------------------------------------
* Invoked inside a worker to perform the job.
*/
virtual void Finish(HSQUIRRELVM vm, Worker & worker)
{
// Do we have a callback?
if (!mCallback.IsNull())
{
mCallback.Execute();
}
}
};
/* ------------------------------------------------------------------------------------------------
* Tell the worker to evaluate a piece of code.
*/
struct EvaluateJob : public BaseJob
{
// --------------------------------------------------------------------------------------------
String mCode; // The code to evaluate.
String mName; // How to refer to this code in errors.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
EvaluateJob()
: BaseJob(), mCode(), mName()
{
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
virtual ~EvaluateJob() override = default;
/* --------------------------------------------------------------------------------------------
* Create a new job that will replace the specified job.
*/
BaseJob * New(BaseJob & job) override
{
return new EvaluateJob();
}
/* --------------------------------------------------------------------------------------------
*
*/
bool Start(HSQUIRRELVM vm, Worker & worker) override
{
sq_compilebuffer(vm, mCode.data(), mCode.size(), mName.data(), SQTrue);
SQInteger top = sq_gettop(vm);
sq_pushroottable(vm);
sq_call(vm, 1, false, true);
sq_settop(vm, top);
return true;
}
};
/* ------------------------------------------------------------------------------------------------
* Tell the worker to execute the code from a file.
*/
struct ExecuteJob : public BaseJob
{
// --------------------------------------------------------------------------------------------
String mFile; // The file to evaluate.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
ExecuteJob()
: BaseJob()
{
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
virtual ~ExecuteJob() override = default;
/* --------------------------------------------------------------------------------------------
* Create a new job that will replace the specified job.
*/
BaseJob * New(BaseJob & job) override
{
return new ExecuteJob();
}
/* --------------------------------------------------------------------------------------------
*
*/
bool Start(HSQUIRRELVM vm, Worker & worker) override
{
std::puts("Executing...");
return true;
}
};
/* ------------------------------------------------------------------------------------------------
* Used internally to wrap a job and expose it to the script.
*/
struct JobWrapperBase
{
/* --------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
JobWrapperBase(const JobWrapperBase & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
JobWrapperBase(JobWrapperBase && o) noexcept = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~JobWrapperBase()
{
// There should always be an instance
delete m_Inst;
m_Inst = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
JobWrapperBase & operator = (const JobWrapperBase & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator (disabled).
*/
JobWrapperBase & operator = (JobWrapperBase && o) = delete;
/* --------------------------------------------------------------------------------------------
* Retrieve a raw pointer to the managed instance.
*/
BaseJob * Get() const
{
return m_Inst;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a managed pointer managed instance and yield ownership.
*/
std::unique_ptr< BaseJob > Grab()
{
std::unique_ptr< BaseJob > ptr(m_Inst);
m_Inst = ptr->New(*m_Inst);
return ptr;
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetCallback(Function & cb)
{
m_Inst->mCallback = std::move(cb);
}
/* --------------------------------------------------------------------------------------------
*
*/
const Function & GetCallback() const
{
return m_Inst->mCallback;
}
protected:
/* --------------------------------------------------------------------------------------------
* Instance of the wrapped job.
*/
BaseJob * m_Inst;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
JobWrapperBase(BaseJob * job) noexcept
: m_Inst(job)
{
}
};
/* ------------------------------------------------------------------------------------------------
* Used internally to wrap a job and expose it to the script.
*/
template < class T > struct JobWrapper : public JobWrapperBase
{
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
JobWrapper()
: JobWrapperBase(new T)
{
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetCode(StackStrF & str)
{
static_cast< T * >(m_Inst)->mCode.assign(str.mPtr, static_cast< size_t >(str.mLen));
}
/* --------------------------------------------------------------------------------------------
*
*/
const String & GetCode() const
{
return static_cast< T * >(m_Inst)->mCode;
}
/* --------------------------------------------------------------------------------------------
*
*/
void SetName(StackStrF & str)
{
static_cast< T * >(m_Inst)->mName.assign(str.mPtr, static_cast< size_t >(str.mLen));
}
/* --------------------------------------------------------------------------------------------
*
*/
JobWrapper< T > & ApplyName(StackStrF & str)
{
SetName(str);
return *this;
}
/* --------------------------------------------------------------------------------------------
*
*/
const String & GetName() const
{
return static_cast< T * >(m_Inst)->mName;
}
};
} // Namespace:: SqMod