[C++-sig] problems with lvalue_from_pytype

Mark Russell mrussell8081 at pacbell.net
Sat Nov 23 00:59:44 CET 2002


Hi David thanks for your reply.  In your response you said:

... as near as I can tell, you want Noddy objects to be converted
automatically to HWND for assignment to the magicnum member of magicSt.
Well, the problem is that HWND is a pointer type, and when converting a
Python object to a pointer type, Boost.Python actually attempts to find an
object of the pointee type. So if you want to convert to HWND, your
converter's execute function must return a reference to the type that HWND
points to (remove_pointer<HWND>::type& if your compiler supported partial
specialization, which it doesn't).

This is indeed what I am trying to do.  In fact the full situation is this.
I am working on a project that involves wrapping parts of DirectX8.  So far
I am having great success with BPL in getting this done--indeed I would not
attempt this myself if I didn't have BPL to work with so great lib!  Many of
the structures in DirectX have a handle to the target window as a member.  I
am using Mark Hammond's win32ui for my windows frame and can get a
representation of the window handle as an int in python.  My plan is to wrap
this integer value in an hwnd object and pass this object to hwnd data
members in DirectX.  I need the bpl wrapper to convert the integer to an
HWND.  I there any way to do this without the partial specialization you
mentioned?  Thanks again for your help.  --Mark



-----Original Message-----
From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org]On
Behalf Of David Abrahams
Sent: Friday, November 22, 2002 2:51 PM
To: c++-sig at python.org
Subject: Re: [C++-sig] problems with lvalue_from_pytype


Mark Russell <mrussell8081 at pacbell.net> writes:

> I have modified the noddy example from the python docs to store a magic
> number.  I am trying to build a wrapper that will convert the magic number
> of the noddy object to a win32 hwnd and back.  So from python I want to
> following behavior:
>
> magicSt is a structure that has an HWND member magicnum
>
>>> import noddy, cn

What's cn?

>>> n = noddy.noddy()
>>> m = cn.magicSt()
>>> m.magicnum = n

You're sticking your noddy object in the magicnum attribute...

>>> print m.magicnum
> 453311

You could accomplish this by defining a __str__ method for your Noddy
objects.

> In c++ magicnum would be an HWND.  The following compiles and I can
> get the magicnum but when I try to set it
-----------------------^^^^^^^^^^^^^^^^^^^^
What does this mean?Please be very specific when asking for help.

Hmm, I decipher from the code below that you may mean

  >>> m.magicnum = n

> I get:: TypeError: bad agrument type fro built-in operation.  I have
> a version of this that just converts the magic number to long and
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How?

> this works just fine.

But you still want something else?

> What am I missing??

I'm not sure I understand your problem, but as near as I can tell, you
want Noddy objects to be converted automatically to HWND for
assignment to the magicnum member of magicSt. Well, the problem is
that HWND is a pointer type, and when converting a Python object to a
pointer type, Boost.Python actually attempts to find an object of the
pointee type. So if you want to convert to HWND, your converter's
execute function must return a reference to the type that HWND points
to (remove_pointer<HWND>::type& if your compiler supported partial
specialization, which it doesn't).

>
> struct convertNoddy {
> 	static HWND& execute(PyObject& o) {
> 		static long magic = call_method<long>(&o, "getMagicNum");
> 		static HWND hmagic = reinterpret_cast<HWND>(magic);
> 		return hmagic;
> 	}
> };
>
> BOOST_PYTHON_MODULE(cn) {
> 	lvalue_from_pytype<convertNoddy, &noddy_NoddyType>();


HTH,
Dave

--
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution


_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list