[C++-sig] more problems with lvalue_from_pytype

Mark Russell mrussell8081 at pacbell.net
Mon Nov 18 20:44:51 CET 2002


David,

Thanks for your response to my email on lvalue_from_pytype over the weekend.
I am using the changes you made is cvs and have the following questions.  I
am mainly a python programmer and know just enough c++ to get by, so please
excuse these questions if they are very basic.  I am using msvc 6.5 for this
project; you might remember that I am wrapping DirectX8 so far boost.python
is making this work laughably easy.  There is one part I am stuck on.  In
DirectX there are gobs of structs more of which are no problem to wrap in
boost.  A windows handle (HWND) is a data member that appears in a number of
these structs.  I am using Mark Hammods win32ui lib for the windows frame in
python.  This handle is a pointer in c++ and is represented as an int in
python--so I need to convert this.  As it turns out the win32ui class that
creates the window is put together as a python type--ui_type.  My plan is to
register a converter for this type in boost as follows:

struct PyCWnd_to_HWND
{
	static HWND& execute(PyObject& o)
	{
		static HWND w;
		PyObject* pylong = PyObject_CallMethod(&o, "GetSafeHwnd", NULL);
		long hwndlong = PyLong_AsLong(pylong);
		w = reinterpret_cast<HWND>(hwndlong);
		return w;
	}
};

I am having trouble linking to the PyTypeObject and am working with the
example in lvalue_from_pytype to understand how to do this.  I am working
with the following:

#include <boost/python.hpp>

extern PyTypeObject noddy_NoddyType;

typedef struct {
    PyObject_HEAD
} noddy_NoddyObject;

using namespace boost::python;
static handle<> cache;

bool is_cached(noddy_NoddyObject* x)
{
   return x == cache.get();
}

void set_cache(noddy_NoddyObject* x)
{
   cache = handle<>(borrowed(x));
}

BOOST_PYTHON_MODULE(noddy_cache)
{
   // register Noddy lvalue converter

lvalue_from_pytype<extract_identity<noddy_NoddyObject>,&noddy_NoddyType>();

   def("is_cached", is_cached);
   def("set_cache", set_cache);

}

I am statically linking this at the moment and will dynamically link next.
Your example does not declare noddy_NoddyType but I can't get this to
compile so use the extern declaration.  When I compile I get the following
error:

Compiling...
nc.cpp
C:\developer\com-hacking\boost hacking\noddy cache\nc.cpp(14) : error C2446:
'==' : no conversion from 'struct _object *' to 'noddy_NoddyObject *'
        Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
C:\developer\com-hacking\boost hacking\noddy cache\nc.cpp(14) : error C2230:
'==' : indirection to different types
Error executing cl.exe.

noddy cache.dll - 2 error(s), 0 warning(s)

What am I missing??

Many thanks for all your help.  --Mark





More information about the Cplusplus-sig mailing list