From de97d239baf32665fb78e88c61ce3b4847e0d5e9 Mon Sep 17 00:00:00 2001 From: Andreas Drewke Date: Wed, 10 Jan 2024 09:22:37 +0100 Subject: [PATCH] ext: updated MiniScript --- .../miniscript/HTTPDownloadClientClass.cpp | 337 ++++++++++++++++++ .../miniscript/HTTPDownloadClientClass.h | 80 +++++ 2 files changed, 417 insertions(+) create mode 100644 ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.cpp create mode 100644 ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.h diff --git a/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.cpp b/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.cpp new file mode 100644 index 000000000..2c7b7865a --- /dev/null +++ b/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.cpp @@ -0,0 +1,337 @@ +#include +#include +#include + +#include + +#include +#include +#include +#include + +using std::make_shared; +using std::shared_ptr; +using std::span; +using std::string; + +using miniscript::miniscript::HTTPDownloadClientClass; + +using miniscript::miniscript::MiniScript; +using _Console = miniscript::utilities::Console; +using _HTTPDownloadClient = miniscript::network::httpclient::HTTPDownloadClient; + +const string HTTPDownloadClientClass::TYPE_NAME = "HTTPDownloadClient"; +vector> HTTPDownloadClientClass::instances; + +void HTTPDownloadClientClass::initialize() { +} + +void HTTPDownloadClientClass::registerConstants(MiniScript* miniScript) const { +} + +void HTTPDownloadClientClass::registerMethods(MiniScript* miniScript) const { + // + { + // + class MethodHTTPDownloadClient: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClient(MiniScript* miniScript): + MiniScript::Method({}, MiniScript::TYPE_HTTPDOWNLOADCLIENT), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + auto httpDownloadClient = make_shared<_HTTPDownloadClient>(); + returnValue.setType(MiniScript::TYPE_HTTPDOWNLOADCLIENT); + returnValue.setValue(&httpDownloadClient); + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClient(miniScript)); + } + { + // + class MethodHTTPDownloadClientGetURL: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientGetURL(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_STRING + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::getURL"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + returnValue.setValue(httpDownloadClient->getURL()); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientGetURL(miniScript)); + } + { + // + class MethodHTTPDownloadClientSetURL: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientSetURL(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + { .type = MiniScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_NULL + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::setURL"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + string url; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true && + MiniScript::getStringValue(arguments, 1, url, false) == true) { + httpDownloadClient->setURL(url); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientSetURL(miniScript)); + } + { + // + class MethodHTTPDownloadClientGetFile: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientGetFile(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_STRING + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::getFile"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + returnValue.setValue(httpDownloadClient->getFile()); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientGetFile(miniScript)); + } + { + // + class MethodHTTPDownloadClientSetFile: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientSetFile(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + { .type = MiniScript::TYPE_STRING, .name = "url", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_NULL + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::setFile"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + string file; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true && + MiniScript::getStringValue(arguments, 1, file, false) == true) { + httpDownloadClient->setFile(file); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientSetFile(miniScript)); + } + { + // + class MethodHTTPDownloadClientReset: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientReset(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_NULL + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::reset"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + httpDownloadClient->reset(); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientReset(miniScript)); + } + { + // + class MethodHTTPDownloadClientExecute: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientExecute(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_NULL + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::execute"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + httpDownloadClient->start(); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientExecute(miniScript)); + } + { + // + class MethodHTTPDownloadClientGetProgress: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientGetProgress(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_FLOAT + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::getProgress"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + returnValue.setValue(httpDownloadClient->getProgress()); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientGetProgress(miniScript)); + } + { + // + class MethodHTTPDownloadClientIsFinished: public MiniScript::Method { + private: + MiniScript* miniScript { nullptr }; + public: + MethodHTTPDownloadClientIsFinished(MiniScript* miniScript): + MiniScript::Method( + { + { .type = MiniScript::TYPE_HTTPDOWNLOADCLIENT, .name = "httpdownloadclient", .optional = false, .reference = false, .nullable = false }, + }, + MiniScript::TYPE_BOOLEAN + ), + miniScript(miniScript) {} + const string getMethodName() override { + return "HTTPDownloadClient::isFinished"; + } + void executeMethod(span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) override { + shared_ptr<_HTTPDownloadClient> httpDownloadClient; + if (HTTPDownloadClientClass::getHTTPDownloadClientValue(arguments, 0, httpDownloadClient, false) == true) { + returnValue.setValue(httpDownloadClient->isFinished()); + } else { + _Console::println(getMethodName() + "(): " + miniScript->getStatementInformation(statement) + ": argument mismatch: expected arguments: " + miniScript->getArgumentInformation(getMethodName())); + miniScript->startErrorScript(); + } + } + }; + miniScript->registerMethod(new MethodHTTPDownloadClientIsFinished(miniScript)); + } +} + +void HTTPDownloadClientClass::unsetVariableValue(MiniScript::Variable& variable) const { + delete static_cast*>(variable.getValuePtr()); +} + +void HTTPDownloadClientClass::setVariableValue(MiniScript::Variable& variable) const { + auto sharedPtr = make_shared<_HTTPDownloadClient>(); + instances.push_back(sharedPtr); + variable.setValuePtr(new shared_ptr<_HTTPDownloadClient>(sharedPtr)); +} + +void HTTPDownloadClientClass::setVariableValue(MiniScript::Variable& variable, const void* value) const { + *static_cast*>(variable.getValuePtr()) = *static_cast*>(value); +} + +void HTTPDownloadClientClass::copyVariable(MiniScript::Variable& to, const MiniScript::Variable& from) const { + to.setType(MiniScript::TYPE_HTTPDOWNLOADCLIENT); + *static_cast*>(to.getValuePtr()) = *static_cast*>(from.getValuePtr()); +} + +bool HTTPDownloadClientClass::mul(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const { + return false; +} + +bool HTTPDownloadClientClass::div(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const { + return false; +} + +bool HTTPDownloadClientClass::add(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const { + return false; +} + +bool HTTPDownloadClientClass::sub(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const { + return false; +} + +const string& HTTPDownloadClientClass::getTypeAsString() const { + return TYPE_NAME; +} + +const string HTTPDownloadClientClass::getValueAsString(const MiniScript::Variable& variable) const { + const auto& httpDownloadClient = *static_cast*>(variable.getValuePtr()); + return "HTTPDownloadClientClass(url: " + httpDownloadClient->getURL() + ", file: " + httpDownloadClient->getFile() + ")"; +} + diff --git a/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.h b/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.h new file mode 100644 index 000000000..a95dcd24a --- /dev/null +++ b/ext/miniscript/src/miniscript/miniscript/HTTPDownloadClientClass.h @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include +#include + +using std::shared_ptr; +using std::span; +using std::vector; + +using miniscript::miniscript::MiniScript; +using _HTTPDownloadClient = miniscript::network::httpclient::HTTPDownloadClient; + +/** + * HTTP download client + * @author Andreas Drewke + */ +class miniscript::miniscript::HTTPDownloadClientClass final: public MiniScript::DataType { +private: + MINISCRIPT_STATIC_DLL_IMPEXT static const string TYPE_NAME; + MINISCRIPT_STATIC_DLL_IMPEXT static vector> instances; + + // overridden methods + void registerConstants(MiniScript* miniScript) const override; + void registerMethods(MiniScript* miniScript) const override; + void unsetVariableValue(MiniScript::Variable& variable) const override; + void setVariableValue(MiniScript::Variable& variable) const override; + void setVariableValue(MiniScript::Variable& variable, const void* value) const override; + void copyVariable(MiniScript::Variable& to, const MiniScript::Variable& from) const override; + bool mul(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const override; + bool div(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const override; + bool add(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const override; + bool sub(MiniScript* miniScript, const span& arguments, MiniScript::Variable& returnValue, const MiniScript::Statement& statement) const override; + +public: + // forbid class copy + FORBID_CLASS_COPY(HTTPDownloadClientClass) + + /** + * Initialize + */ + static void initialize(); + + /** + * Get HTTPDownloadClient value from given variable + * @param arguments arguments + * @param idx argument index + * @param value value + * @param optional optional + * @return success + */ + static inline bool getHTTPDownloadClientValue(const span& arguments, int idx, shared_ptr<_HTTPDownloadClient>& value, bool optional = false) { + if (idx >= arguments.size()) return optional; + const auto& argument = arguments[idx]; + if (argument.getType() == MiniScript::TYPE_HTTPDOWNLOADCLIENT) { + value = *static_cast*>(argument.getValuePtr()); + return true; + } + return optional; + + } + + /** + * MiniScript Vector2 data type + */ + HTTPDownloadClientClass(): MiniScript::DataType(false) { + // + } + + // overridden methods + const string& getTypeAsString() const override; + const string getValueAsString(const MiniScript::Variable& variable) const override; + +};