AW: AW: [C++-sig] Wrapping opaque pointers returned from API func tion s

Gottfried.Ganssauge at HAUFE.DE Gottfried.Ganssauge at HAUFE.DE
Mon Jan 13 14:55:28 CET 2003



> -----Ursprungliche Nachricht-----
> Von: David Abrahams [mailto:dave at boost-consulting.com]
> Gesendet: Montag, 13. Januar 2003 13:47
> An: c++-sig at python.org
> Betreff: Re: AW: [C++-sig] Wrapping opaque pointers returned from API
> function s
> 
> 
> Gottfried.Ganssauge at HAUFE.DE writes:
> 
> >> I didn't say I tested the code ;-)
> >
> > Of course.
> > Anyway, your upcast is probably invalid anyway, because 
> PyObject ist not a
> > base class of instance.
> 
> No, it is not invalid.  My upcast template is specially designed to
> deal with this.  Please try doing as I suggested.
Here you are:
c:\daten\devel\boost\boost\python\cast.hpp(31) : error C2039: 'type' : Ist
kein Element von 'base_type_traits<struct convert_opaque_pointer<struct
workspace_ *>::instance>'
c:\daten\devel\boost\boost\python\cast.hpp(52) : Siehe Verweis auf
Instantiierung der kompilierten Funktionsvorlage 'struct _object *__cdecl
boost::python::detail::upcast(struct convert_opaque_pointer<struct
workspace_ *>::instance *,int *,int *,struct _object *)'

I had seen that message before and concluded from it, that
convert_opaque_pointer<struct workspace_ *>::instance * can't be converted
to PyObject * using upcast<>.

> 
> >> No, that's non-POD.  It has a base class.  
> >
> > I think I have a solution that is hopefully more conforming 
> but doesn't rely
> > on the Python implementation details (and it even resembles 
> a bit to real
> > inheritance)
> > 	struct instance {
> > 		PyObject base_;
> > 		Pointer x;
> > 	};
> 
> The contents of PyObject are still Python implementation details.
True, but I don't use it. PyObject_HEAD may be the implementation of
PyObject but there is no direct relationship between the two except Guido.
(no criticism; just a fact)

> This one is no better than my proposal, and may in fact take extra
> storage for alignment.
At least it doesn't need a cast :-)
Why should there be extra alignment storage used after PyObject but not
after PyObject_HEAD?

> 
> > 	static PyObject* convert(Pointer x)
> > 	{
> > 		instance *o = PyObject_New (instance, &type_object);
> > 		o->x = x;
> > 		return &o->base_;
> > 	}
> > I can't lookup the paragraph in the standard, but I found 
> something in the
> > draft of "C++ in a nutshell".
> > In Chapter 7 it says:
> > """
> > A POD class can contain padding between data members, but 
> no padding appears
> > before the first member. Therefore, a pointer to the POD 
> object can be
> > converted (with reinterpret_cast<>) to a pointer to the 
> first element.
> > """
> 
> They're wrong about reinterpret_cast<>, as far as the standard goes.
> That performs an implementation-defined mapping.  The only thing you
> can do portably with it is convert a pointer to a different type and
> back again, without using it in between.  Please, just try it the way
> I suggested!
My solution only uses the fact, that there is no extra padding before the
base_ member so that a pointer to base_ may be returned from the convert
method that can be converted back to a pointer to our instance struct.

Cheers,

Gottfried
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20030113/7223fdcc/attachment.htm>


More information about the Cplusplus-sig mailing list