[C++-sig] Exposing c++ classes with static arrays using Boost.Python and py++
Abhi
abhi at qualcomm.com
Thu May 4 02:51:43 CEST 2006
While running py++ on a class with a static array inside it, py++ generates
Boost.Python bindings using a derived class for the original C++ class.
Let me illustrate with the help of the following example:
class AttributesTest
{
public:
int staticIntArray[10];
};
Notice this class has an array inside it
Running py++ generates the following code, with a wrapper class that
derives from the AttributesTest class.
struct AttributesTest_wrapper : AttributesTest, bp::wrapper< AttributesTest
> {
AttributesTest_wrapper(AttributesTest const & arg )
: AttributesTest( arg )
, bp::wrapper< AttributesTest >()
{}
AttributesTest_wrapper( )
: AttributesTest( )
, bp::wrapper< AttributesTest >()
{}
pyplusplus::containers::static_sized::array_1_t< int, 10 >
pyplusplus_staticIntArray_wrapper(){
return pyplusplus::containers::static_sized::array_1_t< int, 10 >(
staticIntArray );
}
};
and
BOOST_PYTHON_MODULE(PYPP_YodaPacketCommon){
if( true ){
typedef bp::class_< AttributesTest_wrapper >
AttributesTest_exposer_t;
AttributesTest_exposer_t AttributesTest_exposer =
AttributesTest_exposer_t( "AttributesTest" );
bp::scope AttributesTest_scope( AttributesTest_exposer );
AttributesTest_exposer.def( bp::init<
>()[bp::default_call_policies()] );
pyplusplus::containers::static_sized::register_array_1< int, 10,
bp::default_call_policies >( "__array_1_int_10" );
AttributesTest_exposer.add_property( "staticIntArray"
, bp::make_function(
(pyplusplus::containers::static_sized::array_1_t< int, 10 > (
AttributesTest_wrapper::* )( )
)(&AttributesTest_wrapper::pyplusplus_staticIntArray_wrapper)
, bp::with_custodian_and_ward_postcall< 0, 1,
bp::default_call_policies >() ) );;
Note that it maps the python name "AttributesTest" to the derived class
AttributesTest_wrapper.
Why not just keep it simple and rather than creating a derived class just
map the variable "staticIntArray" via a global method, such as:
.add_property( "staticIntArray"
, bp::make_function( (&::someglobal_staticIntArray_wrapper)
, bp::with_custodian_and_ward_postcall< 0, 1,
bp::default_call_policies >() ) );;
and define the someglobal_staticIntArray_wrapper method as:
pyplusplus::containers::static_sized::array_1_t< int, 10 >
someglobal_staticIntArray_wrapper(){
return pyplusplus::containers::static_sized::array_1_t< int, 10 >(
staticIntArray );
}
Is there any drawback with just mapping the array variable to a global
method rather than mapping the whole class.
thanks
- Abhi
More information about the Cplusplus-sig
mailing list