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 21 namespace Uscript { 22 class UScriptInstructionFactory; 23 class UScriptInstruction; 24 typedef UScriptInstructionFactory* UScriptInstructionFactoryPtr; 25 typedef UScriptInstruction* UScriptInstructionPtr; 26 27 /** 28 * 定义环境变量,记录需要使用的全局对象 29 */ 30 class UScriptEnv { 31 public: UScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager)32 UScriptEnv(Hpackage::PkgManager::PkgManagerPtr pkgManager) : pkgManager_(pkgManager) {} 33 ~UScriptEnv()34 virtual ~UScriptEnv() {} 35 GetPkgManager()36 Hpackage::PkgManager::PkgManagerPtr GetPkgManager() 37 { 38 return pkgManager_; 39 } GetState()40 int32_t GetState() 41 { 42 return state_; 43 } 44 45 virtual void PostMessage(const std::string &cmd, std::string content) = 0; 46 virtual UScriptInstructionFactoryPtr GetInstructionFactory() = 0; 47 virtual const std::vector<std::string> GetInstructionNames() const = 0; 48 virtual bool IsRetry() const = 0; 49 private: 50 Hpackage::PkgManager::PkgManagerPtr pkgManager_ = nullptr; 51 int32_t state_ = 0; 52 }; 53 54 /** 55 * 脚本执行时的上下文描述,在调用脚本指令时,使用这个函数传参 56 * 输入参数使用 GetParam 获取 57 * 输出参数使用 PushParam 添加 58 */ 59 class UScriptContext { 60 public: 61 enum ParamType { 62 PARAM_TYPE_INTEGER = 1, // 整数类型 63 PARAM_TYPE_FLOAT, // float类型 64 PARAM_TYPE_STRING, // string类型 65 PARAM_TYPE_INVALID = -1 66 }; 67 68 virtual ~UScriptContext() = default; 69 70 /** 71 * 按不同的类型添加一个输出参数,可以添加任意输出 72 */ 73 virtual int32_t PushParam(int value) = 0; 74 virtual int32_t PushParam(float value) = 0; 75 virtual int32_t PushParam(const std::string& value) = 0; 76 77 /** 78 * 获取输入参数的个数 79 */ 80 virtual int32_t GetParamCount() = 0; 81 82 /** 83 * 获取对应索引的输入参数的类型 84 */ 85 virtual ParamType GetParamType(int32_t index) = 0; 86 87 /** 88 * 获取对应索引的输入参数的值 89 */ 90 virtual int32_t GetParam(int32_t index, int32_t& value) = 0; 91 virtual int32_t GetParam(int32_t index, float& value) = 0; 92 virtual int32_t GetParam(int32_t index, std::string& value) = 0; 93 }; 94 95 /** 96 * 脚本执行指令,实现对应指令的功能 97 */ 98 class UScriptInstruction { 99 public: 100 virtual ~UScriptInstruction() = default; 101 102 /** 103 * 脚本调用,执行函数 104 * context : 函数执行的上下文信息 105 * 入参:通过GetParam可以获取输入参数的类型和值 106 * 出参:PushParam将输出结果压栈 107 * 返回值:函数处理结果 108 */ 109 virtual int32_t Execute(UScriptEnv &env, UScriptContext &context) = 0; 110 }; 111 112 /** 113 * 脚本执行指令的工厂类,根据指令名创建对应的实例 114 */ 115 class UScriptInstructionFactory { 116 public: 117 // 创建时必须使用new,在程序结束后,会使用delete删除指令对象 118 virtual int32_t CreateInstructionInstance(UScriptInstructionPtr &instr, const std::string &name) = 0; 119 120 virtual ~UScriptInstructionFactory() = default; 121 }; 122 } // namespace Uscript 123 124 #ifdef __cplusplus 125 extern "C" { 126 #endif 127 128 /** 129 * 接口,用来从用户自定义的共享库中获取factory 130 */ 131 Uscript::UScriptInstructionFactoryPtr GetInstructionFactory(); 132 133 #ifdef __cplusplus 134 } 135 #endif 136 #endif