🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
PythonExecutor.hpp
浏览该文件的文档.
1
21
22#pragma once
23
24#include "AstGlobal.h"
25#include "ScriptExecutor.hpp"
26
27struct _object;
28typedef struct _object PyObject;
29
30AST_NAMESPACE_BEGIN
31
32class PythonAPI;
33
37class AST_SCRIPT_API PythonExecutor : public ScriptExecutor
38{
39public:
41 ~PythonExecutor() override;
42
43 errc_t initialize() override;
44 void finalize() override;
45 errc_t execute(StringView script, ScriptResult* resultOut = nullptr) override;
46 errc_t evaluate(StringView expression, ScriptResult* resultOut = nullptr) override;
47 std::string getLastError() const override;
48
49 errc_t setVariable(StringView name, StringView value) override;
50 errc_t setVariable(StringView name, double value) override;
51 errc_t setVariable(StringView name, int value) override;
52 errc_t setVariable(StringView name, bool value) override;
53
54 // 防止 const char* 被隐式转换为 bool
55 errc_t setVariable(StringView name, const char* value) {
56 return setVariable(name, StringView(value));
57 }
58
59 errc_t getVariable(StringView name, std::string& value) const override;
60 errc_t getVariable(StringView name, double& value) const override;
61 errc_t getVariable(StringView name, int& value) const override;
62 errc_t getVariable(StringView name, bool& value) const override;
63
64protected:
65 std::string captureError() const;
66
67protected:
68 PythonAPI* api_;
69 PyObject* globals_{nullptr}; // 私有无名 dict,每个 executor 独立命名空间
70 mutable std::string lastError_;
71};
72
73AST_NAMESPACE_END
Python API — 与 Python C API 接口一致的动态加载包装
定义 PythonAPI.hpp:49
Python 脚本执行器
定义 PythonExecutor.hpp:38
脚本执行器,用于执行外部脚本
定义 ScriptExecutor.hpp:80
virtual std::string getLastError() const =0
获取最近一次执行脚本时的错误信息
virtual errc_t execute(StringView script, ScriptResult *resultOut=nullptr)=0
执行脚本语句
virtual errc_t initialize()=0
初始化脚本执行器
virtual errc_t setVariable(StringView name, StringView value)=0
设置脚本执行器的变量
virtual errc_t evaluate(StringView expression, ScriptResult *resultOut=nullptr)
对脚本表达式进行求值
定义 ScriptExecutor.cpp:111
virtual errc_t getVariable(StringView name, std::string &value) const =0
获取脚本执行器的字符串变量值
virtual void finalize()=0
结束脚本执行器
脚本执行结果
定义 ScriptExecutor.hpp:64