7#ifndef STOREGATEBINDINGS_SGPYDATAMODEL_H
8#define STOREGATEBINDINGS_SGPYDATAMODEL_H 1
16#include "GaudiKernel/Bootstrap.h"
17#include "GaudiKernel/ClassID.h"
18#include "GaudiKernel/DataObject.h"
19#include "GaudiKernel/IClassIDSvc.h"
32#include "TClassEdit.h"
36#include "TMethodCall.h"
40#ifdef ATLAS_HAVE_CPYCPPYY
41# include "CPyCppyy/PyException.h"
44#include <unordered_map>
52namespace SG {
struct PyProxyMgr; }
58 std::string::size_type
59 cxx_replace(std::string& s,
60 const std::string& from,
61 const std::string& to)
63 std::string::size_type
cnt(std::string::npos);
65 if(from != to && !from.empty()) {
66 std::string::size_type pos1(0);
67 std::string::size_type pos2(0);
68 const std::string::size_type from_len(from.size());
69 const std::string::size_type to_len(to.size());
72 while((pos1 =
s.find(from, pos2)) != std::string::npos) {
73 s.replace(pos1, from_len, to);
139 bool isConst =
true)
override;
150 virtual void*
cast (
const std::type_info&
tinfo,
152 bool isConst =
true)
override;
162 virtual const std::type_info&
tinfo()
const override
164 return m_bib->typeinfo();
178 virtual void lock()
override;
205 typedef std::unordered_map<StoreGateSvc*,SG::PyProxyDict*>
PyProxyMap_t;
209 typedef std::unordered_map<CLID, std::string>
ClidMap_t;
226 if ( NULL ==
clid ) {
228 if (
id == CLID_NULL ) {
231 clid = PyLong_FromLong(
id);
248 cpp_tp).isSuccess()) {
255 tp = PyUnicode_FromString(cpp_tp.c_str());
282 m_clidSvc->getIDOfTypeName(tn,
id).ignore();
283 if (
id == CLID_NULL ) {
288 m_clidSvc->getIDOfTypeName(aliasstr,
id).ignore();
291 if (
id == CLID_NULL) {
303 m_clidSvc->getIDOfTypeInfoName(tn,
id).ignore();
304 if (
id == CLID_NULL ) {
323 typedef ClidMap_t::iterator Itr_t;
326 return i->second.c_str();
331 ::cxx_replace(tname,
"basic_string<char> >",
"string>");
332 ::cxx_replace(tname,
"basic_string<char>",
"string");
336 std::string bibname =
"SG::BaseInfo<" + tname +
">";
337 TClass* bibcl = gROOT->GetClass (bibname.c_str());
339 TMethod* m = bibcl->GetMethodAny (
"baseinfo");
341 TMethodCall call (m);
353 const std::string moduleName =
"AthenaPython.Bindings";
354 PyObject* module = PyImport_ImportModule
355 (
const_cast<char*
>(moduleName.c_str()) );
356 if ( !module || !PyModule_Check(module) ) {
357 std::cerr <<
"SG::PyProxyDict WARNING: could not import module '"
358 << moduleName <<
"' !\n";
364 m_aliases = PyDict_GetItemString(PyModule_GetDict(module),
365 (
char*)
"_clid_typename_aliases");
372 std::cerr <<
"SG::PyProxyDict WARNING: could not retrieve the "
373 <<
"dictionary of aliases from '"
374 << moduleName <<
"' !\n";
409 unsigned int id_tmp = 0;
410 if (!PyArg_Parse( pyclid,
"I", &id_tmp )) {
416 if (pykey != Py_None) {
425 pyproxy = TPython::CPPInstance_FromVoidPtr((
void*)
proxy,
428 _SGPY_MSG(
"PyProxyDict::proxy(...)... [done]");
436 if ( !(obj = TPython::CPPInstance_FromVoidPtr((
void*)addr, klass)) ) {
437#ifdef ATLAS_HAVE_CPYCPPYY
438 throw CPyCppyy::PyException();
440 throw std::runtime_error (
"Python exception");
455 PyProxyMap_t::iterator i =
m_proxyMap.find(sg);
#define CPPInstance_ASVOIDPTR(o)
Provide an interface for finding inheritance information at run time.
Recursively separate out template arguments in a C++ class name.
uint32_t CLID
The Class ID type.
Define macros for attributes used to control the static checker.
#define ATLAS_NO_CHECK_FILE_THREAD_SAFETY
T * cast(SG::IRegisterTransient *irt=0, bool isConst=true)
Return the contents of the DataBucket, converted to type T.
a simple interface to interact with the Reflex dictionaries and abstract/workaround a few ROOT bugs.
The non-template portion of the BaseInfo implementation.
Interface for registering a transient object in t2p map.
virtual ~PyDataBucket() override
Destructor.
PyDataBucket(PyObject *obj, CLID clid)
Constructor.
virtual void * object() override
Return the held object.
PyObject * m_pyObj
Pointer to the held pyroot object (or 'regular' PyObject).
virtual const CLID & clID() const override
Retrieve reference to class definition structure.
virtual void * cast(CLID clid, IRegisterTransient *itr=0, bool isConst=true) override
Return the contents of the DataBucket, converted to type given by clid.
virtual void lock() override
If the held object derives from ILockable, call lock() on it.
CLID m_clid
The class ID of the wrapped object.
virtual void relinquish() override
Give up ownership of the DataBucket contents.
virtual const std::type_info & tinfo() const override
Return the type_info for the stored object.
const SG::BaseInfoBase * m_bib
pointer to the SG::BaseInfoBase structure holding the converter functions for objects held by StoreGa...
The Athena Transient Store API.
std::pair< std::string, bool > PyGetString(PyObject *s)
Convert python string -> C++ string for py2 and py3.
a python front-end to the IProxyDict interface PyProxyDict encapsulates getting python objects from t...
PyObject * proxy(PyObject *pyclid, PyObject *pykey)
PyObject * newPyDataObject(const char *klass, void *addr=0)
PyProxyDict(StoreGateSvc *sgSvc)
CLID clid_from_tid(PyObject *tp)
PyObject * importDictAliases()
import the dictionary of aliases from a well known location
static PyProxyMgr & instance()
PyObject * pytp(CLID clid)
returns a borrowed reference
std::unordered_map< CLID, std::string > ClidMap_t
a dictionary of CLID -> reflex typename
CxxUtils::ClassName::Rules m_dv_rules
PyObject * m_aliases
a dictionary of "typedef'ed typename" -> "typename"
const char * load_type(CLID id)
ensure everything has been loaded for the clid id (classid, reflex dict, baseinfobase,...
SG::PyProxyDict * pyproxy(StoreGateSvc *sg)
IDictLoaderSvc * m_dictSvc
std::unordered_map< StoreGateSvc *, SG::PyProxyDict * > PyProxyMap_t
PyObject * pyclid(PyObject *tp)
returns a borrowed reference
PyObject * pytp(PyObject *clid)
returns a borrowed reference
std::string typename_from_python(PyObject *tp) const
Extract a string from a python object and interpret it as a type name, performing any needed massagin...
PyObject * m_clids
a dictionary of 'typename' -> CLID (and reverse CLID->'typename')