Guest User

C++ to Python

a guest
Oct 15th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <boost/python.hpp>
  2.  
  3. using namespace boost::python;
  4.  
  5. //I'm assuming your buffer data is allocated from CSomeClass::load()
  6. //it should return the allocated size in the second argument
  7. static object csomeclass_load(CSomeClass& self) {
  8.   unsigned char* buffer;
  9.   int size;
  10.   self.load(buffer, size);
  11.  
  12.   //now you wrap that as buffer
  13.   PyObject* py_buf = PyBuffer_FromReadWriteMemory(buffer, size);
  14.   object retval = object(handle<>(py_buf));
  15.   return retval;
  16. }
  17.  
  18. static int csomeclass_save(CSomeClass& self, object buffer) {
  19.   PyObject* py_buffer = buffer.ptr();
  20.   if (!PyBuffer_Check(py_buffer)) {
  21.     //raise TypeError using standard boost::python mechanisms
  22.   }
  23.  
  24.   //you can also write checks here for length, verify the
  25.   //buffer is memory-contiguous, etc.
  26.   unsigned char* cxx_buf = (unsigned char*)py_buffer.buf;
  27.   int size = (int)py_buffer.len;
  28.   return self.save(cxx_buf, size);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment