繁体   English   中英

使用Boost.Python将Python转换为C ++函数

[英]Python to C++ function conversion using Boost.Python

我有一堆用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 metainfo提取所有必需的数据以通用方式执行此操作-在编译时,我不应该知道要调用的函数以及它们接受的参数。

几个问题:
1.是否可以访问任何共享的Python信息表来检查其中的某些内容?
2. Boost.Python会进行类型参数检查。 可以单独重用吗?

让我知道你的想法。

谢谢

我会考虑在python级别上缓存函数及其参数-使用本教程的关键字参数部分中的最新格式保存参数,然后调用C ++函数,然后在python级别解压缩保存的参数将使您免受任何boost typesafety并发症的困扰(所有类型检查将在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部分之外处理参数解析,因此使其尽可能快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM