gpt4 book ai didi

c++ - 如何使用 boost 和 Firebreath 从 python 中正确触发浏览器事件

转载 作者:太空狗 更新时间:2023-10-29 23:08:31 25 4
gpt4 key购买 nike

首先我必须说,作为一名 Python 程序员,我可能从错误的角度看待这个问题,自从我在大学里写完我的最后一个 c++ 代码以来已经过去了很多年。

我在尝试使用 firebreath 创建混合 python/c++ 插件时遇到了一些问题。到目前为止,我已经成功地使用 boost/python.h 集成了所有部分,但是当我尝试从 python 中触发事件时出现了问题。我偶然发现了必须将 python 函数与 c++ 函数绑定(bind)在一起的问题(使用 BOOST_PYTHON_MODULE)。首先,我尝试直接将 python 与我的 JSAPI 派生类 fbtestconpythonAPI 绑定(bind),这种方法的问题似乎是缺少对浏览器实例化的 JSAPI 对象的引用,给我带来了 python 函数和 c++ 等效函数之间的各种签名不匹配问题执行时间处理时间。

我想到的唯一解决这个问题的方法(我同意,这是一个丑陋肮脏的解决方案)是使用我用 set_pluginPointer 手动初始化的全局指针。到目前为止,这实际上工作得很好,但我知道这不是正确的方法。我读到我不应该对 JSAPI 对象使用“原始”指针,但我不确定如何为这个特定的实现用 shared_ptr 替换它。另一个问题是全局变量,它在所有实例之间共享,例如,导致所有事件都在最后打开的选项卡/窗口上触发。解决后者的一种方法是创建某种数组,索引是当前窗口/线程 id,这是我应该能够从我的 JSAPI 对象和 python/c++ 函数访问的东西。

当然,对于任何关于如何改进/修复此特​​定变通方法的建议,或者更好的是,在没有黑客攻击的情况下交流 boost::python 和 firebreath 的正确方法,我当然是开放的,并且会非常感激。

下面是插件代码的相关部分

// Global pointer to plugin instance
fbtestconpythonAPI *fbtestPtr;
void fbtestconpythonAPI::set_pluginPointer(const std::string& val){
m_testString = val;
fbtestPtr = this; //Global pointer initialization
}

void echo(std::string x){
// Firing the echo event on the plugin instance using the global raw pointer
fbtestPtr->fire_echo(x, 1);
}

BOOST_PYTHON_MODULE(Pointless) {
def("echo", echo);
}

FB::variant fbtestconpythonAPI::echo(const FB::variant& msg){
int result_value;
Py_Initialize();

try {
initPointless(); // initialize Pointless

PyRun_SimpleString("import Pointless");
PyRun_SimpleString("Pointless.echo('hello world')");

object module(handle<>(borrowed(PyImport_AddModule("__main__"))));
object dictionary = module.attr("__dict__");
} catch (error_already_set) {
PyErr_Print();
}

Py_Finalize();
return 0;
}

最佳答案

快速浏览一下,您会像这样设置要导出的类:

class_<YourAPI, boost::noncopyable>("YourAPI", no_init)
.def("function", &YourAPI::function);

那你可以传a reference到你的 C++ 实例到 Python,允许你调用它的函数,这反过来又可以触发事件。

关于c++ - 如何使用 boost 和 Firebreath 从 python 中正确触发浏览器事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com