Re: [pygccxml-development] py++ and make_contructor
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2008-08-13 20:01:41
|
On Wed, Aug 13, 2008 at 6:45 PM, Damien Fagnou <dam...@mo...> wrote: > Hi , > > so I have class with create function that act a factory function . > > typedef boost::smart_ptr<Color> ColorPtr ; > > class Color > { > static ColorPtr create(); > static ColorPtr create(int i); > > private: > Color(); > } > > make sense for the c++ side by in python I want to hide that > so I can do > > col = Color() # this is actually calling Color.create() .. > > // this code that py++ create > { //::Color::create > typedef ::ColorPtr ( *create_function_type )( ); > > Color_exposer.def( > "create" > , create_function_type( &::Color::create ) > , "Create a color 0 0 0 " ); > } > > I found that I can use make_contructor easily and do : > { > typedef ::ColorPtr ( *create_function_type )( ); > Color_exposer.def ("__init__", > bp::make_constructor(create_function_type(&::Color::create) ) ); > } > > so is there a nice way for ne to generate this code for any class that > have > static boost::smart_ptr<Class> create(....) methods ? > > looking onto the source I saw : > > class constructor_transformed_t <cid:par...@mo...>( calldef_t ): > > that doesn something that might be in the right direction for me . > > I can see how I would find all the methods that match this style . > and doing all the typed def and thing but it look like I might be able to do > > md.classes().decls('create').make_contructors() :) > > well not that simple but something in that direction ? Why not? I just committed the implementation of the functionality you want. http://pygccxml.svn.sourceforge.net/viewvc/pygccxml?view=rev&revision=1392 The usage is pretty simple: namespace mc{ struct numbers_t{ numbers_t() : x( 0 ) , y( 0 ) {} ~numbers_t(){} static std::auto_ptr<numbers_t> create( int i, int j, int k, int m ); int x; int y; }; std::auto_ptr<numbers_t> create( int i, int j); } Usage: mc = mb.namespace( 'mc' ) numbers = mc.class_( 'numbers_t' ) numbers.add_fake_constructors( mc.calldefs( 'create' ) ) and I attached the generated code. Obviously this functionality may contain few bugs here and there, but main success scenario should work. > the more I get into py++ the more I like it !! :-), thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |