[C++-sig] Wrapping functions that take void* parameters

Nat Goodspeed nat at lindenlab.com
Thu Aug 14 17:56:18 CEST 2008


Geoff wrote:
> I'm trying to wrap a C++ library that is used for serial communications. 
> It has functions such as:
> 
> ssize_t Read (void * const buffer, size_t count);
> 
> and
> 
> ssize_t Write (const void * const buffer, size_t count);
> 
> There are two issues here that I haven't been able to solve:
> 
> 1) How to wrap the void pointers. Ideally, I'd like to be able to do 
> things like pass in a buffer of raw data or a Python string and have it 
> written. Half a day of searching hasn't turned up anything even vaguely 
> related to this more recent than 2003, so I was hoping there may have 
> been some progress since then.
> 
> 2) The Read() method expects buffer to point to some pre-allocated space 
> into which it can put the data that is read from the serial port. I'm 
> not sure how I could wrap this, because just passing in a Python string 
> or something doesn't guarantee that that string has enough space to 
> store the data. My first instinct would be to write a wrapper function 
> that allocates space based on count, then calls Read(), passing in a 
> pointer to that space, but I don't know how I could return that to the 
> Python side.

Isn't this what the Python buffer object is for?
http://www.python.org/doc/2.5.2/lib/typesseq.html
http://docs.python.org/api/bufferObjects.html



More information about the Cplusplus-sig mailing list