[C++-sig] How to use a C++ function with pointer/length arguments in Python

Roman Yakovenko roman.yakovenko at gmail.com
Wed Jan 16 10:24:38 CET 2008


2008/1/16 Huang You Gong <huangyougong at gmail.com>:
>
> Hi,
>
> in c++ there are codes like this:
>
> struct x
> {
>     int a;
>     short b;
>     short c;
> };
> struct y
> {
>     byte a;
>     byte b;
>     byte c;
> };
> void foo( byte * data, int len ){ //... }
>
> in boost_python_module, I defined it as:
>
> class_< x >( "x" )
>     .def_readwrite( "a", &x::a )
>     .def_readwrite( "b", &x::b )
>     .def_readwrite( "c", &x::c )
> .def( "foo", foo )
>
> then in python, how to write code to send x or y object to foo? in c++ way,
> it should be like this:
>
> x X;
> foo( &X, sizeof( x ) );
> y Y;
> foo( &Y, sizeof( y ) );

I suggest you to create another function, that wraps "foo". Take a
look on http://language-binding.net/pyplusplus/documentation/functions/transformation/built_in/input_c_buffer.html
and http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/pyplusplus/code_repository/convenience.py?revision=1032&view=markup

HTH

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list