Python 到 C++使用 Boost.Python 进行函数转换

发布于 2024-09-27 20:28:22 字数 1024 浏览 4 评论 0 原文

我有一堆用 C++ 编写的类和 API,并在 Boost.Python 的帮助下暴露给 Python

。我目前正在研究创建以下架构的可能性。
在 python 中:

from boostPythonModule import *
AddFunction( boostPythonObject.Method1, args )
AddFunction( boostPythonObject.Method2, args )
AddFunction( boostPythonObject.Method2, args )
RunAll( ) # running is done by C++

在 C++ 中:

void AddFunction( boost::object method,  boost::object args )
{
    /// 1. Here i need to extract a real pointer to a function
    /// 2. Make argument and type checking for a function under method
    /// 3. Unpack all arguments to native types
    /// 4. Store the pointer to a function somewhere in local storage
}

void RunAll( )
{
    /// 1. run all previously stored functions and arguments for them
}

基本上我试图将所有函数都放到我的程序的本机部分。 问题是我不确定是否可以从 Boost 元信息中提取所有必需的数据以通用方式执行此操作 - 在编译时我不应该知道我要调用哪些函数以及它们接受哪些参数。

几个问题:
1. 有没有我可以访问的共享 Python 信息表来检查其中的一些内容?
2. Boost.Python 进行类型参数检查。可以单独重复使用吗?

让我知道您的想法。

谢谢

I have a bunch of classes and APIs written in C++ and exposed to Python with help of Boost.Python

I am currently investigating the possibilities of creating the following architecture.
In python:

from boostPythonModule import *
AddFunction( boostPythonObject.Method1, args )
AddFunction( boostPythonObject.Method2, args )
AddFunction( boostPythonObject.Method2, args )
RunAll( ) # running is done by C++

In C++:

void AddFunction( boost::object method,  boost::object args )
{
    /// 1. Here i need to extract a real pointer to a function
    /// 2. Make argument and type checking for a function under method
    /// 3. Unpack all arguments to native types
    /// 4. Store the pointer to a function somewhere in local storage
}

void RunAll( )
{
    /// 1. run all previously stored functions and arguments for them
}

Basically I am trying to put all functions down to the native part of my program.
The thing is that I am not sure if it's possible to extract all required data from Boost metainfo to do this in generic way - at compile time I should not know what functions I'm gonna call and what arguments they accept.

Few questions:
1. Is there any shared Python info tables I can access to check for some of this stuff ?
2. Boost.Python does type arguments checking. Can it be reused separately ?

Let me know your thoughts.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-10-04 20:28:22

我会考虑在 python 级别缓存函数及其参数 - 使用 解压保存的参数 在 python 级别完成的解压将使您免受任何 boost 类型安全复杂性的影响(所有类型检查都将在 RunAll 阶段完成,从而使其速度更慢且安全性更低)。

速度优化方法是实现一个具有通用接口的 C++ 类,该接口可以接受支持给定参数的函数调用,并在内部缓存它们的值以供以后运行时使用。

struct Runner {
  virtual int run() = 0;
};

struct ConcreteRunner: public Runner {
  std::string _arg;
  void setArguments(std::string arg) {_arg=arg;}
  virtual int run() {clog << "ConcreteRunner is called with argument" << _arg << endl;}
};

此方法在 RunAll 部分之外处理参数解析,因此使其尽可能快。

I would think about caching functions and their arguments on python level - save the arguments using the latest form from Keyword arguments section of tutorial and call your C++ functions later unpacking saved arguments unpacking done at python level will isolate you from any boost typesafety complications (all typechecking will be done on RunAll stage making it slower and less secure).

Speed optimized approach would be to implement a C++ clasess with a common interface that can accept a function calls supporting given arguments and caching their values internally to use in later run.

struct Runner {
  virtual int run() = 0;
};

struct ConcreteRunner: public Runner {
  std::string _arg;
  void setArguments(std::string arg) {_arg=arg;}
  virtual int run() {clog << "ConcreteRunner is called with argument" << _arg << endl;}
};

This approach handles argument parsing outside of RunAll section therefore making it as fast as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文