Python Extension: Packaging C ++ classes

Source: Internet
Author: User

Reprinted with the source: http://blog.csdn.net/you_lan_hai/article/details/8597451

// Testpython. cpp: defines the entry point of the console application. // Author: Swim blue ocean // blog: http://blog.csdn.net/you_lan_hai#include "stdafx. H "# include <iostream> using namespace STD; # include ".. /Python/include/python. H "# include ".. /Python/include/structmember. H "// Python export function with multiple parameters static pyobject * py_test (pyobject * Self, pyobject * ARGs) {cout <"this message comes from C ++" <Endl; py_incref (py_none); Return py_none ;} // method definition static pymethoddef lazymethods [] = {"test", pycfu Nction (py_test), meth_varargs, "just for test"}, {null, null}, // end flag}; // Python export class testpyclass: public pyobject {public: testpyclass (pytypeobject * ptype): ID _ (0), score _ (99) {If (pytype_ready (ptype) <0) {cout <"pytype_ready faild. "<Endl;} pyobject_init (this, ptype);} virtual ~ Testpyclass () {} static pyobject * py_new (pytypeobject * ptype, pyobject *, pyobject *) {return New testpyclass (ptype);} static void py_dealloc (pyobject * Self) {Delete (testpyclass *) self;} // init method. Static int py_init (pyobject * Self, pyobject * ARGs, pyobject *) {testpyclass * pthis = (testpyclass *) self; If (! Pyarg_parsetuple (ARGs, "II", & pthis-> ID _, & pthis-> score _) {return 0 ;}return 1 ;}pyobject * py_test (pyobject * ARGs) {cout <"this message comes from testpyclass. "<Endl; py_incref (py_none); Return py_none;} // export function static pyobject * _ py_test (pyobject * Self, pyobject * ARGs) {return (testpyclass *) self)-> py_test (ARGs);} int ID _; int score _ ;};/* If the class contains virtual functions, the starting address of the class object is the address of a virtual function table. Because pyobject does not have a virtual function, and subclass has a virtual function, the Child class and the base class do not have a starting address in total. */# Define offsetofvirtual (type, member) (INT) & (type *) 0)-> Member-sizeof (void *)) // member variable static pymemberdef testclassmembers [] = {"ID", t_int, offsetofvirtual (testpyclass, ID _), 0, "ID"}, {"score", t_int, offsetofvirtual (testpyclass, score _), 0, "score" },{ null, 0, null },}; // member function static pymethoddef testclassmethods [] = {"test", pycfunction (testpyclass: _ py_test), meth_varargs, "Just fo R test "},{ null, null },}; // class type Static pytypeobject testpyclass_type = {pyvarobject_head_init (& pytype_type, 0)" testpyclass ", sizeof (testpyclass), 0, (destructor) testpyclass: py_dealloc,/* tp_dealloc */0,/* tp_print */0,/* 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 */0,/* tp_call */0,/* tp_s Tr */0,/* tp_getattro */0,/* tp_setattro */0,/* tp_as_buffer */py_tpflags_default | py_tpflags_basetype,/* tp_flags */0, /* tp_doc */0,/* tp_traverse */0,/* tp_clear */0,/* tp_richcompare */0,/* tp_weaklistoffset */0, /* tp_iter */0,/* tp_iternext */testclassmethods,/* tp_methods */testclassmembers,/* tp_members */0,/* tp_getset */0, /* tp_base */0,/* tp_dict */0,/* tp_descr_get */0,/* TP _ Descr_set */0,/* tp_dictoffset */(initproc) testpyclass: py_init,/* tp_init */0,/* tp_alloc */(newfunc) testpyclass: py_new, /* tp_new */0,/* tp_free */}; void initlazy (void) {pyobject * pmodule = py_initmodule ("lazy", lazymethods); If (pmodule) {py_incref (pyobject *) & testpyclass_type); pymodule_addobject (pmodule, "testclass", (pyobject *) & testpyclass_type) ;}} int _ tmain (INT argc, _ tchar * argv []) {py_setp Ythonhome ("F:/workspace/test/Python"); py_initialize (); If (! Py_isinitialized () {cout <"py_initialize faild! "<Endl; pyerr_print (); Return 0 ;}cout <" Python initialize success. "<Endl; initlazy (); pyrun_simplestring (" Import lazy "); pyrun_simplestring (" lazy. test () "); pyrun_simplestring (" A = lazy. testclass (2, 3) "); pyrun_simplestring (" Print Dir (a) "); pyrun_simplestring (". test () "); pyrun_simplestring (" Print 'a. id = ',. ID, ',. score = ',. score "); # If 0 // test object size. Classes containing virtual functions are 4 bytes in size. Cout <"sizeof (pyobject)" <sizeof (pyobject) <Endl; cout <"sizeof (testpyclass)" <sizeof (testpyclass) <Endl; testpyclass testpy (& testpyclass_type); cout <"testpy ADDR:" <& testpy <"<(pyobject *) & testpy <", ID: "<& (testpyclass *) 0)-> ID _) <Endl; # endifpy_finalize (); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.