[C++-sig] Re: Converting CString to/from std::string
David Abrahams
dave at boost-consulting.com
Thu Sep 25 14:46:38 CEST 2003
Martin Blais <blais at iro.umontreal.ca> writes:
> David Abrahams wrote:
>> Martin Blais <blais at iro.umontreal.ca> writes:
>> It's an interesting interface, the idea of using member function
>> specialization to provide the functionality. The downside is that a
>> failure to implement the member functions shows up only at runtime.
>> If you left out the default implementations, you could get it to fail
>> at link time, but I'd much rather see a solution that fails at compile
>> time. It's easy enough to do.
>
> well, the fact is you have a choice to specialize the method that uses
> PyObject* or the one that uses/returns
> boost::python::object. whichever one you decide to use the other one
> has to be defined (even if not called i think, iirc that's a
> compiler-dependent behaviour, not sure).
I'm not sure which of my points you're addressing, but it's possible
to design an interface that lets you define your to-python converter
as returning either object or PyObject*, and fails at compile-time if
it's not defined.
>> Another minor issue is that your converters force you to register
>> conversions in both directions (not always desired), and only handle
>> rvalue from-python conversions. That may be what most people want,
>> most of the time... though I'm not sure.
>
> i assumed that for that you would use the code you provide. i wonder
> if we could split in two base classes and mixin.
I think that design sounds a little bit too complicated for what it
actually accomplishes.
Maybe I'll work out a small proposal.
template <>
python::object AutoConverter<MyPath>::toObject( MyPath const& s )
{
return python::str( s.cstr() );
}
template <>
void* AutoConverter<MyPath>::convertible( PyObject* obj_ptr )
{
if ( !PyString_Check(obj_ptr) ) return 0;
return obj_ptr;
}
template <>
void AutoConverter<MyPath>::fromPython(
PyObject* obj_ptr,
void* memblock
)
{
const char* value = PyString_AsString( obj_ptr );
if (value == 0) {
python::throw_error_already_set();
}
new ( memblock ) MyPath( value );
}
Then in my initmodule definition I add::
...
AutoConverter<MyPath>();
...
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list