Belle II Software  release-08-01-10
PyModule.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #pragma once
10 
11 #include <boost/python/call_method.hpp>
12 #include <framework/core/Module.h>
13 
14 namespace Belle2 {
29  class PyModule : public Module {
30  public:
32  explicit PyModule(PyObject* p):
33  Module(),
34  m_self(p)
35  {
36  setName(p->ob_type->tp_name);
37  setType("PyModule");
38  }
39 
41  PyModule(PyObject* p, const Module& m):
42  Module(m),
43  m_self(p)
44  {
45  setName(p->ob_type->tp_name);
46  setType("PyModule");
47  }
48 
49 
50  /* reimplement all virtual functions of base class and call the corresponding method in the python class
51  * note that all methods must be registered in Module::exposePythonAPI()
52  */
53 
54  virtual void initialize() override final
55  {
56  boost::python::call_method<void>(m_self, "initialize");
57  }
58 
59  virtual void beginRun() override final
60  {
61  boost::python::call_method<void>(m_self, "beginRun");
62  }
63 
64  virtual void event() override final
65  {
66  boost::python::call_method<void>(m_self, "event");
67  }
68 
69  virtual void endRun() override final
70  {
71  boost::python::call_method<void>(m_self, "endRun");
72  }
73 
74  virtual void terminate() override final
75  {
76  boost::python::call_method<void>(m_self, "terminate");
77  }
78 
79  private:
81 
82  virtual void def_initialize() override final
83  {
84  // Call empty base implementation if initialize() is not overriden from Python.
86  }
87 
88  virtual void def_beginRun() override final
89  {
90  // Call empty base implementation if beginRun() is not overriden from Python.
92  }
93 
94  virtual void def_event() override final
95  {
96  // Call empty base implementation if event() is not overriden from Python.
97  Module::event();
98  }
99 
100  virtual void def_endRun() override final
101  {
102  // Call empty base implementation if endRun() is not overriden from Python.
103  Module::endRun();
104  }
105 
106  virtual void def_terminate() override final
107  {
108  // Call empty base implementation if terminate() is not overriden from Python.
110  }
112 
113  PyObject* m_self;
114  };
116 }
Base class for Modules.
Definition: Module.h:72
virtual void event()
This method is the core of the module.
Definition: Module.h:157
virtual void initialize()
Initialize the Module.
Definition: Module.h:109
virtual void beginRun()
Called when entering a new run.
Definition: Module.h:147
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:166
void setName(const std::string &name)
Set the name of the module.
Definition: Module.h:214
void setType(const std::string &type)
Set the module type.
Definition: Module.cc:48
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:176
Python wrapper for Belle2::Module.
Definition: PyModule.h:29
virtual void def_event() override final
Wrapper method for the virtual function event() that has the implementation to be used in a call from...
Definition: PyModule.h:94
virtual void endRun() override final
This method is called if the current run ends.
Definition: PyModule.h:69
virtual void def_beginRun() override final
Wrapper method for the virtual function beginRun() that has the implementation to be used in a call f...
Definition: PyModule.h:88
virtual void def_terminate() override final
Wrapper method for the virtual function terminate() that has the implementation to be used in a call ...
Definition: PyModule.h:106
PyModule(PyObject *p, const Module &m)
copy constructor
Definition: PyModule.h:41
virtual void initialize() override final
Initialize the Module.
Definition: PyModule.h:54
virtual void def_initialize() override final
default implementation used when Python module doesn't supply its own
Definition: PyModule.h:82
PyObject * m_self
the actual python module
Definition: PyModule.h:113
virtual void terminate() override final
This method is called at the end of the event processing.
Definition: PyModule.h:74
PyModule(PyObject *p)
constructor
Definition: PyModule.h:32
virtual void beginRun() override final
Called when entering a new run.
Definition: PyModule.h:59
virtual void def_endRun() override final
This method can receive that the current run ends as a call from the Python side.
Definition: PyModule.h:100
virtual void event() override final
This method is the core of the module.
Definition: PyModule.h:64
Abstract base class for different kinds of events.