ATLAS Offline Software
PyReverseProxy.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef PYKERNEL_PYREVERSEPROXY_H
6 #define PYKERNEL_PYREVERSEPROXY_H
7 
14 #ifdef _POSIX_C_SOURCE
15 # undef _POSIX_C_SOURCE
16 #endif
17 #ifdef _XOPEN_SOURCE
18 # undef _XOPEN_SOURCE
19 #endif
20 #include "Python.h"
21 #include <unordered_map>
22 #include <string>
25 
26 
28 {
31  {
33  s_proxyMap.erase(m_key);
34  }
35 
37  void setFunc (PyObject * func)
38  {
40  Py_DECREF(m_func);
41  m_func = func;
42  Py_INCREF(m_func);
43  }
44 
45  // getter
46  void getObj (void *& obj)
47  {
48  obj = m_obj;
49  }
50 
51  PyObject * toPyObj (void * obj)
52  {
54  m_obj = obj;
55  return PyObject_CallObject(m_func,NULL);
56  }
57 
59  static PyReverseProxy * getProxy(const std::string & key)
60  {
62  PyReverseProxy* & prox = s_proxyMap[key];
63  if (!prox) {
64  prox = new PyReverseProxy(key);
65  }
66  return prox;
67  }
68 
69 private:
72  {}
73 
75  PyReverseProxy(const std::string &key)
76  : m_key(key), m_obj(0), m_func(0)
77  {}
78 
80  std::string m_key;
81 
83  void * m_obj;
84 
87 
90  static std::unordered_map<std::string, PyReverseProxy *> s_proxyMap ATLAS_THREAD_SAFE;
91 };
92 
93 #endif
94 
95 
PyReverseProxy::toPyObj
PyObject * toPyObj(void *obj)
Definition: PyReverseProxy.h:51
PyReverseProxy::m_key
std::string m_key
key
Definition: PyReverseProxy.h:80
PyReverseProxy
A utility class to convert a C++ object to a PyObject.
Definition: PyReverseProxy.h:28
RootUtils::PyGILStateEnsure
Definition: PyAthenaGILStateEnsure.h:20
PyReverseProxy::setFunc
void setFunc(PyObject *func)
setter
Definition: PyReverseProxy.h:37
PyReverseProxy::PyReverseProxy
PyReverseProxy()
default constructor : never used
Definition: PyReverseProxy.h:71
PyReverseProxy::getProxy
static PyReverseProxy * getProxy(const std::string &key)
factory method
Definition: PyReverseProxy.h:59
PyReverseProxy::ATLAS_THREAD_SAFE
static std::unordered_map< std::string, PyReverseProxy * > s_proxyMap ATLAS_THREAD_SAFE
proxy map protected by the python GIL.
Definition: PyReverseProxy.h:90
PyReverseProxy::getObj
void getObj(void *&obj)
Definition: PyReverseProxy.h:46
PyReverseProxy::m_obj
void * m_obj
C++ obj.
Definition: PyReverseProxy.h:83
PyAthenaGILStateEnsure.h
checker_macros.h
Define macros for attributes used to control the static checker.
python.PyAthena.obj
obj
Definition: PyAthena.py:135
PyObject
_object PyObject
Definition: IPyComponent.h:26
PyReverseProxy::~PyReverseProxy
~PyReverseProxy()
destructor
Definition: PyReverseProxy.h:30
PyReverseProxy::m_func
PyObject * m_func
python code fragment to convert C++ obj to PyObj
Definition: PyReverseProxy.h:86
PyReverseProxy::PyReverseProxy
PyReverseProxy(const std::string &key)
constructor
Definition: PyReverseProxy.h:75
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37