00001
00002
00003
00004 #include "PyROOT.h"
00005 #include "TemplateProxy.h"
00006 #include "Utility.h"
00007
00008
00009 namespace PyROOT {
00010
00011 namespace {
00012
00013
00014 TemplateProxy* tpp_new( PyTypeObject*, PyObject*, PyObject* )
00015 {
00016 TemplateProxy* pytmpl = PyObject_GC_New( TemplateProxy, &TemplateProxy_Type );
00017 pytmpl->fPyName = NULL;
00018 pytmpl->fPyClass = NULL;
00019 pytmpl->fSelf = NULL;
00020
00021 PyObject_GC_Track( pytmpl );
00022 return pytmpl;
00023 }
00024
00025
00026 void tpp_dealloc( TemplateProxy* pytmpl )
00027 {
00028 PyObject_GC_UnTrack( pytmpl );
00029 PyObject_GC_Del( pytmpl );
00030 }
00031
00032
00033 int tpp_traverse( TemplateProxy* pytmpl, visitproc visit, void* args )
00034 {
00035 if ( pytmpl->fPyName ) {
00036 int err = visit( (PyObject*)pytmpl->fPyName, args );
00037 if ( err )
00038 return err;
00039 }
00040
00041 if ( pytmpl->fPyClass ) {
00042 int err = visit( (PyObject*)pytmpl->fPyClass, args );
00043 if ( err )
00044 return err;
00045 }
00046
00047 if ( pytmpl->fSelf ) {
00048 int err = visit( (PyObject*)pytmpl->fSelf, args );
00049 if ( err )
00050 return err;
00051 }
00052
00053 return 0;
00054 }
00055
00056
00057 int tpp_clear( TemplateProxy* pytmpl )
00058 {
00059 Py_XDECREF( (PyObject*)pytmpl->fPyName );
00060 pytmpl->fPyName = NULL;
00061
00062 Py_XDECREF( (PyObject*)pytmpl->fPyClass );
00063 pytmpl->fPyClass = NULL;
00064
00065 Py_XDECREF( (PyObject*)pytmpl->fSelf );
00066 pytmpl->fSelf = NULL;
00067
00068 return 0;
00069 }
00070
00071
00072 PyObject* tpp_call( TemplateProxy* pytmpl, PyObject* args, PyObject* kwds )
00073 {
00074
00075
00076 PyObject* pymeth = 0;
00077
00078 Py_ssize_t nArgs = PyTuple_GET_SIZE( args );
00079 if ( 1 <= nArgs ) {
00080
00081
00082 Py_INCREF( pytmpl->fPyName );
00083 PyObject* pyname = pytmpl->fPyName;
00084 if ( Utility::BuildTemplateName( pyname, args, 0 ) ) {
00085
00086 pymeth = PyObject_GetAttr( pytmpl->fSelf, pyname );
00087 }
00088 Py_XDECREF( pyname );
00089
00090 }
00091
00092 if ( pymeth )
00093 return pymeth;
00094
00095
00096 PyErr_Clear();
00097 pymeth = PyObject_GetAttrString( pytmpl->fSelf, const_cast< char* >(
00098 (std::string( "__generic_" ) + PyROOT_PyUnicode_AsString( pytmpl->fPyName )).c_str()) );
00099
00100 if ( pymeth )
00101 return PyObject_Call( pymeth, args, kwds );
00102
00103 return pymeth;
00104 }
00105
00106
00107 TemplateProxy* tpp_descrget( TemplateProxy* pytmpl, PyObject* pyobj, PyObject* )
00108 {
00109
00110 TemplateProxy* newPyTmpl = (TemplateProxy*)TemplateProxy_Type.tp_alloc( &TemplateProxy_Type, 0 );
00111
00112
00113 Py_INCREF( pytmpl->fPyName );
00114 newPyTmpl->fPyName = pytmpl->fPyName;
00115
00116 Py_XINCREF( pytmpl->fPyClass );
00117 newPyTmpl->fPyClass = pytmpl->fPyClass;
00118
00119
00120 Py_XINCREF( pyobj );
00121 newPyTmpl->fSelf = pyobj;
00122
00123 return newPyTmpl;
00124 }
00125
00126 }
00127
00128
00129
00130 PyTypeObject TemplateProxy_Type = {
00131 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
00132 (char*)"ROOT.TemplateProxy",
00133 sizeof(TemplateProxy),
00134 0,
00135 (destructor)tpp_dealloc,
00136 0,
00137 0,
00138 0,
00139 0,
00140 0,
00141 0,
00142 0,
00143 0,
00144 0,
00145 (ternaryfunc)tpp_call,
00146 0,
00147 0,
00148 0,
00149 0,
00150 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
00151 (char*)"PyROOT template proxy (internal)",
00152 (traverseproc)tpp_traverse,
00153 (inquiry)tpp_clear,
00154 0,
00155 0,
00156 0,
00157 0,
00158 0,
00159 0,
00160 0,
00161 0,
00162 0,
00163 (descrgetfunc)tpp_descrget,
00164 0,
00165 0,
00166 0,
00167 0,
00168 (newfunc)tpp_new,
00169 0,
00170 0,
00171 0,
00172 0,
00173 0,
00174 0,
00175 0
00176 #if PY_VERSION_HEX >= 0x02030000
00177 , 0
00178 #endif
00179 #if PY_VERSION_HEX >= 0x02060000
00180 , 0
00181 #endif
00182 };
00183
00184 }