[C++-sig] Newbie Question about wrapping a function with int *, int & , etc...
Abhi
abhi at qualcomm.com
Fri Mar 17 23:46:14 CET 2006
Dear Roman,
Thanks for the wrapper code. It works great.
> This was easy, right? So why "no"? Because there is no code generator that
> supports this feature. I plan to add to pyplusplus such feature. You are
> welcome to contribute: test cases or code.
We are looking at py++. I will keep you posted.
- Abhi
--On Friday, March 10, 2006 8:44 PM +0400 Roman Yakovenko
<roman.yakovenko at gmail.com> wrote:
> On 3/10/06, Abhi <abhi at qualcomm.com> wrote:
>> I need to wrap the following class in python
>>
>> someclass.h
>>
>> class BasicTypesTest
>> {
>> // Pointer, Reference and const types
>>
>> // int* argument, int return
>> int ptrIntArg_intRet(int* x);
>>
>> // int& argument, int return
>> int refIntArg_intRet(int& x);
>>
>> // const int argument, int return
>> int intConstArg_intRet(const int x);
>>
>> // const int* argument, int return
>> int ptrIntConstArg_intRet(const int* x);
>>
>> // const int& argument, int return
>> int refIntConstArg_intRet(const int& x);
>>
>> };
>>
>>
>> myboostpythonwrapper.cpp
>>
>> BOOST_PYTHON_MODULE (BoostPythonTest)
>> {
>>
>> class_<BasicTypesTest>("BasicTypesTest")
>> .def( init<>())
>>
>> .def( "ptrIntArg_intRet", &BasicTypesTest::ptrIntArg_intRet )
>> .def( "refIntArg_intRet", &BasicTypesTest::refIntArg_intRet )
>> .def( "intConstArg_intRet", &BasicTypesTest::intConstArg_intRet )
>> .def( "ptrIntConstArg_intRet",
>> &BasicTypesTest::ptrIntConstArg_intRet ) .def(
>> "refIntConstArg_intRet", &BasicTypesTest::refIntConstArg_intRet ) ;
>> }
>>
>>
>> This does not work.
>> For example if I try to do:
>>
>> import BoostPythonTest
>>
>> # Get the class
>> b = BoostPythonTest.BasicTypesTest()
>>
>> x = 5;
>> y = 5;
>>
>> # Call methods
>> print "Calling C++ ptrIntArg_intRet"
>> print "Argument x =", x
>> b.ptrIntArg_intRet(x) <------------Error
>> print "Argument x =", x
>> print "---"
>>
>>
>> Similarly for other methods.
>>
>> Is there an easy way to do this?
>
> Yes and no. Yes, because it very easy to write wrapper for such functions.
> Something like this:
>
> boost::python::tuple refIntArg_intRet(BasicTypesTest& self, int x){
> int x_ref = x;
> int result = self.refIntArg_intRet( ref_x );
> return boost::python::tuple( result, x );
> }
>
> def( "refIntArg_intRet", &::refIntArg_intRet )
> So, as you can see you can expose such functions.
>
> This was easy, right? So why "no"? Because there is no code generator that
> supports this feature. I plan to add to pyplusplus such feature. You are
> welcome to contribute: test cases or code.
>
>> thanks
>> - Abhi
>
> Hope, I helped.
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list