FunctionHolder.cxx

Go to the documentation of this file.
00001 // Author: Wim Lavrijsen, Apr 2005
00002 
00003 // Bindings
00004 #include "PyROOT.h"
00005 #include "FunctionHolder.h"
00006 #include "ObjectProxy.h"
00007 #include "Adapters.h"
00008 
00009 // ROOT
00010 #include "TClass.h"
00011 
00012 
00013 //- data and local helpers ---------------------------------------------------
00014 namespace {
00015 
00016    TClassRef GetGlobalNamespace() {
00017       static TClass c;
00018       return &c;
00019    }
00020 
00021 } // unnamed namespace
00022 
00023 
00024 //- constructor -----------------------------------------------------------------
00025 namespace PyROOT {
00026 
00027 #ifdef PYROOT_USE_REFLEX
00028 template<>
00029 TFunctionHolder< ROOT::Reflex::Scope, ROOT::Reflex::Member >::TFunctionHolder(
00030       const ROOT::Reflex::Member& function ) :
00031    TMethodHolder< ROOT::Reflex::Scope, ROOT::Reflex::Member >( ROOT::Reflex::Scope(), function )
00032 {
00033 }
00034 #endif
00035 
00036 } // namespace PyROOT
00037 
00038 template< class T, class M >
00039 PyROOT::TFunctionHolder< T, M >::TFunctionHolder( const M& function ) :
00040       TMethodHolder< T, M >( GetGlobalNamespace().GetClass(), function )
00041 {
00042 }
00043 
00044 template< class T, class M >
00045 PyROOT::TFunctionHolder< T, M >::TFunctionHolder( const T& scope, const M& function ) :
00046       TMethodHolder< T, M >( scope, function )
00047 {
00048 }
00049 
00050 //- public members --------------------------------------------------------------
00051 template< class T, class M >
00052 PyObject* PyROOT::TFunctionHolder< T, M >::FilterArgs(
00053       ObjectProxy*& self, PyObject* args, PyObject* )
00054 {
00055 // no self means called as a free function; all ok
00056    if ( ! self ) {
00057       Py_INCREF( args );
00058       return args;
00059    }
00060 
00061 // otherwise, add self as part of the function arguments (means bound member)
00062    Py_ssize_t sz = PyTuple_GET_SIZE( args );
00063    PyObject* newArgs = PyTuple_New( sz + 1 );
00064    for ( int i = 0; i < sz; ++i ) {
00065       PyObject* item = PyTuple_GET_ITEM( args, i );
00066       Py_INCREF( item );
00067       PyTuple_SET_ITEM( newArgs, i + 1, item );
00068    }
00069 
00070    Py_INCREF( self );
00071    PyTuple_SET_ITEM( newArgs, 0, (PyObject*)self );
00072 
00073    return newArgs;
00074 }
00075 
00076 //____________________________________________________________________________
00077 template< class T, class M >
00078 PyObject* PyROOT::TFunctionHolder< T, M >::operator()(
00079       ObjectProxy* self, PyObject* args, PyObject* kwds, Long_t user )
00080 {
00081 // preliminary check in case keywords are accidently used (they are ignored otherwise)
00082    if ( kwds != 0 && PyDict_Size( kwds ) ) {
00083       PyErr_SetString( PyExc_TypeError, "keyword arguments are not yet supported" );
00084       return 0;
00085    }
00086 
00087 // setup as necessary
00088    if ( ! this->Initialize() )
00089       return 0;                              // important: 0, not Py_None
00090 
00091 // reorder self into args, if necessary
00092    if ( ! ( args = this->FilterArgs( self, args, kwds ) ) )
00093       return 0;
00094 
00095 // translate the arguments
00096    Bool_t bConvertOk = this->SetMethodArgs( args, user );
00097    Py_DECREF( args );
00098 
00099    if ( bConvertOk == kFALSE )
00100       return 0;                              // important: 0, not Py_None
00101 
00102 // execute function
00103    return this->Execute( 0 );
00104 }
00105 
00106 //____________________________________________________________________________
00107 template class PyROOT::TFunctionHolder< PyROOT::TScopeAdapter, PyROOT::TMemberAdapter >;
00108 #ifdef PYROOT_USE_REFLEX
00109 template class PyROOT::TFunctionHolder< ROOT::Reflex::Scope, ROOT::Reflex::Member >;
00110 #endif

Generated on Tue Jul 5 14:10:12 2011 for ROOT_528-00b_version by  doxygen 1.5.1