我有一堆用 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
发布评论
评论(1)
我会考虑在 python 级别缓存函数及其参数 - 使用 解压保存的参数 在 python 级别完成的解压将使您免受任何 boost 类型安全复杂性的影响(所有类型检查都将在 RunAll 阶段完成,从而使其速度更慢且安全性更低)。
速度优化方法是实现一个具有通用接口的 C++ 类,该接口可以接受支持给定参数的函数调用,并在内部缓存它们的值以供以后运行时使用。
此方法在 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.
This approach handles argument parsing outside of RunAll section therefore making it as fast as possible.