Enhalten ist 1x eine Funktion die etwas ausgibt. Und zwar "println" und dann wollte ich noch versuchen mittels ein paar Samples aus dem Internet einen ObjectWrapper für mein Objekt zu schreiben. Leider scheiter ich jedoch. Kann mir jemand helfen?
Danke!! Oder wo ich eventuell alternative SourceCodes herbekomme damit ich mir das ganze an einem Beispiel klarmachen kann.
Code: Alles auswählen
#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;
}