No subject


Tue Oct 14 00:29:20 CEST 2008


snip...

One way is just to find out what the real type of the pointee is on your
own, by
examining the header file.

An HWND is a pointer to an HWND__ struct defined as follows:

struct HWND__ { int unused; };
typedef struct HWND__* HWND

I have attached three files noddy.cpp cn.cpp, and cnl.cpp-- cnl contains my
conversion for noddy objects just using longs cn does the same thing using
HWND.  The following python session shows how it works for longs:

C:\developer\com-hacking\boost hacking\convert noddy long>python
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import noddy,cnl
>>> n=noddy.noddy()
>>> m=cnl.magic()
>>> m.magicnum
1957
>>> m.magicnum=n
>>> m.magicnum
453311
>>>

cnl (convert noddy long) contains a struct called magic with a single data
member--magicnum.  The struct initializes magicnum to 1957.  Noddy's all
have an int value attribute 453311.  I assign a noddy to m.magicnum
(setattr) and my converter correctly retrieves values and assigns it to the
magicnum data member.  The problem is indeed occurring when I try to do the
same thing using pointers (my attempt is in cn.cpp).  I just want to pass an
int from python to my converter--have the converter cast the int to an HWND
(pointer).  How does knowing the real type help me in this situation?
Thanks --Mark




--Boundary_(ID_xRd++t/Mn4vDAnuEgMc4Ug)
Content-type: application/octet-stream; name=cnl.cpp
Content-transfer-encoding: 7bit
Content-disposition: attachment; filename=cnl.cpp

#include <boost/python.hpp>
using namespace std;
using namespace boost::python;

//declarations
extern "C" _declspec(dllimport) PyTypeObject noddy_NoddyType;

typedef struct
{
	PyObject_HEAD
	long magicNum;
} noddy_NoddyObject;

//magic struct
typedef struct _magic {
	_magic() : magicnum(1957) {}
	long magicnum;
} magic;
//noddy object converter
struct convertNoddy {
	static long& execute(PyObject& o) {
		static long magic = call_method<long>(&o, "getMagicNum");
		return  magic;
	}
};

BOOST_PYTHON_MODULE(cnl) {
	class_<magic>("magic")
		.def_readwrite("magicnum", &magic::magicnum);
	lvalue_from_pytype<convertNoddy, &noddy_NoddyType>();
}

--Boundary_(ID_xRd++t/Mn4vDAnuEgMc4Ug)
Content-type: application/octet-stream; name=noddy.cpp
Content-transfer-encoding: 7bit
Content-disposition: attachment; filename=noddy.cpp

#include <Python.h>

extern "C" _declspec(dllexport) PyTypeObject noddy_NoddyType;

typedef struct {
    PyObject_HEAD
	long magicNum;
} noddy_NoddyObject;

static PyObject*
noddy_new_noddy(PyObject* self, PyObject* args) {

    noddy_NoddyObject* noddy;

    if (!PyArg_ParseTuple(args,":new_noddy")) 
        return NULL;

    noddy = PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
	noddy->magicNum = 453311;

    return (PyObject*)noddy;
}

#define MAGICNUM(v) (((noddy_NoddyObject *)(v))->magicNum)

static PyObject*
getMagicNum(PyObject* self, PyObject* args) {
     return Py_BuildValue("i", MAGICNUM(self));
}

static void
noddy_noddy_dealloc(PyObject* self) {
    PyObject_Del(self);
}

static PyMethodDef noddy_methods[] = {
    {"noddy", noddy_new_noddy, METH_VARARGS,
     "Create a new Noddy object."},
    {"getMagicNum", getMagicNum, METH_VARARGS,
     "get magic number."},
//    {"setMagicNum", setMagicNum, METH_VARARGS,
//     "set magic number."},
    {NULL, NULL, 0, NULL}
};

static PyObject*
noddyGetAttr(PyObject* self, char* attrname) {
    return Py_FindMethod(noddy_methods, self, attrname);
}

extern "C" _declspec(dllexport)PyTypeObject noddy_NoddyType = {
    PyObject_HEAD_INIT(NULL)
    0,
    "Noddy",
    sizeof(noddy_NoddyObject),
    0,
    noddy_noddy_dealloc,    /*tp_dealloc*/
    0,						/*tp_print*/
    noddyGetAttr,           /*tp_getattr*/
    0,          /*tp_setattr*/
    0,          /*tp_compare*/
    0,          /*tp_repr*/
    0,          /*tp_as_number*/
    0,          /*tp_as_sequence*/
    0,          /*tp_as_mapping*/
    0,          /*tp_hash */
};

DL_EXPORT(void)
initnoddy(void) 
{
    noddy_NoddyType.ob_type = &PyType_Type;

    Py_InitModule("noddy", noddy_methods);
}


--Boundary_(ID_xRd++t/Mn4vDAnuEgMc4Ug)
Content-type: application/octet-stream; name=cn.cpp
Content-transfer-encoding: 7bit
Content-disposition: attachment; filename=cn.cpp

#include <boost/python.hpp>
#include <d3d8.h>
using namespace boost::python;

//declarations
extern "C" _declspec(dllimport) PyTypeObject noddy_NoddyType;
typedef struct {
	PyObject_HEAD
	long magicNum;
} noddy_NoddyObject;

//magic structure
typedef struct _magic {
	_magic(){
		long deflong = 1957;
		magicnum= reinterpret_cast<HWND>(deflong);	
	}
	HWND magicnum;
} magic;

//converters
struct HWND_PyInt {
	static PyObject* convert(const HWND& h) {
		static PyObject* pyh = PyInt_FromLong((long) h);
		return pyh;
	}
};
struct convertNoddy {
	static HWND& execute(PyObject& o) {
		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>();
	to_python_converter<HWND, HWND_PyInt>();
	class_<magic>("magic")
		.def_readwrite("magicnum", &magic::magicnum);
}

--Boundary_(ID_xRd++t/Mn4vDAnuEgMc4Ug)--




More information about the Cplusplus-sig mailing list