[C++-sig] Pyplusplus - anonymous union
Roman Yakovenko
roman.yakovenko at gmail.com
Wed Aug 2 19:26:55 CEST 2006
On 8/2/06, Jan Langer <jan at langernetz.de> wrote:
> Roman Yakovenko wrote:
> >>I'm using pyplusplus to generate python-bindings for a C API.
> >
> > mb = module_builder_t( ... )
> > #The code in subversion does it for you
> > mb.classes( name='' ).exclude()
> >
> > Next code is copied from Python-OGRE bindings:
> >
> > [...]
> >
> > This should work.
> >
> > P.S. You project is 3rd project that need this feature, may be this is a time
> > to implement it :-)
>
> Thank you. Actually this behavior does not work like I want, but gave me
> more insight into the workings of pyplusplus. I now use a different
> strategy. First, I generate a union wrapper to give the anonymous union
> a name. Then, I provide getter and setter functions for each union
> member and getter/setter for the union itself. The access to the union
> members and the union is then accomplished by the .add_property calls. I
> paste the relevant boost.python code below. However, I omit the
> pyplusplus code, but in case someone is interested I can send it, too.
>
> Jan
>
> //-------------------------------------------------
> // library code
>
> typedef struct
> {
> IntT offset;
> IntT dataType;
> union {
> StringT sdata;
> IntT idata;
> } u;
> } F_TextItemT;
>
> // boost.python code
>
> union F_TextItemT_union
> {
> StringT sdata;
> IntT idata;
> };
>
> StringT get_F_TextItemT_union_sdata (union_wrapper <F_TextItemT_union>
> const &t)
> {
> return t.u.sdata;
> }
> void set_F_TextItemT_union_sdata (union_wrapper <F_TextItemT_union> &t,
> StringT const &v)
> {
> t.u.sdata = v;
> }
>
> IntT get_F_TextItemT_union_idata (union_wrapper <F_TextItemT_union>
> const &t)
> {
> return t.u.idata;
> }
> void set_F_TextItemT_union_idata (union_wrapper <F_TextItemT_union> &t,
> IntT const &v)
> {
> t.u.idata = v;
> }
>
> union_wrapper <F_TextItemT_union> get_F_TextItemT_u (F_TextItemT const &t)
> {
> union_wrapper <F_TextItemT_union> uw;
> assert(sizeof(uw.u) == sizeof(uw.u));
> std::memcpy(&uw.u,&t.u,sizeof(uw.u));
> return uw;
> }
> void set_F_TextItemT_u (F_TextItemT &t, union_wrapper
> <F_TextItemT_union> const &uw)
> {
> assert(sizeof(uw.u) == sizeof(t.u));
> std::memcpy(&t.u,&uw.u,sizeof(uw.u));
> }
>
> BOOST_PYTHON_MODULE(pyfdk){
> if( true ){
> typedef bp::class_< F_TextItemT > F_TextItemT_exposer_t;
> F_TextItemT_exposer_t F_TextItemT_exposer =
> F_TextItemT_exposer_t( "F_TextItemT" );
> bp::scope F_TextItemT_scope( F_TextItemT_exposer );
> bp::class_ <union_wrapper <F_TextItemT_union > >
> ("F_TextItemT_union")
> .add_property ("sdata",
> bp::make_function
> (&get_F_TextItemT_union_sdata,bp::return_value_policy
> <bp::return_by_value> ()),
> &set_F_TextItemT_union_sdata)
> .add_property ("idata",
> bp::make_function
> (&get_F_TextItemT_union_idata,bp::return_value_policy
> <bp::return_by_value> ()),
> &set_F_TextItemT_union_idata)
> ;
> F_TextItemT_exposer.def_readwrite( "offset", &F_TextItemT::offset );
> F_TextItemT_exposer.def_readwrite( "dataType",
> &F_TextItemT::dataType );
> F_TextItemT_exposer.add_property("u",
> &get_F_TextItemT_u,
> &set_F_TextItemT_u);
> }
> }
>
Cool! Can you post pyplusplus code? I will create a FAQ entry.
Thank you
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list