[C++-sig] Pyplusplus: Removing generation of bp::implicitly_convertible code?

Roman Yakovenko roman.yakovenko at gmail.com
Tue Aug 1 08:37:02 CEST 2006


On 8/1/06, Haridev, Meghana <mharidev at qualcomm.com> wrote:
> I'm using pyplusplus to generate boost.python wrappers for the following
> piece of code:
>
> //test.h
> class Y
> {  };
> class X
> {
>     public:
>     X(const Y *yptr = 0){  }
> };
> Pyplusplus generates the corresponding boost.python code for it:
>
> // This file has been generated by pyplusplus.
> #include "boost/python.hpp"
> #include "test.h"
> namespace bp = boost::python;
> BOOST_PYTHON_MODULE(TEST){
>     bp::class_< Y >( "Y" );
>     bp::class_< X >( "X", bp::init< bp::optional< Y const * > >((
> bp::arg("yptr")=bp::object() )) );
>     bp::implicitly_convertible< Y const *, X >();
> }
> Two Questions:
>
> ----------------------
>
> Question 1: How can I make pyplusplus to NOT generate the implicitly
> convertible boost code? I do not want to take advantage of the conversion
> facility. Basically, I want the pyplusplus generated code to look like this:

mb = module_builder_t( ... )
mb.build_code_creator( ..., create_castinig_constructor=False, ... )

>
> Question 2: Is there any way I can expose only the default no-arg
> constructor for class X i.e X( ) and not expose the constructor X(const Y
> *yptr = 0)? That is, expose class X as: bp::class_< X >( "X", bp::init< >( )
> ). That would automatically take care of not generating the
> bp::implicitly_convertible code also. Generated code should look like this:
>
> BOOST_PYTHON_MODULE(TEST){
>
>     bp::class_< Y >( "Y" );
>
>     bp::class_< X >( "X", bp::init< >( ) ); //Expose just default
> constructor
>
>    //bp::implicitly_convertible< Y const *, X >(); - Will not be generated
>
> }
>
> I also tried excluding constructors for class X, but it excludes all the
> constructors. I want to be able to select and just exclude the X(const Y
> *yptr = 0) constructor. Is it possible to selectively exclude constructors
> from being exposed to C++ using pyplusplus?

Class X does not have empty constructor. I assume that it has.

Before you read my explanation, please read this tutorials:
http://language-binding.net/pygccxml/query_interface.html

from pygccxml import declarations

mb = module_builder_t( ... )

def criteria( constructor ):
    if len( constructor.arguments ) != 1:
        return False
    arg0_type = constructor.arguments[0].type
    if not declarations.is_pointer( arg0_type ):
        return
    tmp = declarations.remove_pointer( arg0_type ):
    if not declarations.is_const( tmp ):
         return False
    return declarations.is_same( declarations.remove_const( tmp ),
mb.class_( "Y" ) )

X = mb.class_( "X" )
constructor_from_Y = X.constructor( criteria )

Ther is another approach.
constructor_from_Y = X.constructor( args_type=[ "Y const *" ] )
-----------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This query will return constructor, that takes 1 argument "Y const *".
The problem
with this approach is that this string should be equal to arg0.type.decl_string.

And third approach it to select all constructors that take 1 argument:

cs = X.constructors( [None] )


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



More information about the Cplusplus-sig mailing list