00001 00002 /*************************************************************************** 00003 * Generic include file providing customizable methods table 00004 * 00005 * For mex2c_lib: 00006 * Contains one function which 00007 * 00008 * For mex2py: 00009 * This file contains one function which notifies Python about 00010 * a methods table (here called mxMethods). The table is used to interface 00011 * a mexFunction, declared static in the current namespace, to python's 00012 * C-API using the numeric array (numpy) types. Because any and all 00013 * identifiers *not* declared static can conflict with python or each other, 00014 * we declare things static in this file. 00015 * This file has been written to be generic across multiple mex files. 00016 * It expects that PROGNAME is #defined to be a unique string. 00017 * It expects to be #included from the file containing mexFunction, because 00018 * mexFunction must be declared static to get around the namespace problems 00019 * outlined above. 00020 ***************************************************************************/ 00021 00022 #ifndef _generic_mex_tail_h_ 00023 #define _generic_mex_tail_h_ 00024 00025 /**** Hook for generic inclusion *************************************/ 00026 00027 #ifdef MEX2C_TAIL_FILE 00028 #include MEX2C_TAIL_FILE 00029 #endif 00030 00031 /**** mex2lib: mexfile into c library ********************************/ 00032 00033 #ifdef USING_MEX2LIB 00034 00035 /* utility: paste main_ plus PROGNAME into one token */ 00036 #define MAINNAME1(X) MAINNAME(X) 00037 #define MAINNAME(X) main_ ## X 00038 00039 /* this is the c-library entry point */ 00040 char * 00041 MAINNAME1(PROGNAME) (int nlhs, mxArray **plhs, int nrhs, mxArray **prhs) 00042 { 00043 char *err; 00044 00045 err = mxt_mexDispatcher(mexFunction, progname, 1, 00046 nlhs, plhs, 00047 nrhs, (const mxArray **) prhs); 00048 return err; 00049 } 00050 00051 #undef MAINNAME1 00052 #undef MAINNAME 00053 00054 #endif /* USING_MEX2LIB */ 00055 00056 /**** mex2py: mexfile into numpy shared object ***********************/ 00057 00058 #ifdef USING_MEX2PY 00059 00060 /* Provide a constant function pointer to initialize the mxMethods table */ 00061 static PyObject * 00062 mx_entry_proxy(PyObject *self, PyObject *args, PyObject *kw) 00063 { 00064 /* note, self is not in the argument list */ 00065 return mx_entry(mexFunction, args, kw, 00066 NARGIN_MIN, 00067 NARGIN_MAX, 00068 NARGOUT_MIN, 00069 NARGOUT_MAX, 00070 in_specs, in_names, out_names); 00071 } 00072 00073 /* methods table */ 00074 static PyMethodDef mxMethods[] = { 00075 /* key method is mexFunction, visible to Python as lhs = modulename.main(rhs) */ 00076 { 00077 "main", 00078 (PyCFunction) mx_entry_proxy, 00079 METH_VARARGS|METH_KEYWORDS, 00080 docstring /* autogenerated, and in Doc/PROGNAME_docstring.h */ 00081 }, 00082 /* other methods here as needed */ 00083 /* ... */ 00084 /* sentinel */ 00085 { 00086 NULL, NULL, 0, NULL 00087 } 00088 }; 00089 00090 /* utility: paste init plus PROGNAME into one token */ 00091 #define INITNAME1(X) INITNAME(X) 00092 #define INITNAME(X) init ## X 00093 00094 /* initialization */ 00095 void INITNAME1(PROGNAME) () { 00096 /* module name below must be consistent with shared library name */ 00097 (void) Py_InitModule(progname, mxMethods); 00098 import_array(); /* Must be present for NumPy. */ 00099 } 00100 00101 #undef INITNAME1 00102 #undef INITNAME 00103 00104 #endif /* USING_MEX2PY */ 00105 00106 #endif /* _generic_mex_tail_h */