00001 #ifndef GAUDIPYTHON_HELPERS_H
00002 #define GAUDIPYTHON_HELPERS_H
00003
00004
00005 #include "GaudiKernel/IAlgManager.h"
00006 #include "GaudiKernel/IAlgorithm.h"
00007 #include "GaudiKernel/IAlgTool.h"
00008
00009 #ifdef _POSIX_C_SOURCE
00010 #undef _POSIX_C_SOURCE
00011 #endif
00012 #include "Python.h"
00013
00014 #if PY_VERSION_HEX < 0x02050000
00015
00016
00017
00018
00019
00020
00021
00023 typedef int Py_ssize_t;
00024 #endif
00025
00026
00027
00028
00029
00030
00031
00032
00033
00037 namespace GaudiPython {
00038
00039 struct Helper {
00040
00041
00042
00043 Helper() {}
00044
00045 static IService* service(ISvcLocator* svcloc, const std::string& name, bool createif=false) {
00046 return svcloc->service(name, createif).get();
00047 }
00048
00049 static IAlgorithm* algorithm(IAlgManager* algmgr, const std::string& name) {
00050 const bool createIf = false;
00051 return algmgr->algorithm(name, createIf).get();
00052 }
00053 static DataObject* dataobject(IDataProviderSvc* dpsvc, const std::string& path ) {
00054 DataObject* o;
00055 if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
00056 else return 0;
00057 }
00058 static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
00059 IAlgTool* o;
00060 if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
00061 else return 0;
00062 }
00063 static long loadDynamicLib(const std::string& name) {
00064 void* h;
00065 return System::loadDynamicLib(name, &h);
00066 }
00067 static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
00068 IHistogram1D* h;
00069 if ( hsvc->findObject(path, h ).isSuccess() ) return h;
00070 else return 0;
00071 }
00072 static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
00073 IHistogram2D* h;
00074 if ( hsvc->findObject(path, h ).isSuccess() ) return h;
00075 else return 0;
00076 }
00077 static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
00078 IHistogram3D* h;
00079 if ( hsvc->findObject(path, h ).isSuccess() ) return h;
00080 else return 0;
00081 }
00082 static IProfile1D*
00083 profile1D
00084 ( IHistogramSvc* hsvc ,
00085 const std::string& path )
00086 {
00087 IProfile1D* h = 0 ;
00088 if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
00089 return 0 ;
00090 }
00091 static IProfile2D*
00092 profile2D
00093 ( IHistogramSvc* hsvc ,
00094 const std::string& path )
00095 {
00096 IProfile2D* h = 0 ;
00097 if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
00098 return 0 ;
00099 }
00100
00101 private:
00102 template <class T>
00103 static Py_ssize_t Array_length( PyObject* self ) {
00104 #if PY_VERSION_HEX < 0x02050000
00105 const
00106 #endif
00107 char* buf = 0;
00108 Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
00109 return size/sizeof(T);
00110 }
00111
00112 template <class T> static PyObject* toPython(T* o) { return 0; }
00113 static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
00114 static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
00115 static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
00116 static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
00117 static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
00118 static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
00119
00120 template <class T>
00121 static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
00122 #if PY_VERSION_HEX < 0x02050000
00123 const
00124 #endif
00125 char* buf = 0;
00126 Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
00127 if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
00128 PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
00129 return 0;
00130 }
00131 return toPython((T*)buf + idx);
00132 }
00133
00134 public:
00135 template <class T>
00136 static PyObject* toArray( T* ptr, Py_ssize_t size ) {
00137 static PyTypeObject type = PyBuffer_Type;
00138 static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
00139 #if PY_VERSION_HEX < 0x02050000
00140 meth.sq_item = (intargfunc) &Array_item<T>;
00141 meth.sq_length = (inquiry) &Array_length<T>;
00142 #else
00143 meth.sq_item = (ssizeargfunc) &Array_item<T>;
00144 meth.sq_length = (lenfunc) &Array_length<T>;
00145 #endif
00146 type.tp_as_sequence = &meth;
00147 PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
00148 Py_INCREF( &type );
00149 buf->ob_type = &type;
00150 return buf;
00151 }
00152 static PyObject* toIntArray ( void* ptr, Py_ssize_t size ) { return toArray( (int*) ptr , size ); }
00153 static PyObject* toShortArray ( void* ptr, Py_ssize_t size ) { return toArray( (short*) ptr , size ); }
00154 static PyObject* toFloatArray ( void* ptr, Py_ssize_t size ) { return toArray( (float*) ptr , size ); }
00155 static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
00156
00157 template <class T>
00158 static T* toAddress( std::vector<T>& v ) {
00159 return &(*v.begin());
00160 }
00161 template <class T>
00162 static T* toAddress( void * a) {
00163 return (T*)a;
00164 }
00165
00166 };
00167
00168 template PyObject* Helper::toArray(int*,Py_ssize_t);
00169 template PyObject* Helper::toArray(char*,Py_ssize_t);
00170 template PyObject* Helper::toArray(short*,Py_ssize_t);
00171 template PyObject* Helper::toArray(float*,Py_ssize_t);
00172 template PyObject* Helper::toArray(double*,Py_ssize_t);
00173 template int* Helper::toAddress(std::vector<int>&);
00174 template float* Helper::toAddress(std::vector<float>&);
00175 template double* Helper::toAddress(std::vector<double>&);
00176 template int* Helper::toAddress<int>(void*);
00177 template float* Helper::toAddress<float>(void*);
00178 template double* Helper::toAddress<double>(void*);
00179
00180 }
00181
00182 #endif // !GAUDIPYTHON_HELPERS_H