From 7342bc84f52e2f27fd08e3e3a0392acaabade624 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 12 Feb 2018 16:20:33 -0800 Subject: [PATCH] 'Hello world.' implementation of a Job engine --- libraries/workload/src/workload/Engine.cpp | 57 ++++++++++++++++ libraries/workload/src/workload/Engine.h | 76 ++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 libraries/workload/src/workload/Engine.cpp create mode 100644 libraries/workload/src/workload/Engine.h diff --git a/libraries/workload/src/workload/Engine.cpp b/libraries/workload/src/workload/Engine.cpp new file mode 100644 index 0000000000..7e69731d4e --- /dev/null +++ b/libraries/workload/src/workload/Engine.cpp @@ -0,0 +1,57 @@ +// +// Engine.cpp +// libraries/workload/src/workload +// +// Created by Andrew Meadows 2018.02.08 +// Copyright 2018 High Fidelity, Inc. +// +// Originally from lighthouse3d. Modified to utilize glm::vec3 and clean up to our coding standards +// Simple plane class. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "Engine.h" + +#include + +namespace workload { + + // the "real Job" + class HelloWorld { + QString _message; + bool _isEnabled { true }; + public: + using JobModel = Job::Model; + HelloWorld() {} + void configure(const HelloWorldConfig& configuration) { + _isEnabled = configuration.isEnabled(); + _message = configuration.getMessage(); + } + void run(const WorkloadContextPointer& context) { + if (_isEnabled) { + std::cout << _message.toStdString() << std::endl; + } + } + }; + + WorkloadContext::WorkloadContext() : task::JobContext(trace_workload()) {} + + using EngineModel = Task::Model; + + // the 'Builder' is the 'Data' on which the EngineModel templatizes. + // It must implement build() which is called by EngineModel::create(). + class HelloWorldBuilder { + public: + using JobModel = Task::Model; + void build(EngineModel& model, const Varying& in, Varying& out) { + model.addJob("helloWorld"); + } + }; + + Engine::Engine() : Task("Engine", EngineModel::create()), + _context(std::make_shared()) { + } +} // namespace workload + diff --git a/libraries/workload/src/workload/Engine.h b/libraries/workload/src/workload/Engine.h new file mode 100644 index 0000000000..a422c5d715 --- /dev/null +++ b/libraries/workload/src/workload/Engine.h @@ -0,0 +1,76 @@ +// +// Engine.h +// libraries/workload/src/workload +// +// Created by Andrew Meadows 2018.02.08 +// Copyright 2018 High Fidelity, Inc. +// +// Originally from lighthouse3d. Modified to utilize glm::vec3 and clean up to our coding standards +// Simple plane class. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_workload_Engine_h +#define hifi_workload_Engine_h + +#include +#include + +#include + +namespace workload { + + // How to make an Engine under the task::Task paradigm... + + // (1) Derive class C from task::JobContext + class WorkloadContext : public task::JobContext { + public: + WorkloadContext(); + virtual ~WorkloadContext() {} + }; + using WorkloadContextPointer = std::shared_ptr; + + // (2) Apply a macro which will create local aliases (via "using") for example: + // using Task = task::Task; + Task_DeclareTypeAliases(WorkloadContext) + + // (3) You'll need a 'real Job' but it will need a Config for exposing settings to JS, + // and you should do that here: + class HelloWorldConfig : public Job::Config { + Q_OBJECT + Q_PROPERTY(QString message READ getMessage WRITE setMessage) + QString _message {"Hello World."}; + public: + HelloWorldConfig() : Job::Config(true) {} + QString getMessage() const { return _message; } + void setMessage(const QString& msg) { _message = msg; } + }; + + // (4) In cpp file the 'real Job' will need a 'builder'. The 'builder' is the 'Data' argument + // for the Model template. + // Data must implement Data::build(). + // Data::build() is called when the Model is added (in Engine ctor) as the first child job of the Engine + + // (5) Engine derives from task::Task and will run all the Job's + class Engine : public Task { + public: + Engine(); + ~Engine() = default; + + // (6) The Engine's Context is passed to its Jobs when they are run() + void run() { assert(_context); Task::run(_context); } + + protected: + // (6) Again, the Engine's Context is passed to its Jobs when they are run() + void run(const WorkloadContextPointer& context) override { assert(_context); Task::run(_context); } + + private: + WorkloadContextPointer _context; + }; + using EnginePointer = std::shared_ptr; + +} // namespace workload + +#endif // hifi_workload_Space_h