00001 #include "PyObjectPlus.h" 00002 #include "EmbeddedClass.h" 00003 00004 00005 /*------------------------------ 00006 * EmbeddedClass Type // TYPE structure 00007 ------------------------------*/ 00008 00009 PyTypeObject EmbeddedClass::Type = { 00010 PyObject_HEAD_INIT(&PyType_Type) 00011 0, /*ob_size*/ 00012 "EmbeddedClass", /*tp_name*/ 00013 sizeof(EmbeddedClass), /*tp_basicsize*/ 00014 0, /*tp_itemsize*/ 00015 /* methods */ 00016 PyDestructor, /*tp_dealloc*/ 00017 0, /*tp_print*/ 00018 __getattr, /*tp_getattr*/ 00019 __setattr, /*tp_setattr*/ 00020 0, /*tp_compare*/ 00021 __repr, /*tp_repr*/ 00022 0, /*tp_as_number*/ 00023 0, /*tp_as_sequence*/ 00024 0, /*tp_as_mapping*/ 00025 0, /*tp_hash*/ 00026 0 //sPyCycle, /*tp_call */ 00027 }; 00028 00029 /*------------------------------ 00030 * NA_PWC Methods // Methods structure 00031 ------------------------------*/ 00032 PyMethodDef EmbeddedClass::Methods[] = { 00033 {"PrintHello", (PyCFunction) sPyPrintHello, Py_NEWARGS}, 00034 00035 {NULL, NULL} /* Sentinel */ 00036 }; 00037 00038 /*------------------------------ 00039 * EmbeddedClass Parents // Parents structure 00040 ------------------------------*/ 00041 PyParentObject EmbeddedClass::Parents[] = {&EmbeddedClass::Type, 00042 NULL}; // Sentinel 00043 00044 /*------------------------------ 00045 * EmbeddedClass constructor 00046 ------------------------------*/ 00047 EmbeddedClass::EmbeddedClass(char *name, 00048 00049 PyTypeObject *T) 00050 : PyObjectPlus(T) 00051 //: V(n0), NA(name, n0, tau0, T) 00052 { 00053 00054 } 00055 00056 PyObject *EmbeddedClass::PyMake(PyObject *ignored, PyObject *args) // Python wrapper 00057 { 00058 // expects (name) (n) (tau=0) (gamma=0) 00059 char *name; 00060 //int n; 00061 //float tau = 0; 00062 //float gamma = 0; 00063 00064 Py_Try(PyArg_ParseTuple(args, "s",//i|ff", 00065 &name));//, &n, &tau, &gamma)); // Read arguments 00066 //Py_Assert(n > 0, PyExc_ValueError, "n <= 0"); // Check values ok 00067 //Py_Assert(tau >= 0, PyExc_ValueError, "tau << 0"); // Check values ok 00068 00069 return new EmbeddedClass(name); // Make new Python-able object 00070 } 00071 00072 /*------------------------------ 00073 * NA_PWC destructor 00074 ------------------------------*/ 00075 EmbeddedClass::~EmbeddedClass() // Everything handled in parent 00076 {} 00077 00078 00079 /*------------------------------ 00080 * NA_PWC Attributes 00081 ------------------------------*/ 00082 PyObject *EmbeddedClass::_getattr(char *attr) // __getattr__ function: note only need to handle new state 00083 { 00084 //if (streq(attr, "gamma")) // accessable new state 00085 // return Py_BuildValue("f", gamma); 00086 00087 //if (streq(attr, "locked")) // accessable new state 00088 // return Py_BuildValue("i", int(locked)); 00089 00090 _getattr_up(PyObjectPlus); // send to parent 00091 00092 } 00093 00094 int EmbeddedClass::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state 00095 { 00096 //if (streq(attr, "gamma")) // settable new state 00097 // gamma = PyFloat_AsDouble(value); 00098 00099 //else if (streq(attr, "locked")) // settable new state 00100 // { 00101 // if (PyObject_IsTrue(value)) 00102 // locked = true; 00103 // else 00104 // locked = false; 00105 // } 00106 //else 00107 // return NA::_setattr(attr, value); // send up to parent 00108 00109 return 0; // never reaches here -- keeps compiler from complaining 00110 } 00111 00112 /*------------------------------ 00113 * EmbeddedClass PrintHello // A typical new method 00114 ------------------------------*/ 00115 void EmbeddedClass::PrintHello(void) // C++ method 00116 { 00117 printf("hi!\n"); 00118 } 00119 00120 PyObject *EmbeddedClass::PyPrintHello(PyObject *args) // Python wrapper 00121 { 00122 PrintHello(); Py_Return; 00123 } 00124 00125 00126 00127 // These methods are modifications of methods already 00128 // available in the parent. This means that they 00129 // only need new C++ versions. The python wrappers 00130 // do not change. (Just make sure the C++ methods in 00131 // the parent are declared virtual.) 00132 00133 // This is the module initialization. 00134 00135 /*------------------------------ 00136 * Module Initialization 00137 ------------------------------*/ 00138 00139 static PyMethodDef FileMethods[] = { // Only one file method available to python: 00140 // make a new EmbeddedClass object. 00141 {"new", EmbeddedClass::PyMake, Py_NEWARGS}, 00142 00143 {NULL, NULL} // Sentinel 00144 }; 00145 00146 extern "C" { // Python is a C program and wants to call 00147 // init with a C protocol. 00148 void initEmbeddedClass(void) 00149 { 00150 Py_InitModule("EmbeddedClass", FileMethods); 00151 } 00152 } 00153 00154