Algorithm.h
Go to the documentation of this file.
1 #ifndef GAUDIPYTHON_ALGORITHM_H
2 #define GAUDIPYTHON_ALGORITHM_H
3 // ============================================================================
4 // Include Files
5 // ============================================================================
6 // Python
7 // ============================================================================
8 #include "Python.h"
9 // ============================================================================
10 // GaudiKernel
11 // ============================================================================
12 #include "GaudiKernel/Algorithm.h"
13 #include "GaudiKernel/Bootstrap.h"
14 // ============================================================================
15 // GaudiPython
16 // ============================================================================
17 #include "GaudiPython/GaudiPython.h"
18 // ============================================================================
19 
20 namespace GaudiPython
21 {
23  GAUDI_API StatusCode call_python_method ( PyObject* self , const char* method ) ;
24 }
25 
26 namespace GaudiPython
27 {
28 
32  class GAUDI_API PyAlgorithm : public Algorithm
33  {
34  public:
39  PyAlgorithm
40  ( PyObject* self ,
41  const std::string& name ) ;
42  public:
44  StatusCode start () ;
45  StatusCode beginRun () ;
46  StatusCode endRun () ;
47  StatusCode execute () ;
48  StatusCode stop () ;
49  StatusCode finalize () ;
50  IAlgorithm* myself() { return this; }
51  private:
52  PyObject* m_self;
53  };
54  // ==========================================================================
61  template <class ALGORITHM>
62  class GAUDI_API PyAlg : public ALGORITHM
63  {
64  // ========================================================================
65  public:
66  // ========================================================================
71  PyAlg
72  ( PyObject* self ,
73  const std::string& name )
74  : ALGORITHM ( name , Gaudi::svcLocator() )
75  , m_self ( self )
76  {
77  // the printout of actual type for embedded algorithm has no sense
78  this->setProperty ( "TypePrint" , "false" ) ;
79  this->setProperty ( "StatPrint" , "true" ) ;
80  // The owner of the Algorithm is Python (as creator) therefore
81  // it should not be deleted by Gaudi (added an extra addRef()).
82  this->addRef() ;
83  this->addRef() ;
84  }
85  // ========================================================================
86  protected:
87  // ========================================================================
89  ~PyAlg() override = default; // virtual destructor
91  PyObject* _obj() const { return m_self ; } // get the object
92  // ========================================================================
93  public:
94  // ========================================================================
95  StatusCode initialize () override
96  { return GaudiPython::call_python_method ( m_self , "initialize" ) ; }
97  StatusCode start () override
98  { return GaudiPython::call_python_method ( m_self , "start" ) ; }
99  StatusCode beginRun () override
100  { return GaudiPython::call_python_method ( m_self , "beginRun" ) ; }
101  StatusCode endRun () override
102  { return GaudiPython::call_python_method ( m_self , "endRun" ) ; }
103  StatusCode execute () override
104  { return GaudiPython::call_python_method ( m_self , "execute" ) ; }
105  StatusCode stop () override
106  { return GaudiPython::call_python_method ( m_self , "stop" ) ; }
107  StatusCode finalize () override
108  { return GaudiPython::call_python_method ( m_self , "finalize" ) ; }
109  // ========================================================================
110  virtual IAlgorithm* ialgorithm () { return this ; }
111  virtual IProperty* iproperty () { return this ; }
112  // ========================================================================
113  // preserve the existing methods
114  virtual StatusCode initialize_ () { return ALGORITHM::initialize () ; }
115  virtual StatusCode finalize_ () { return ALGORITHM::finalize () ; }
116  // ========================================================================
117  private:
118  // ========================================================================
120  PyAlg() = delete;
122  PyAlg ( const PyAlg& ) = delete;
124  PyAlg& operator=( const PyAlg& ) = delete;
125  // ========================================================================
126  private:
127  // ========================================================================
129  PyObject* m_self; // "shadow" python class
130  // ========================================================================
131  } ;
132  // ==========================================================================
133 } // end of namespace GaudiPython
134 // ============================================================================
135 // The END
136 // ============================================================================
137 #endif // GAUDIPYTHON_ALGORITHM_H
138 // ============================================================================
def initialize()
Definition: AnalysisTest.py:12
#define GAUDI_API
Definition: Kernel.h:107
GAUDI_API ISvcLocator * svcLocator()
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:255
GAUDI_API StatusCode call_python_method(PyObject *self, const char *method)
call the python method
Definition: Algorithm.cpp:29
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:19
GaudiPython.h GaudiPython/GaudiPython.h.
Definition: AlgDecorators.h:37
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:61
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
Helper functions to set/get the application return code.
Definition: __init__.py:1
tuple start
Definition: IOTest.py:88