![]() |
|
|
Generated: 8 Jan 2009 |
00001 // $Id: Algorithm.cpp,v 1.8 2008/10/28 10:40:19 marcocle Exp $ 00002 // ============================================================================ 00003 // Incldue files 00004 // ============================================================================ 00005 #ifdef _POSIX_C_SOURCE 00006 #undef _POSIX_C_SOURCE 00007 #endif 00008 #include "Python.h" 00009 // ============================================================================ 00010 // GaudiKernel 00011 // ============================================================================ 00012 #include "GaudiKernel/Bootstrap.h" 00013 // ============================================================================ 00014 // GaudiPython 00015 // ============================================================================ 00016 #include "GaudiPython/Algorithm.h" 00017 // ============================================================================ 00018 00019 namespace { 00026 inline char *chr(const char*c){ return const_cast<char*>(c); } 00027 } 00028 00029 // ============================================================================ 00031 // ============================================================================ 00032 StatusCode GaudiPython::call_python_method 00033 ( PyObject* self, const char* method ) 00034 { 00035 StatusCode sc = StatusCode::FAILURE; 00036 // check arguments 00037 if ( 0 == self || 0 == method ) { return StatusCode::FAILURE ; } 00038 00039 // call Python 00040 PyObject* r = PyObject_CallMethod(self, chr(method), chr("")); 00041 00042 if ( 0 == r ) { PyErr_Print() ; return sc ; } // RETURN 00043 00044 if ( PyInt_Check ( r ) ) 00045 { sc = PyInt_AS_LONG( r ) ; Py_DECREF( r ) ; return sc ; } // RETURN 00046 00047 // look for the method getCode with the signature: 00048 // ' int getCode() ' 00049 PyObject* c = PyObject_CallMethod(r, chr("getCode"), chr("")); 00050 00051 if ( 0 == c ) { PyErr_Print() ; } 00052 else if ( PyLong_Check( c )) { sc = PyLong_AsLong( c ); } 00053 else 00054 { 00055 std::string msg( " call_python_method unexpected type from '" ); 00056 msg += method ; 00057 msg += "().getCode()' " ; 00058 PyErr_SetString( PyExc_TypeError , msg.c_str() ) ; 00059 PyErr_Print() ; 00060 } 00061 // release used objects 00062 Py_XDECREF( c ) ; 00063 Py_XDECREF( r ) ; 00064 // 00065 return sc; 00066 } ; 00067 // ============================================================================ 00068 00069 // ============================================================================ 00074 // ============================================================================ 00075 GaudiPython::PyAlgorithm::PyAlgorithm 00076 ( PyObject* self , 00077 const std::string& name ) 00078 : Algorithm ( name , Gaudi::svcLocator() ) 00079 , m_self ( self ) 00080 { 00081 // The owner of the Algorithm is Python (as creator) therefore 00082 // it should not be deleted by Gaudi (added an extra addRef()). 00083 addRef() ; 00084 addRef() ; 00085 }; 00086 // ============================================================================ 00087 00088 // ============================================================================ 00089 StatusCode GaudiPython::PyAlgorithm::initialize () 00090 { return GaudiPython::call_python_method ( m_self , "initialize" ) ; } 00091 // ============================================================================ 00092 StatusCode GaudiPython::PyAlgorithm::start () 00093 { return GaudiPython::call_python_method ( m_self , "start" ) ; } 00094 // ============================================================================ 00095 StatusCode GaudiPython::PyAlgorithm::execute () 00096 { return GaudiPython::call_python_method ( m_self , "execute" ) ; } 00097 // ============================================================================ 00098 StatusCode GaudiPython::PyAlgorithm::stop () 00099 { return GaudiPython::call_python_method ( m_self , "stop" ) ; } 00100 // ============================================================================ 00101 StatusCode GaudiPython::PyAlgorithm::finalize () 00102 { return GaudiPython::call_python_method ( m_self , "finalize" ) ; } 00103 00104 StatusCode GaudiPython::PyAlgorithm::beginRun() { 00105 return GaudiPython::call_python_method ( m_self , "beginRun" ) ; } 00106 StatusCode GaudiPython::PyAlgorithm::endRun() { 00107 return GaudiPython::call_python_method ( m_self , "endRun" ) ; } 00108