1//-------------------------------------------------------------------------- 2// Name: src/wxpybuffer.sip 3// Purpose: A MappedType for wxPyBuffer to automatically convert buffer- 4// compatible objects to a C pointer and length. 5// 6// Also MappedTypes for wxMemoryBuffer and wxCharBuffer 7// 8// Author: Robin Dunn 9// 10// Created: 26-Apr-2012 11// Copyright: (c) 2012-2018 by Total Control Software 12// Licence: wxWindows license 13//-------------------------------------------------------------------------- 14 15 16%ModuleHeaderCode 17#include "wxpybuffer.h" 18%End 19 20 21%MappedType wxPyBuffer 22{ 23 %ConvertToTypeCode 24 // Code to test a PyObject for compatibility 25 if (!sipIsErr) { 26 if (PyObject_CheckBuffer(sipPy) // New buffer interface 27 #if PY_MAJOR_VERSION < 3 28 || PyObject_CheckReadBuffer(sipPy) // or old buffer interface 29 #endif 30 ) 31 return TRUE; 32 return FALSE; 33 } 34 35 // Code to create a new wxPyBuffer from the PyObject 36 wxPyBuffer* buf = new wxPyBuffer(); 37 buf->create(sipPy); 38 *sipCppPtr = buf; 39 return sipGetState(sipTransferObj); 40 %End 41 42 43 // This isn't being used anywhere yet, but it should work. 44 %ConvertFromTypeCode 45 return wxPyMakeBuffer(sipCpp->m_ptr, sipCpp->m_len); 46 %End 47}; 48 49 50//-------------------------------------------------------------------------- 51// The wxMemoryBuffer class can be mapped to/from Python buffer objects 52// similarly, and we'll use a wxPyBuffer to help out. 53 54%ModuleHeaderCode 55#include <wx/buffer.h> 56%End 57 58 59%MappedType wxMemoryBuffer 60{ 61 %ConvertToTypeCode 62 // Code to test a PyObject for compatibility 63 if (!sipIsErr) { 64 if (PyObject_CheckBuffer(sipPy)) 65 return TRUE; 66 return FALSE; 67 } 68 69 // Code to create a new wxMemoryBuffer from the PyObject 70 wxPyBuffer pybuf; 71 pybuf.create(sipPy); 72 wxMemoryBuffer* buf = new wxMemoryBuffer(pybuf.m_len); 73 buf->AppendData(pybuf.m_ptr, pybuf.m_len); 74 *sipCppPtr = buf; 75 return sipGetState(sipTransferObj); 76 %End 77 78 79 // Code to convert a wxMemoryBuffer to a Python object 80 %ConvertFromTypeCode 81 return wxPyMakeBuffer(sipCpp->GetData(), sipCpp->GetDataLen()); 82 %End 83}; 84 85//-------------------------------------------------------------------------- 86// wxCharBuffers will be converted to/from bytes objects 87 88%MappedType wxCharBuffer 89{ 90 %ConvertToTypeCode 91 // Code to test a PyObject for compatibility 92 if (!sipIsErr) { 93 if (PyBytes_Check(sipPy)) 94 return TRUE; 95 return FALSE; 96 } 97 98 // Code to create a new wxCharBuffer from the PyObject 99 char* bbuf; 100 Py_ssize_t blen; 101 PyBytes_AsStringAndSize(sipPy, &bbuf, &blen); 102 wxCharBuffer* cbuf = new wxCharBuffer(blen); 103 memcpy(cbuf->data(), bbuf, blen); 104 *sipCppPtr = cbuf; 105 return sipGetState(sipTransferObj); 106 %End 107 108 109 // Code to convert a wxCharBuffer to a Python object 110 %ConvertFromTypeCode 111 return PyBytes_FromStringAndSize(sipCpp->data(), sipCpp->length()); 112 %End 113}; 114 115 116//-------------------------------------------------------------------------- 117