| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# 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
    00050   ( IAlgManager*       algmgr           ,
    00051     const std::string& name             ,
    00052     const bool         createIf = false )
    00053   {
    00054     return algmgr->algorithm(name, createIf).get();
    00055   }
    00056   // ==========================================================================
    00057   static DataObject* dataobject ( IDataProviderSvc*  dpsvc ,
    00058                                   const std::string& path  )
    00059   {
    00060     DataObject* o;
    00061     if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
    00062     else return 0;
    00063   }
    00064   // ==========================================================================
    00075   static GAUDI_API DataObject* findobject
    00076   ( IDataProviderSvc*  dpsvc ,
    00077     const std::string& path  ) ;
    00078   // ==========================================================================
    00090   static GAUDI_API DataObject* getobject
    00091   ( IDataProviderSvc*  dpsvc               ,
    00092     const std::string& path                ,
    00093     const bool         retrieve    = true  ,
    00094     const bool         disableDoD  = false ) ;
    00095   // ==========================================================================
    00096   static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
    00097     IAlgTool* o;
    00098     if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
    00099     else return 0;
    00100   }
    00101   static long loadDynamicLib(const std::string& name) {
    00102     void* h;
    00103     return System::loadDynamicLib(name, &h);
    00104   }
    00105   static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
    00106     IHistogram1D* h;
    00107     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00108     else                                          return 0;
    00109   }
    00110   static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
    00111     IHistogram2D* h;
    00112     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00113     else                                          return 0;
    00114   }
    00115   static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
    00116     IHistogram3D* h;
    00117     if ( hsvc->findObject(path, h ).isSuccess() ) return h;
    00118     else                                          return 0;
    00119   }
    00120   static IProfile1D*
    00121   profile1D
    00122   ( IHistogramSvc*     hsvc ,
    00123     const std::string& path )
    00124   {
    00125     IProfile1D* h = 0 ;
    00126     if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
    00127     return 0 ;
    00128   }
    00129   static IProfile2D*
    00130   profile2D
    00131   ( IHistogramSvc*     hsvc ,
    00132     const std::string& path )
    00133   {
    00134     IProfile2D* h = 0 ;
    00135     if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
    00136     return 0 ;
    00137   }
    00138 // Array support
    00139 private:
    00140   template <class T>
    00141   static Py_ssize_t Array_length( PyObject* self ) {
    00142 #if PY_VERSION_HEX < 0x02050000
    00143     const
    00144 #endif
    00145     char* buf = 0;
    00146     Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
    00147     return size/sizeof(T);
    00148   }
    00149
    00150   template <class T> static PyObject* toPython(T* o) { return 0; }
    00151   static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
    00152   static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
    00153   static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
    00154   static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
    00155   static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
    00156   static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
    00157
    00158   template <class T>
    00159   static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
    00160 #if PY_VERSION_HEX < 0x02050000
    00161     const
    00162 #endif
    00163     char* buf = 0;
    00164     Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
    00165     if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
    00166        PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
    00167        return 0;
    00168     }
    00169     return toPython((T*)buf + idx);
    00170   }
    00171
    00172 public:
    00173   template <class T>
    00174   static PyObject* toArray( T* ptr, Py_ssize_t size ) {
    00175    static PyTypeObject      type = PyBuffer_Type;
    00176    static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
    00177 #if PY_VERSION_HEX < 0x02050000
    00178    meth.sq_item         = (intargfunc) &Array_item<T>;
    00179    meth.sq_length       = (inquiry) &Array_length<T>;
    00180 #else
    00181    meth.sq_item         = (ssizeargfunc) &Array_item<T>;
    00182    meth.sq_length       = (lenfunc) &Array_length<T>;
    00183 #endif
    00184    type.tp_as_sequence  = &meth;
    00185    PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
    00186    Py_INCREF( &type );
    00187    buf->ob_type = &type;
    00188    return buf;
    00189   }
    00190   static PyObject* toIntArray    ( void* ptr, Py_ssize_t size ) { return toArray( (int*)    ptr , size ); }
    00191   static PyObject* toShortArray  ( void* ptr, Py_ssize_t size ) { return toArray( (short*)  ptr , size ); }
    00192   static PyObject* toFloatArray  ( void* ptr, Py_ssize_t size ) { return toArray( (float*)  ptr , size ); }
    00193   static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
    00194
    00195   template <class T>
    00196   static T* toAddress( std::vector<T>& v ) {
    00197     return &(*v.begin());
    00198   }
    00199   template <class T>
    00200   static T* toAddress( void * a) {
    00201     return (T*)a;
    00202   }
    00203
    00204 };
    00205
    00206 template PyObject* Helper::toArray(int*,Py_ssize_t);
    00207 template PyObject* Helper::toArray(char*,Py_ssize_t);
    00208 template PyObject* Helper::toArray(short*,Py_ssize_t);
    00209 template PyObject* Helper::toArray(float*,Py_ssize_t);
    00210 template PyObject* Helper::toArray(double*,Py_ssize_t);
    00211 template int* Helper::toAddress(std::vector<int>&);
    00212 template float* Helper::toAddress(std::vector<float>&);
    00213 template double* Helper::toAddress(std::vector<double>&);
    00214 template int* Helper::toAddress<int>(void*);
    00215 template float* Helper::toAddress<float>(void*);
    00216 template double* Helper::toAddress<double>(void*);
    00217
    00218 } // namespace GaudiPython
    00219
    00220 #endif // !GAUDIPYTHON_HELPERS_H