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