简体   繁体   English

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

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

I have a bunch of classes and APIs written in C++ and exposed to Python with help of Boost.Python 我有一堆用C ++编写的类和API,并在Boost.Python的帮助下暴露给Python

I am currently investigating the possibilities of creating the following architecture. 我目前正在研究创建以下架构的可能性。
In python: 在python中:

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

In 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
}

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. 问题是我不确定是否有可能从Boost metainfo提取所有必需的数据以通用方式执行此操作-在编译时,我不应该知道要调用的函数以及它们接受的参数。

Few questions: 几个问题:
1. Is there any shared Python info tables I can access to check for some of this stuff ? 1.是否可以访问任何共享的Python信息表来检查其中的某些内容?
2. Boost.Python does type arguments checking. 2. Boost.Python会进行类型参数检查。 Can it be reused separately ? 可以单独重用吗?

Let me know your thoughts. 让我知道你的想法。

Thanks 谢谢

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). 我会考虑在python级别上缓存函数及其参数-使用本教程的关键字参数部分中的最新格式保存参数,然后调用C ++函数,然后在python级别解压缩保存的参数将使您免受任何boost typesafety并发症的困扰(所有类型检查将在RunAll阶段进行,这会使其速度变慢且安全性降低)。

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. 速度优化的方法是使用通用接口实现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;}
};

This approach handles argument parsing outside of RunAll section therefore making it as fast as possible. 这种方法在RunAll部分之外处理参数解析,因此使其尽可能快。

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

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