| # Gaudi Framework, version v21r6 |

Home |  | Generated: 11 Nov 2009

# Helpers.h

Go to the documentation of this file.

    00001 #ifndef GAUDIPYTHON_HELPERS_H
    00002 #define GAUDIPYTHON_HELPERS_H
    00003
    00004 // Framework
    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 // Note (MCl):
    00016 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
    00017 // instead of int. The also changed the name of the typedefs for those functions.
    00018 // Here we use:
    00019 //  intargfunc -> ssizeargfunc
    00020 //  inquiry    -> lenfunc
    00021 //
    00023 typedef int Py_ssize_t;
    00024 #endif
    00025
    00026 // the following is done instead of including Python.h becuase there is
    00027 // a clash with codecs.h defined in Python and the one in VC++ 7.1
    00028 //struct _object;
    00029 //typedef _object PyObject;
    00030 //extern "C" {
    00031 //  PyObject* PyBuffer_FromMemory( void *ptr, int size);
    00032 //}
    00033
    00037 namespace GaudiPython {
    00038
    00039 struct Helper {
    00040   // This is a number of static functions to overcome the current problem with PyLCGDict that
    00041   // does not allow to return instances of complex objects by calling arguments
    00042   // (reference to pointers)
    00043   Helper() {}
    00044   // Provided for backward compatibility
    00045   static IService* service(ISvcLocator* svcloc, const std::string& name, bool createif=false) {
    00046     return svcloc->service(name, createif).get();
    00047   }
    00048   // Provided for backward compatibility
    00049   static IAlgorithm* algorithm(IAlgManager* algmgr, const std::string& name) {
    00050     const bool createIf = false;
    00051     return algmgr->algorithm(name, createIf).get();
    00052   }
    00053
    00054   // ==========================================================================
    00055   static DataObject* dataobject ( IDataProviderSvc*  dpsvc ,
    00056                                   const std::string& path  )
    00057   {
    00058     DataObject* o;
    00059     if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
    00060     else return 0;
    00061   }
    00062   // ==========================================================================
    00073   static GAUDI_API DataObject* findobject
    00074   ( IDataProviderSvc*  dpsvc ,
    00075     const std::string& path  ) ;
    00076   // ==========================================================================
    00088   static GAUDI_API DataObject* getobject
    00089   ( IDataProviderSvc*  dpsvc               ,
    00090     const std::string& path                ,
    00091     const bool         retrieve    = true  ,
    00092     const bool         disableDoD  = false ) ;
    00093   // ==========================================================================
    00094   static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
    00095     IAlgTool* o;
    00096     if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
    00097     else return 0;
    00098   }
    00099   static long loadDynamicLib(const std::string& name) {
    00100     void* h;
    00101     return System::loadDynamicLib(name, &h);
    00102   }
    00103   static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
    00104     IHistogram1D* h;
    00105     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00106     else                                          return 0;
    00107   }
    00108   static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
    00109     IHistogram2D* h;
    00110     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00111     else                                          return 0;
    00112   }
    00113   static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
    00114     IHistogram3D* h;
    00115     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00116     else                                          return 0;
    00117   }
    00118   static IProfile1D*
    00119   profile1D
    00120   ( IHistogramSvc*     hsvc ,
    00121     const std::string& path )
    00122   {
    00123     IProfile1D* h = 0 ;
    00124     if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
    00125     return 0 ;
    00126   }
    00127   static IProfile2D*
    00128   profile2D
    00129   ( IHistogramSvc*     hsvc ,
    00130     const std::string& path )
    00131   {
    00132     IProfile2D* h = 0 ;
    00133     if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
    00134     return 0 ;
    00135   }
    00136 // Array support
    00137 private:
    00138   template <class T>
    00139   static Py_ssize_t Array_length( PyObject* self ) {
    00140 #if PY_VERSION_HEX < 0x02050000
    00141     const
    00142 #endif
    00143     char* buf = 0;
    00144     Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
    00145     return size/sizeof(T);
    00146   }
    00147
    00148   template <class T> static PyObject* toPython(T* o) { return 0; }
    00149   static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
    00150   static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
    00151   static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
    00152   static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
    00153   static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
    00154   static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
    00155
    00156   template <class T>
    00157   static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
    00158 #if PY_VERSION_HEX < 0x02050000
    00159     const
    00160 #endif
    00161     char* buf = 0;
    00162     Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
    00163     if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
    00164        PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
    00165        return 0;
    00166     }
    00167     return toPython((T*)buf + idx);
    00168   }
    00169
    00170 public:
    00171   template <class T>
    00172   static PyObject* toArray( T* ptr, Py_ssize_t size ) {
    00173    static PyTypeObject      type = PyBuffer_Type;
    00174    static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
    00175 #if PY_VERSION_HEX < 0x02050000
    00176    meth.sq_item         = (intargfunc) &Array_item<T>;
    00177    meth.sq_length       = (inquiry) &Array_length<T>;
    00178 #else
    00179    meth.sq_item         = (ssizeargfunc) &Array_item<T>;
    00180    meth.sq_length       = (lenfunc) &Array_length<T>;
    00181 #endif
    00182    type.tp_as_sequence  = &meth;
    00183    PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
    00184    Py_INCREF( &type );
    00185    buf->ob_type = &type;
    00186    return buf;
    00187   }
    00188   static PyObject* toIntArray    ( void* ptr, Py_ssize_t size ) { return toArray( (int*)    ptr , size ); }
    00189   static PyObject* toShortArray  ( void* ptr, Py_ssize_t size ) { return toArray( (short*)  ptr , size ); }
    00190   static PyObject* toFloatArray  ( void* ptr, Py_ssize_t size ) { return toArray( (float*)  ptr , size ); }
    00191   static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
    00192
    00193   template <class T>
    00194   static T* toAddress( std::vector<T>& v ) {
    00195     return &(*v.begin());
    00196   }
    00197   template <class T>
    00198   static T* toAddress( void * a) {
    00199     return (T*)a;
    00200   }
    00201
    00202 };
    00203
    00204 template PyObject* Helper::toArray(int*,Py_ssize_t);
    00205 template PyObject* Helper::toArray(char*,Py_ssize_t);
    00206 template PyObject* Helper::toArray(short*,Py_ssize_t);
    00207 template PyObject* Helper::toArray(float*,Py_ssize_t);
    00208 template PyObject* Helper::toArray(double*,Py_ssize_t);
    00209 template int* Helper::toAddress(std::vector<int>&);
    00210 template float* Helper::toAddress(std::vector<float>&);
    00211 template double* Helper::toAddress(std::vector<double>&);
    00212 template int* Helper::toAddress<int>(void*);
    00213 template float* Helper::toAddress<float>(void*);
    00214 template double* Helper::toAddress<double>(void*);
    00215
    00216 } // namespace GaudiPython
    00217
    00218 #endif // !GAUDIPYTHON_HELPERS_H