The Gaudi Framework  v30r4 (9b837755)
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 
8 #include "GaudiKernel/IAlgTool.h"
13 #include "GaudiKernel/IToolSvc.h"
14 #include "GaudiKernel/Property.h"
15 
16 // FIXME: (MCl) workaround for ROOT-5850
17 namespace AIDA
18 {
19  class IHistogram1D;
20  class IHistogram2D;
21  class IHistogram3D;
22  class IProfile1D;
23  class IProfile2D;
24 }
25 
26 #if PY_VERSION_HEX < 0x02050000
27 // Note (MCl):
28 // In Python 2.5, all the functions working with lenghts use the type PySsize_t
29 // instead of int. The also changed the name of the typedefs for those functions.
30 // Here we use:
31 // intargfunc -> ssizeargfunc
32 // inquiry -> lenfunc
33 //
35 typedef int Py_ssize_t;
36 #endif
37 
38 // the following is done instead of including Python.h becuase there is
39 // a clash with codecs.h defined in Python and the one in VC++ 7.1
40 // struct _object;
41 // typedef _object PyObject;
42 // extern "C" {
43 // PyObject* PyBuffer_FromMemory( void *ptr, int size);
44 //}
45 
49 namespace GaudiPython
50 {
51 
52  struct Helper {
53  // This is a number of static functions to overcome the current problem with PyLCGDict that
54  // does not allow to return instances of complex objects by calling arguments
55  // (reference to pointers)
56  Helper() {}
57  // Provided for backward compatibility
58  static IService* service( ISvcLocator* svcloc, const std::string& name, bool createif = false )
59  {
60  return svcloc->service( name, createif ).get();
61  }
62  // Provided for backward compatibility
63  static IAlgorithm* algorithm( IAlgManager* algmgr, const std::string& name, const bool createIf = false )
64  {
65  return algmgr->algorithm( name, createIf ).get();
66  }
67  // ==========================================================================
69  {
70  DataObject* o;
71  return dpsvc->retrieveObject( path, o ).isSuccess() ? o : nullptr;
72  }
73  // ==========================================================================
75  {
76  return dpsvc->registerObject( path, pObject );
77  }
78  // ==========================================================================
80  {
81  return dpsvc->unregisterObject( path );
82  }
83  // ==========================================================================
94  static GAUDI_API DataObject* findobject( IDataProviderSvc* dpsvc, const std::string& path );
95  // ==========================================================================
107  static GAUDI_API DataObject* getobject( IDataProviderSvc* dpsvc, const std::string& path,
108  const bool retrieve = true, const bool disableDoD = false );
109  // ==========================================================================
110  static IAlgTool* tool( IToolSvc* toolsvc, const std::string& type, const std::string& name, IInterface* p,
111  bool cif )
112  {
113  IAlgTool* o;
114  return toolsvc->retrieve( type, name, IAlgTool::interfaceID(), o, p, cif ).isSuccess() ? o : nullptr;
115  }
116  static long loadDynamicLib( const std::string& name )
117  {
118  void* h;
119  return System::loadDynamicLib( name, &h );
120  }
121  static IHistogram1D* histo1D( IHistogramSvc* hsvc, const std::string& path )
122  {
123  IHistogram1D* h;
124  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
125  }
126  static IHistogram2D* histo2D( IHistogramSvc* hsvc, const std::string& path )
127  {
128  IHistogram2D* h;
129  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
130  }
131  static IHistogram3D* histo3D( IHistogramSvc* hsvc, const std::string& path )
132  {
133  IHistogram3D* h;
134  return ( hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
135  }
136  static IProfile1D* profile1D( IHistogramSvc* hsvc, const std::string& path )
137  {
138  IProfile1D* h = 0;
139  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
140  }
141  static IProfile2D* profile2D( IHistogramSvc* hsvc, const std::string& path )
142  {
143  IProfile2D* h = 0;
144  return ( hsvc && hsvc->findObject( path, h ).isSuccess() ) ? h : nullptr;
145  }
146 
147  // Array support
148  private:
149  template <class T>
150  static Py_ssize_t Array_length( PyObject* self )
151  {
152 #if PY_VERSION_HEX < 0x02050000
153  const
154 #endif
155  char* buf = 0;
156  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
157  return size / sizeof( T );
158  }
159 
160  template <class T>
161  static PyObject* toPython( T* /*o*/ )
162  {
163  return 0;
164  }
165  static PyObject* toPython( int* o ) { return PyInt_FromLong( (long)*o ); }
166  static PyObject* toPython( short* o ) { return PyInt_FromLong( (long)*o ); }
167  static PyObject* toPython( char* o ) { return PyInt_FromLong( (long)*o ); }
168  static PyObject* toPython( long* o ) { return PyInt_FromLong( *o ); }
169  static PyObject* toPython( float* o ) { return PyFloat_FromDouble( (double)*o ); }
170  static PyObject* toPython( double* o ) { return PyFloat_FromDouble( *o ); }
171 
172  template <class T>
173  static PyObject* Array_item( PyObject* self, Py_ssize_t idx )
174  {
175 #if PY_VERSION_HEX < 0x02050000
176  const
177 #endif
178  char* buf = nullptr;
179  Py_ssize_t size = ( *( self->ob_type->tp_as_buffer->bf_getcharbuffer ) )( self, 0, &buf );
180  if ( idx < 0 || idx >= size / int( sizeof( T ) ) ) {
181  PyErr_SetString( PyExc_IndexError, "buffer index out of range" );
182  return nullptr;
183  }
184  return toPython( (T*)buf + idx );
185  }
186 
187  public:
188  template <class T>
189  static PyObject* toArray( T* ptr, Py_ssize_t size )
190  {
191  static PyTypeObject type = PyBuffer_Type;
192  static PySequenceMethods meth = *( PyBuffer_Type.tp_as_sequence );
193 #if PY_VERSION_HEX < 0x02050000
194  meth.sq_item = (intargfunc)&Array_item<T>;
195  meth.sq_length = (inquiry)&Array_length<T>;
196 #else
197  meth.sq_item = (ssizeargfunc)&Array_item<T>;
198  meth.sq_length = (lenfunc)&Array_length<T>;
199 #endif
200  type.tp_as_sequence = &meth;
201  PyObject* buf = PyBuffer_FromReadWriteMemory( ptr, size * sizeof( T ) );
202  buf->ob_type = &type;
203  Py_INCREF( buf->ob_type );
204  return buf;
205  }
206  static PyObject* toIntArray( void* ptr, Py_ssize_t size ) { return toArray( (int*)ptr, size ); }
207  static PyObject* toShortArray( void* ptr, Py_ssize_t size ) { return toArray( (short*)ptr, size ); }
208  static PyObject* toFloatArray( void* ptr, Py_ssize_t size ) { return toArray( (float*)ptr, size ); }
209  static PyObject* toDoubleArray( void* ptr, Py_ssize_t size ) { return toArray( (double*)ptr, size ); }
210 
211  template <class T>
212  static T* toAddress( std::vector<T>& v )
213  {
214  return v.data();
215  }
216  template <class T>
217  static T* toAddress( void* a )
218  {
219  return (T*)a;
220  }
221 
222  // FIXME: (MCl) workaround for ROOT-6028, ROOT-6054, ROOT-6073
224  {
225  return p.fromString( s );
226  }
227  };
228 
229  template PyObject* Helper::toArray( int*, Py_ssize_t );
230  template PyObject* Helper::toArray( char*, Py_ssize_t );
231  template PyObject* Helper::toArray( short*, Py_ssize_t );
232  template PyObject* Helper::toArray( float*, Py_ssize_t );
233  template PyObject* Helper::toArray( double*, Py_ssize_t );
234  template int* Helper::toAddress( std::vector<int>& );
235  template float* Helper::toAddress( std::vector<float>& );
236  template double* Helper::toAddress( std::vector<double>& );
237  template int* Helper::toAddress<int>( void* );
238  template float* Helper::toAddress<float>( void* );
239  template double* Helper::toAddress<double>( void* );
240 
241 } // namespace GaudiPython
242 
243 #endif // !GAUDIPYTHON_HELPERS_H
virtual SmartIF< IAlgorithm > & algorithm(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
The interface implemented by the IToolSvc base class.
Definition: IToolSvc.h:19
static PyObject * toPython(long *o)
Definition: Helpers.h:168
virtual StatusCode retrieve(const std::string &type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent=0, bool createIf=true)=0
Retrieve tool with tool dependent part of the name automatically assigned.
static IAlgTool * tool(IToolSvc *toolsvc, const std::string &type, const std::string &name, IInterface *p, bool cif)
Definition: Helpers.h:110
static PyObject * toDoubleArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:209
int Py_ssize_t
For compatibility with Python 2.4 and 2.5.
Definition: Helpers.h:35
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
static PyObject * toIntArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:206
static PyObject * toPython(double *o)
Definition: Helpers.h:170
static PyObject * toPython(int *o)
Definition: Helpers.h:165
static PyObject * toArray(T *ptr, Py_ssize_t size)
Definition: Helpers.h:189
static IProfile2D * profile2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:141
bool isSuccess() const
Definition: StatusCode.h:287
static PyObject * toPython(T *)
Definition: Helpers.h:161
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
Definition: IAlgManager.h:27
static PyObject * Array_item(PyObject *self, Py_ssize_t idx)
Definition: Helpers.h:173
static PyObject * toPython(float *o)
Definition: Helpers.h:169
Data provider interface definition.
static PyObject * toFloatArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:208
GaudiKernel.
Definition: Fill.h:10
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
virtual StatusCode fromString(const std::string &value)=0
string -> value
STL class.
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:82
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
static StatusCode setPropertyFromString(Gaudi::Details::PropertyBase &p, const std::string &s)
Definition: Helpers.h:223
T data(T...args)
static IHistogram3D * histo3D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:131
static long loadDynamicLib(const std::string &name)
Definition: Helpers.h:116
static T * toAddress(void *a)
Definition: Helpers.h:217
General service interface definition.
Definition: IService.h:18
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
Definition of the basic interface.
Definition: IInterface.h:277
static IAlgorithm * algorithm(IAlgManager *algmgr, const std::string &name, const bool createIf=false)
Definition: Helpers.h:63
Definition of the IHistogramSvc interface class.
Definition: IHistogramSvc.h:47
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:34
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
GaudiPython.h GaudiPython/GaudiPython.h.
Definition: AlgDecorators.h:37
static IHistogram2D * histo2D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:126
static StatusCode unregisterObject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:79
StatusCode registerObject(boost::string_ref fullPath, DataObject *pObject)
Register object with the data store.
virtual StatusCode unregisterObject(boost::string_ref fullPath)=0
Unregister object from the data store.
static IService * service(ISvcLocator *svcloc, const std::string &name, bool createif=false)
Definition: Helpers.h:58
STL class.
static PyObject * toShortArray(void *ptr, Py_ssize_t size)
Definition: Helpers.h:207
static DataObject * dataobject(IDataProviderSvc *dpsvc, const std::string &path)
Definition: Helpers.h:68
virtual StatusCode findObject(const std::string &fullPath, AIDA::IHistogram1D *&h1dObj)=0
def toArray(typ)
Definition: Bindings.py:76
static Py_ssize_t Array_length(PyObject *self)
Definition: Helpers.h:150
The interface implemented by the AlgTool base class.
Definition: IAlgTool.h:23
string s
Definition: gaudirun.py:253
static T * toAddress(std::vector< T > &v)
Definition: Helpers.h:212
static IProfile1D * profile1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:136
#define GAUDI_API
Definition: Kernel.h:71
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
static IHistogram1D * histo1D(IHistogramSvc *hsvc, const std::string &path)
Definition: Helpers.h:121
static const InterfaceID & interfaceID()
Return an instance of InterfaceID identifying the interface.
Definition: IInterface.h:287
static PyObject * toPython(char *o)
Definition: Helpers.h:167
static PyObject * toPython(short *o)
Definition: Helpers.h:166
static StatusCode registerObject(IDataProviderSvc *dpsvc, const std::string &path, DataObject *pObject)
Definition: Helpers.h:74
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:154