1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SCRIPT_INSTRUCTION_H 16 #define SCRIPT_INSTRUCTION_H 17 18 #include <string> 19 #include "pkg_manager.h" 20 #include "updater/updater.h" 21 22 namespace Uscript { 23 class UScriptInstructionFactory; 24 class UScriptInstruction; 25 typedef UScriptInstructionFactory* UScriptInstructionFactoryPtr; 26 typedef UScriptInstruction* UScriptInstructionPtr; 27 28 /** 29 * 定义环境变量,记录需要使用的全局对象 30 */ 31 class UScriptEnv { 32 public: UScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)33 UScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : pkgManager_(pkgManager) {} 34 ~UScriptEnv()35 virtual ~UScriptEnv() {} 36 GetPkgManager()37 Hpackage::PkgManager::PkgManagerPtr GetPkgManager() 38 { 39 return pkgManager_; 40 } GetState()41 int32_t GetState() 42 { 43 return state_; 44 } 45 46 virtual void PostMessage(const std::string &cmd, std::string content) = 0; 47 virtual UScriptInstructionFactoryPtr GetInstructionFactory() = 0; 48 virtual const std::vector<std::string> GetInstructionNames() const = 0; 49 virtual bool IsRetry() const = 0; 50 virtual Updater::PostMessageFunction GetPostmsgFunc() = 0; 51 private: 52 Hpackage::PkgManager::PkgManagerPtr pkgManager_ = nullptr; 53 int32_t state_ = 0; 54 }; 55 56 /** 57 * 脚本执行时的上下文描述,在调用脚本指令时,使用这个函数传参 58 * 输入参数使用 GetParam 获取 59 * 输出参数使用 PushParam 添加 60 */ 61 class UScriptContext { 62 public: 63 enum ParamType { 64 PARAM_TYPE_INTEGER = 1, // 整数类型 65 PARAM_TYPE_FLOAT, // float类型 66 PARAM_TYPE_STRING, // string类型 67 PARAM_TYPE_INVALID = -1 68 }; 69 70 virtual ~UScriptContext() = default; 71 72 /** 73 * 按不同的类型添加一个输出参数,可以添加任意输出 74 */ 75 virtual int32_t PushParam(int value) = 0; 76 virtual int32_t PushParam(float value) = 0; 77 virtual int32_t PushParam(const std::string& value) = 0; 78 79 /** 80 * 获取输入参数的个数 81 */ 82 virtual int32_t GetParamCount() = 0; 83 84 /** 85 * 获取对应索引的输入参数的类型 86 */ 87 virtual ParamType GetParamType(int32_t index) = 0; 88 89 /** 90 * 获取对应索引的输入参数的值 91 */ 92 virtual int32_t GetParam(int32_t index, int32_t& value) = 0; 93 virtual int32_t GetParam(int32_t index, float& value) = 0; 94 virtual int32_t GetParam(int32_t index, std::string& value) = 0; 95 }; 96 97 /** 98 * 脚本执行指令,实现对应指令的功能 99 */ 100 class UScriptInstruction { 101 public: 102 virtual ~UScriptInstruction() = default; 103 104 /** 105 * 脚本调用,执行函数 106 * context : 函数执行的上下文信息 107 * 入参:通过GetParam可以获取输入参数的类型和值 108 * 出参:PushParam将输出结果压栈 109 * 返回值:函数处理结果 110 */ 111 virtual int32_t Execute(UScriptEnv &env, UScriptContext &context) = 0; 112 }; 113 114 /** 115 * 脚本执行指令的工厂类,根据指令名创建对应的实例 116 */ 117 class UScriptInstructionFactory { 118 public: 119 // 创建时必须使用new,在程序结束后,会使用delete删除指令对象 120 virtual int32_t CreateInstructionInstance(UScriptInstructionPtr &instr, const std::string &name) = 0; 121 122 virtual ~UScriptInstructionFactory() = default; 123 }; 124 } // namespace Uscript 125 126 #ifdef __cplusplus 127 extern "C" { 128 #endif 129 130 /** 131 * 接口,用来从用户自定义的共享库中获取factory 132 */ 133 Uscript::UScriptInstructionFactoryPtr GetInstructionFactory(); 134 135 #ifdef __cplusplus 136 } 137 #endif 138 #endif