Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Helpers.h
Go to the documentation of this file.
1 #ifndef GAUDIPYTHON_HELPERS_H
2 #define GAUDIPYTHON_HELPERS_H
3 
4 #include "Python.h"
5 
6 // Framework
9 #include "GaudiKernel/IAlgTool.h"
10 
11 #if PY_VERSION_HEX < 0x02050000
12 // Note (MCl):
13 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
14 // instead of int. The also changed the name of the typedefs for those functions.
15 // Here we use:
16 // intargfunc -> ssizeargfunc
17 // inquiry -> lenfunc
18 //
20 typedef int Py_ssize_t;
21 #endif
22 
23 // the following is done instead of including Python.h becuase there is
24 // a clash with codecs.h defined in Python and the one in VC++ 7.1
25 //struct _object;
26 //typedef _object PyObject;
27 //extern "C" {
28 // PyObject* PyBuffer_FromMemory( void *ptr, int size);
29 //}
30 
34 namespace GaudiPython {
35 
36 struct Helper {
37  // This is a number of static functions to overcome the current problem with PyLCGDict that
38  // does not allow to return instances of complex objects by calling arguments
39  // (reference to pointers)
40  Helper() {}
41  // Provided for backward compatibility
42  static IService* service(ISvcLocator* svcloc, const std::string& name, bool createif=false) {
43  return svcloc->service(name, createif).get();
44  }
45  // Provided for backward compatibility
46  static IAlgorithm* algorithm
47  ( IAlgManager* algmgr ,
48  const std::string& name ,
49  const bool createIf = false )
50  {
51  return algmgr->algorithm(name, createIf).get();
52  }
53  // ==========================================================================
55  const std::string& path )
56  {
57  DataObject* o;
58  if ( dpsvc->retrieveObject(path,o).isSuccess() ) return o;
59  else return 0;
60  }
61  // ==========================================================================
73  ( IDataProviderSvc* dpsvc ,
74  const std::string& path ) ;
75  // ==========================================================================
88  ( IDataProviderSvc* dpsvc ,
89  const std::string& path ,
90  const bool retrieve = true ,
91  const bool disableDoD = false ) ;
92  // ==========================================================================
93  static IAlgTool* tool(IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p, bool cif ) {
94  IAlgTool* o;
95  if ( toolsvc->retrieve(type, name, IAlgTool::interfaceID(), o, p, cif).isSuccess() ) return o;
96  else return 0;
97  }
98  static long loadDynamicLib(const std::string& name) {
99  void* h;
100  return System::loadDynamicLib(name, &h);
101  }
102  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path ) {
103  IHistogram1D* h;
104  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
105  else return 0;
106  }
107  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path ) {
108  IHistogram2D* h;
109  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
110  else return 0;
111  }
112  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path ) {
113  IHistogram3D* h;
114  if ( hsvc->findObject(path, h ).isSuccess() ) return h;
115  else return 0;
116  }
117  static IProfile1D*
118  profile1D
119  ( IHistogramSvc* hsvc ,
120  const std::string& path )
121  {
122  IProfile1D* h = 0 ;
123  if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
124  return 0 ;
125  }
126  static IProfile2D*
127  profile2D
128  ( IHistogramSvc* hsvc ,
129  const std::string& path )
130  {
131  IProfile2D* h = 0 ;
132  if ( 0 != hsvc && hsvc->findObject ( path , h ).isSuccess() ) { return h ; }
133  return 0 ;
134  }
135 // Array support
136 private:
137  template <class T>
138  static Py_ssize_t Array_length( PyObject* self ) {
139 #if PY_VERSION_HEX < 0x02050000
140  const
141 #endif
142  char* buf = 0;
143  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
144  return size/sizeof(T);
145  }
146 
147  template <class T> static PyObject* toPython(T* o) { return 0; }
148  static PyObject* toPython(int* o) { return PyInt_FromLong((long)*o); }
149  static PyObject* toPython(short* o) { return PyInt_FromLong((long)*o); }
150  static PyObject* toPython(char* o) { return PyInt_FromLong((long)*o); }
151  static PyObject* toPython(long* o) { return PyInt_FromLong(*o); }
152  static PyObject* toPython(float* o) { return PyFloat_FromDouble((double)*o); }
153  static PyObject* toPython(double* o) { return PyFloat_FromDouble(*o); }
154 
155  template <class T>
156  static PyObject* Array_item( PyObject* self, Py_ssize_t idx ) {
157 #if PY_VERSION_HEX < 0x02050000
158  const
159 #endif
160  char* buf = 0;
161  Py_ssize_t size = (*(self->ob_type->tp_as_buffer->bf_getcharbuffer))( self, 0, &buf );
162  if ( idx < 0 || idx >= size/int(sizeof(T)) ) {
163  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
164  return 0;
165  }
166  return toPython((T*)buf + idx);
167  }
168 
169 public:
170  template <class T>
171  static PyObject* toArray( T* ptr, Py_ssize_t size ) {
172  static PyTypeObject type = PyBuffer_Type;
173  static PySequenceMethods meth = *(PyBuffer_Type.tp_as_sequence);
174 #if PY_VERSION_HEX < 0x02050000
175  meth.sq_item = (intargfunc) &Array_item<T>;
176  meth.sq_length = (inquiry) &Array_length<T>;
177 #else
178  meth.sq_item = (ssizeargfunc) &Array_item<T>;
179  meth.sq_length = (lenfunc) &Array_length<T>;
180 #endif
181  type.tp_as_sequence = &meth;
182  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size*sizeof(T) );
183  buf->ob_type = &type;
184  Py_INCREF( buf->ob_type );
185  return buf;
186  }
187  static PyObject* toIntArray ( void* ptr, Py_ssize_t size ) { return toArray( (int*) ptr , size ); }
188  static PyObject* toShortArray ( void* ptr, Py_ssize_t size ) { return toArray( (short*) ptr , size ); }
189  static PyObject* toFloatArray ( void* ptr, Py_ssize_t size ) { return toArray( (float*) ptr , size ); }
190  static PyObject* toDoubleArray ( void* ptr, Py_ssize_t size ) { return toArray( (double*) ptr , size ); }
191 
192  template <class T>
193  static T* toAddress( std::vector<T>& v ) {
194  return &(*v.begin());
195  }
196  template <class T>
197  static T* toAddress( void * a) {
198  return (T*)a;
199  }
200 
201 };
202 
203 template PyObject* Helper::toArray(int*,Py_ssize_t);
204 template PyObject* Helper::toArray(char*,Py_ssize_t);
205 template PyObject* Helper::toArray(short*,Py_ssize_t);
206 template PyObject* Helper::toArray(float*,Py_ssize_t);
207 template PyObject* Helper::toArray(double*,Py_ssize_t);
208 template int* Helper::toAddress(std::vector<int>&);
209 template float* Helper::toAddress(std::vector<float>&);
210 template double* Helper::toAddress(std::vector<double>&);
211 template int* Helper::toAddress<int>(void*);
212 template float* Helper::toAddress<float>(void*);
213 template double* Helper::toAddress<double>(void*);
214 
215 } // namespace GaudiPython
216 
217 #endif // !GAUDIPYTHON_HELPERS_H

Generated at Wed Nov 28 2012 12:17:12 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004