Wrapping types with boost.python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping types with boost.python

Robert Parcus
Thank you Stephan, a few lines of your code have triggered a lot of sparks in my brain. 
>template <typename T>
>void define_vec2(char const *name)
>{
>   typedef tvec2<T> vec2_type;
>   class_<vec2_type> vec2(name);
>   float (vec2_type::*swizzle_1)(comp) = &vec2_type::swizzle;
>   vec2.def("swizzle", swizzle_1);
>   //...
>}
>
>and then call that function for all the value-types you want:
>
>   define_vec2<float>("fvec2");
>   define_vec2<int>("ivec2");
>   //...
I see that the template function approach is very useful for the number of value-types I have to work with (you guessed right), but I still have problems...

1) on the python interpreter, i have:
>>> fvec2
< class '_ext.fvec2' >
>>> int
< type 'int' >

Is there a standard way to make fvec2 become a real "type" in python, just like int?

2) I still can't do this from python 
>>> a = fvec2 (4.0,2.0) 

I realise that what I may be missing is .def("__init__", ... ) somewhere, but I don't know where, and how to initialize it properly. (

Also, I had to exclude the "swizzle" part(last 2 lines of the first section), because otherwise the program would no compile, but that's not the important part right?

Thanks again,
Robert

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrapping types with boost.python

Stefan Seefeld-2
On 2011-07-05 11:57, Robert Parcus wrote:

>
> I see that the template function approach is very useful for the
> number of value-types I have to work with (you guessed right), but I
> still have problems...
>
> 1) on the python interpreter, i have:
> >>> fvec2
> < class '_ext.fvec2' >
> >>> int
> < type 'int' >
>
> Is there a standard way to make fvec2 become a real "type" in python,
> just like int?

What is fvec2 missing that makes you think it's not a "real type" ?

>
> 2) I still can't do this from python
> >>> a = fvec2 (4.0,2.0)
>
> I realise that what I may be missing is .def("__init__", ... )
> somewhere, but I don't know where, and how to initialize it properly. (

That's right, you need to add appropriate constructors. I'm again not
sure what specific issues you are facing, and what exactly doesn't work,
making it hard to help. Have you followed the tutorial
(http://www.boost.org/doc/libs/1_46_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors)
?



>
> Also, I had to exclude the "swizzle" part(last 2 lines of the first
> section), because otherwise the program would no compile, but that's
> not the important part right?

That was just meant to illustrate how to add member functions / methods
to a class.
I admit I didn't test the code I typed, so there may be typos in it.
Sorry for that.

     Stefan



--

       ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig