| # Gaudi Framework, version v20r2
Generated: 18 Jul 2008

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- Search for |

- File List
- File Members

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