Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <boost/python.hpp>
- using namespace boost::python;
- //I'm assuming your buffer data is allocated from CSomeClass::load()
- //it should return the allocated size in the second argument
- static object csomeclass_load(CSomeClass& self) {
- unsigned char* buffer;
- int size;
- self.load(buffer, size);
- //now you wrap that as buffer
- PyObject* py_buf = PyBuffer_FromReadWriteMemory(buffer, size);
- object retval = object(handle<>(py_buf));
- return retval;
- }
- static int csomeclass_save(CSomeClass& self, object buffer) {
- PyObject* py_buffer = buffer.ptr();
- if (!PyBuffer_Check(py_buffer)) {
- //raise TypeError using standard boost::python mechanisms
- }
- //you can also write checks here for length, verify the
- //buffer is memory-contiguous, etc.
- unsigned char* cxx_buf = (unsigned char*)py_buffer.buf;
- int size = (int)py_buffer.len;
- return self.save(cxx_buf, size);
- }
Advertisement
Add Comment
Please, Sign In to add comment