[C++-sig] Help with custom converters
Lloyd Weehuizen
lloyd at fusion.net.nz
Sun Jan 6 12:04:03 CET 2008
Hey Roman
Thanks for the tip, the result converter did the trick. Bit of a shame,
it can't be done directly, but I'll happily use your method rather than
hacking up my boost headers :)
Cheers,
Lloyd
Roman Yakovenko wrote:
> I think that you need to use boost::python::ptr if you want to
> construct object from pointer.
>
> I also found it useful to use "call policies" to construct the object:
>
> namespace bpl = boost::python;
>
> template< typename CallPolicies, class T >
> bpl::object make_object( T x ){
> //constructs object using CallPolicies result_converter
> typedef BOOST_DEDUCED_TYPENAME CallPolicies::result_converter::
> template apply< T >::type result_converter_t;
> result_converter_t rc;
> return bpl::object( bpl::handle<>( rc( x ) ) );
> }
>
> I am not sure whether it will help you or not.
>
> On Jan 5, 2008 2:44 AM, Lloyd Weehuizen <lloyd at fusion.net.nz> wrote:
>> Hi
>>
>> I've done more research into the problem and it appears that when
>> passing an Interface* object into a bp::object's constructor, boost
>> looks up a converter for type Interface, not Interface* (which the
>> converter is registered against).
>>
>> Registering a converter for an abstract interface seems impossible at
>> the moment due to the compile time checks in as_to_python_function which
>> cause the compiler to attempt to instance the class.
>>
>> template <class U>
>> static void convert_function_must_take_value_or_const_reference(U(*)(T),
>> int, T* = 0) {}
>> template <class U>
>>
>> static void convert_function_must_take_value_or_const_reference(U(*)(T
>> const&), long ...) {}
>>
>> Commenting out the above checks and registering the converter directly
>> makes everything work correctly.
>>
>> Is there a cleaner solution to my abstract interface/converter problem?
>>
>> Thanks,
>> Lloyd
>>
>> Lloyd Weehuizen wrote:
>> > Hi
>> >
>> > I'm having a strange problem when using custom converters, they seem
>> > to work fine when I have a function/method that returns an object
>> type > but fails if I try use the object as a boost::python::object.
>> >
>> > The error I get is:
>> >
>> > TypeError: No to_python (by-value) converter found for C++ type: class
>> > Interface
>> >
>> > For example:
>> >
>> > Interface* TestConverter(Interface* interface)
>> > {
>> > return interface;
>> > }
>> >
>> > Works correctly, the python class instance that inherits from
>> > Interface passes correctly through the TestConverter method when
>> > called from python.
>> >
>> > However, if I try this:
>> > Interface* TestConverter(Interface* interface)
>> > {
>> > boost::python::object obj(interface);
>> > return interface;
>> > }
>> >
>> > Then the TypeError is thrown. I would have thought the converter would
>> > be called for this case as well?
>> >
>> > Here's the converter code:
>> >
More information about the Cplusplus-sig
mailing list