繁体   English   中英

使用Boost.Python公开带有无符号char和参数的方法

[英]exposing method with unsigned char & argument using Boost.Python

我已经关闭了C ++库,该库提供的头文件的代码等效于:

class CSomething
{
  public:
      void getParams( unsigned char & u8OutParamOne, 
                      unsigned char & u8OutParamTwo ) const;
  private:
      unsigned char u8OutParamOne_,
      unsigned char u8OutParamTwo_,
};

我试图将其公开给Python,我的包装器代码如下所示:

BOOST_PYTHON_MODULE(MySomething)
{
    class_<CSomething>("CSomething", init<>())
        .def("getParams", &CSomething::getParams,(args("one", "two")))

}

现在,我正在尝试在Python中使用它,但失败的非常严重:

one, two = 0, 0
CSomething.getParams(one, two)

结果是:

ArgumentError: Python argument types in
    CSomething.getParams(CSomething, int, int)
did not match C++ signature:
    getParams(CSomething {lvalue}, unsigned char {lvalue} one, unsigned char {lvalue} two)

我需要在Boost.Python包装器代码或Python代码中进行哪些更改才能使其正常工作? 如何添加一些Boost.Python魔术来自动将PyIntunsigned char ,反之亦然?

Boost.Python抱怨缺少lvalue参数,这个概念在Python中不存在:

def f(x):
  x = 1

y = 2
f(y)
print(y) # Prints 2

f函数的x参数不是类似C ++的引用。 在C ++中,输出是不同的:

void f(int &x) {
  x = 1;
}

void main() {
  int y = 2;
  f(y);
  cout << y << endl; // Prints 1.
}

您可以在这里选择:

a)包装CSomething.getParams函数以返回新参数值的元组:

one, two = 0, 0
one, two = CSomething.getParams(one, two)
print(one, two)

b)包装CSomething.getParams函数以接受类实例作为参数:

class GPParameter:
  def __init__(self, one, two):
    self.one = one
    self.two = two

p = GPParameter(0, 0)
CSomething.getParams(p)
print(p.one, p.two)

暂无
暂无

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

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