Gaudi Framework, version v21r4

Home   Generated: 7 Sep 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   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 // Array support
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 } // namespace GaudiPython
00181 
00182 #endif // !GAUDIPYTHON_HELPERS_H

Generated at Mon Sep 7 18:05:36 2009 for Gaudi Framework, version v21r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004