Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

Helpers.h

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

Generated at Mon Sep 17 2012 13:49:29 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004