donnerCobra 0 Newbie Poster

Hi, I wanted to ask if you can help me with my example-sourecode to Wrap an C++ Object. I know Boost, but I don't want to use it cause of several points.

Does someone knows an example code for an Object Wrapper? Thanks A LOT!!!

#include <Python/Python.h>
#include <string.h>

using namespace std;

//Function to print out something///////////////////////////////////////////
PyObject* println(PyObject* pSelf, PyObject* pArgs)
{
	char* s = NULL;
	if (!PyArg_ParseTuple(pArgs, "s", &s)) return NULL;
	printf("%s",s);
	
	Py_INCREF(Py_None);
	return Py_None;
}

static PyMethodDef myMethods[] = {
    {"println", println, METH_VARARGS},
    {NULL, NULL, 0, NULL}
};








////////CLASS WRAPPER!!!!!//////////////////////////////////////////////

class Numbers
{
public:
    Numbers(int first, int second)
	{
		m_first = first;
		m_second = second;
	}
    double NumMemberMult(void){ return m_first*m_second;}
private:
    int m_first;
    double m_second;
};

static void PyDelNumbers(void *ptr)
{
    //std::cout<<"Called PyDelNumbers()\n"; //Good to see once
    Numbers * oldnum = static_cast<Numbers *>(ptr);
    delete oldnum;
    return;
}

PyObject *wrap_new_Numbers(PyObject *, PyObject* args)
{
    //First, extract the arguments from a Python tuple
    int arg1;
    int arg2;
    int ok = PyArg_ParseTuple(args,"ii",&arg1,&arg2);
    if(!ok) return 0;
 
    //Second, dynamically allocate a new object
    Numbers *newnum = new Numbers(arg1, arg2);

    //Third, wrap the pointer as a "PyCObject" and
    // return that object to the interpreter
    return PyCObject_FromVoidPtr( newnum, PyDelNumbers);
}
////////////////////////////////////

int main (int argc, char **argv) {

	Py_Initialize();
	
	PyObject *m = Py_InitModule("c4d", myMethods);
	Py_INCREF((PyObject *)&wrap_new_Numbers);
    PyModule_AddObject(m, "Numbers", (PyObject *)&wrap_new_Numbers);

	PyRun_SimpleString("from c4d import *;\nprintln('ich bin ein test');\ntest = Numbers(3,4)");

	Py_Finalize();
    return 0;
}