如何正确访问结构值并将它们传递给函数- boost::python
How do I correctly access struct values and pass them to functions- boost::python
我正在努力使用 boost 构建一个 c++ python 包装器。这可能是一个非常基础的问题,但我在现有的各种教程中没有看到答案。
[更新:关于如何访问结构数据成员的第一部分已由 xioxox post 解决。仍然悬而未决的问题是:
如何在结构中编写向量的包装器。]
[原post]
我的 C++ 部分看起来像这样:
struct Globals {
AClass *a;
BClass *b;
int someInt = 5;
bool check = FALSE;
vector<unsigned short> someData
};
void func1(Globals* global)
{
// ...
// a propper inizialisation of the globals a and b Classes
// ...
global->check = global->a->returnSomething1(global->someInt);
if(global->check)
{
global->someData = global->b->returnSomething2();
}
return;
}
BOOST_PYTHON_MODULE(demo)
{
using namespace boost::python;
class_<Globals>("Globals")
.def_readonly("check", &Globals::check)
.def_readwrite("someint", &Globals::someint);
//someData to be done...
def("func1", func1);
}
我想用Python创建一个Globals
对象,在里面设置someInt
然后传给func1
。函数完成后,我想读取 check
值,如果它是真的,结果也会再次通过 python 保存在 someData
中。
当我在 IPython 控制台中导入库时,它会给出以下结果
In [1]: import demo as d
In [2]: T = d.Globals
In [3]: T
Out[3]: demo.Globals
In [4]: T.check
Out[4]: <property at 0xa2c72c8>
In [5]: T.someInt
Out[5]: <property at 0xa2c7278>
In [6]: T.someInt = 5
In [7]: d.func1(T)
Out[7]: <property at 0xa2c7278>
Traceback (most recent call last):
File "<ipython-input-7-dc39ba21a63e>", line 1, in <module>
demo.func1(T)
ArgumentError: Python argument types in demo.func1(Boost.Python.class)
did not match C++ signature:
func1(struct Globals * __ptr64)
当我尝试访问值时,我只看到 C++ 指针。然而,据我所知,该结构已更改为 Boost.Python.class
,我必须从该 class 获取一个指针并将其作为 Globals*
输入传递。我怎么做?
我也想访问 python 中的 Vector 数据 (sapGlobal->images.data()
),但我不知道如何为它实现包装器。
如有任何帮助,我们将不胜感激。
[更新 2] 可以通过包含 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
来包装向量
并将以下行添加到 BOOST_PYTHON_MODULE
class_<vector<unsigned short>>("img")
.def(vector_indexing_suite<vector<unsigned short>>());
如描述here
问题是 T 实际上是您声明的 class,而不是 class 的实例。你需要说
T = d.Globals()
而不是
T = d.Globals
我正在努力使用 boost 构建一个 c++ python 包装器。这可能是一个非常基础的问题,但我在现有的各种教程中没有看到答案。
[更新:关于如何访问结构数据成员的第一部分已由 xioxox post 解决。仍然悬而未决的问题是: 如何在结构中编写向量的包装器。]
[原post] 我的 C++ 部分看起来像这样:
struct Globals {
AClass *a;
BClass *b;
int someInt = 5;
bool check = FALSE;
vector<unsigned short> someData
};
void func1(Globals* global)
{
// ...
// a propper inizialisation of the globals a and b Classes
// ...
global->check = global->a->returnSomething1(global->someInt);
if(global->check)
{
global->someData = global->b->returnSomething2();
}
return;
}
BOOST_PYTHON_MODULE(demo)
{
using namespace boost::python;
class_<Globals>("Globals")
.def_readonly("check", &Globals::check)
.def_readwrite("someint", &Globals::someint);
//someData to be done...
def("func1", func1);
}
我想用Python创建一个Globals
对象,在里面设置someInt
然后传给func1
。函数完成后,我想读取 check
值,如果它是真的,结果也会再次通过 python 保存在 someData
中。
当我在 IPython 控制台中导入库时,它会给出以下结果
In [1]: import demo as d
In [2]: T = d.Globals
In [3]: T
Out[3]: demo.Globals
In [4]: T.check
Out[4]: <property at 0xa2c72c8>
In [5]: T.someInt
Out[5]: <property at 0xa2c7278>
In [6]: T.someInt = 5
In [7]: d.func1(T)
Out[7]: <property at 0xa2c7278>
Traceback (most recent call last):
File "<ipython-input-7-dc39ba21a63e>", line 1, in <module>
demo.func1(T)
ArgumentError: Python argument types in demo.func1(Boost.Python.class)
did not match C++ signature:
func1(struct Globals * __ptr64)
当我尝试访问值时,我只看到 C++ 指针。然而,据我所知,该结构已更改为 Boost.Python.class
,我必须从该 class 获取一个指针并将其作为 Globals*
输入传递。我怎么做?
我也想访问 python 中的 Vector 数据 (sapGlobal->images.data()
),但我不知道如何为它实现包装器。
如有任何帮助,我们将不胜感激。
[更新 2] 可以通过包含 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
来包装向量
并将以下行添加到 BOOST_PYTHON_MODULE
class_<vector<unsigned short>>("img")
.def(vector_indexing_suite<vector<unsigned short>>());
如描述here
问题是 T 实际上是您声明的 class,而不是 class 的实例。你需要说
T = d.Globals()
而不是
T = d.Globals