简体   繁体   中英

boost:python passing an pointer to pointer as parameter

First, i'm not a python programmer, so excuse my silly mistakes.

In C++ I have this public method from MyClass that creates a image dynamically and returns its size.

int MyClass::getImg(uchar *uimg[])
{
    int size = variable_size;
    *_uimg = new uchar[size];
    memcpy(*_uimg, imageOrigin->data(), size);
    uimg = _uimg;
    return size;
}

And the boost:python:

BOOST_PYTHON_MODULE(mymodule)
{
    class_<MyClass>("MyClass")
        .def("getImg", &MyClass::getImg)
    ;
}

and when i try to use it in python:

def getImg():
    img = c_char_p()
    size = mymodule.MyClass.getImg(byref(img))

I'm getting this error:

Boost.Python.ArgumentError: Python argument types in
    MyClass.getImg(MyClass, CArgObject, CArgObject)
did not match C++ signature:
    getImg(class MyClass {lvalue}, unsigned char * *)

In python I also tried declaring

img = POINTER(c_ubyte)()

but that did'nt help either.

I googled it around for hours and i didn't came up with any good solution.

I just need access to that image in python, how can i get this done?

Revised and working version, exposing the data as a python list.

I am not sure how to implement this, but there may be a better way of implementing it, if you can choose your python interface freely. I think creating a list may be a better way to expose it. What do you expect from the return value? Maybe you can also wrap it asa numpy array for better performance (there are also some questions on stackoverflow if you search for boost python and numphy)

class TestPtr
{
public:
  int getImg(uchar **uimg)
  {
     int size = 9045;
     uchar *_uimg = new uchar[size];
     for( int i = 0; i < size; ++i )
       _uimg[i] = i;  

    *uimg = _uimg;
     return size;
 }
};

boost::python::list getImgList( TestPtr &p )
{
  uchar *uimg;
  int rv = p.getImg(&uimg);
  boost::python::list l;
  for ( int i = 0; i < rv; ++i)
    l.append( uimg[i] );
  return l;
}

BOOST_PYTHON_MODULE(mymodule)
{
  class_<TestPtr>("TestPtr")
    .def("getImg",&getImgList)
    ;
}

Another hint, in your question, you have used the following line to call the method:

 mymodule.MyClass.getImg()

This will make a class call (more or less equivalent to a static call) to your object. This is a mor ecorrect way to do it:

img = mymodule.MyClass()
data = img.getImg()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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