ATLAS Offline Software
Loading...
Searching...
No Matches
SG::PyDataBucket Class Reference

Concrete DataBucket that holds the object via a void* and uses the Root dictionary to do conversions. More...

#include <SgPyDataModel.h>

Inheritance diagram for SG::PyDataBucket:
Collaboration diagram for SG::PyDataBucket:

Public Member Functions

 PyDataBucket (PyObject *obj, CLID clid)
 Constructor.
virtual ~PyDataBucket () override
 Destructor.
virtual void * object () override
 Return the held object.
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 * cast (const std::type_info &tinfo, IRegisterTransient *itr=0, bool isConst=true) override
 Return the contents of the DataBucket, converted to type given by std::type_info.
virtual const CLID & clID () const override
 Retrieve reference to class definition structure.
virtual const std::type_info & tinfo () const override
 Return the type_info for the stored object.
virtual void relinquish () override
 Give up ownership of the DataBucket contents.
virtual void lock () override
 If the held object derives from ILockable, call lock() on it.
template<class T>
T * cast (SG::IRegisterTransient *irt=0, bool isConst=true)
 Return the contents of the DataBucket, converted to type T.
virtual void * cast (CLID clid, const std::type_info &tinfo, SG::IRegisterTransient *irt=0, bool isConst=true)
 Return the contents of the DataBucket, converted to type given by clid.

Private Attributes

PyObject * m_pyObj
 Pointer to the held pyroot object (or 'regular' PyObject).
CLID m_clid
 The class ID of the wrapped object.
const SG::BaseInfoBase * m_bib
 pointer to the SG::BaseInfoBase structure holding the converter functions for objects held by StoreGate

Detailed Description

Concrete DataBucket that holds the object via a void* and uses the Root dictionary to do conversions.

A concrete DataBucket instance holds a pointer to an arbitrary object, and is able to further convert it to pointers to other types related by inheritance. This variant is used for PyAthena, where don't have the type available at compile time and thus cannot use templates. However, we know that we do have the Root dictionary available for the types, so we can use that information for the conversions.

Definition at line 96 of file SgPyDataModel.h.

Constructor & Destructor Documentation

◆ PyDataBucket()

SG::PyDataBucket::PyDataBucket ( PyObject * obj,
CLID clid )

Constructor.

Parameters
pyObjThe (pyroot) object proxy to hold.
clidThe class ID of the wrapped object.

Definition at line 43 of file SgPyDataModel.cxx.

44 :
46 m_pyObj( pyObj ),
47 m_clid ( clid ),
49 {
50 // prevent Python from sweeping the rug under our feet
51 Py_INCREF( pyObj );
52 }
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition BaseInfo.cxx:587
PyObject * m_pyObj
Pointer to the held pyroot object (or 'regular' PyObject).
CLID m_clid
The class ID of the wrapped object.
const SG::BaseInfoBase * m_bib
pointer to the SG::BaseInfoBase structure holding the converter functions for objects held by StoreGa...

◆ ~PyDataBucket()

virtual SG::PyDataBucket::~PyDataBucket ( )
inlineoverridevirtual

Destructor.

Definition at line 111 of file SgPyDataModel.h.

111 {
112 RootUtils::PyGILStateEnsure gil;
113 Py_DECREF( m_pyObj );
114 }

Member Function Documentation

◆ cast() [1/4]

virtual void * DataBucketBase::cast ( CLID clid,
const std::type_info & tinfo,
SG::IRegisterTransient * irt = 0,
bool isConst = true )
virtual

Return the contents of the DataBucket, converted to type given by clid.

Note that only derived->base conversions are allowed here.

Parameters
clidThe class ID to which to convert.
tinfoThe std::type_info of the type to which to convert.
irtTo be called if we make a new instance.
isConstTrue if the object being converted is regarded as const.

This allows the callee to choose whether to use clid or tinfo. By default, this uses type_info.

Reimplemented from DataBucketBase.

◆ cast() [2/4]

void * SG::PyDataBucket::cast ( CLID clid,
IRegisterTransient * itr = 0,
bool isConst = true )
overridevirtual

Return the contents of the DataBucket, converted to type given by clid.

Note that only derived->base conversions are allowed here.

Parameters
clidThe class ID to which to convert.
irtTo be called if we make a new instance.
isConstTrue if the object being converted is regarded as const.

Implements DataBucketBase.

Definition at line 54 of file SgPyDataModel.cxx.

57 {
58 RootUtils::PyGILStateEnsure gil;
59 // if requested type is same than myself ==> no conversion needed
60 if ( clid == m_clid ) {
61 return clid == PyCLID
62 ? m_pyObj
64 }
65 void* address = (m_clid == PyCLID)
66 ? (void*)m_pyObj
68
69 // try SG-based conversion functions
70 {
71 void* o = m_bib ? m_bib->cast(address, clid) : 0;
72 if ( o ) { return o; }
73 }
74
75 // try PyRoot based ones
76 PyObject* pytp = PyProxyMgr::instance().pytp(clid);
77 if ( !pytp ) {
78 PyErr_Format( PyExc_TypeError, "actual type of CLID %lu unknown",
79 (long unsigned int)clid );
80 return 0;
81 }
82
83 // this will be a conversion for a class instance only (see below:
84 // verified that only a CPPInstance is expected), so bind with cast
85 std::string pytpstr = RootUtils::PyGetString(pytp).first;
86 TClass* cls = TClass::GetClass (pytpstr.c_str());
87 if (!cls) {
88 PyErr_Format( PyExc_TypeError, "Can't find TClass for `%s'",
89 pytpstr.c_str() );
90 return 0;
91 }
92 TClass* act_class = cls->GetActualClass (address);
93 PyObject* value = TPython::CPPInstance_FromVoidPtr (address, act_class->GetName());
94
95 if ( value && TPython::CPPInstance_Check(value) ) {
96 return CPPInstance_ASVOIDPTR(value);
97 }
98 Py_XDECREF(value);
99#ifdef ATLAS_HAVE_CPYCPPYY
100 throw CPyCppyy::PyException();
101#else
102 throw std::runtime_error ("Python exception");
103#endif // ATLAS_HAVE_CPYCPPYY
104 return 0;
105 }
#define CPPInstance_ASVOIDPTR(o)
_object PyObject
const CLID PyCLID
std::pair< std::string, bool > PyGetString(PyObject *s)
Convert python string -> C++ string for py2 and py3.
Definition PyGetString.h:40
static PyProxyMgr & instance()
PyObject * pytp(PyObject *clid)
returns a borrowed reference

◆ cast() [3/4]

void * SG::PyDataBucket::cast ( const std::type_info & tinfo,
IRegisterTransient * itr = 0,
bool isConst = true )
overridevirtual

Return the contents of the DataBucket, converted to type given by std::type_info.

Note that only derived->base conversions are allowed here.

Parameters
tinfoThe std::type_info of the type to which to convert.
irtTo be called if we make a new instance.
isConstTrue if the object being converted is regarded as const.

Implements DataBucketBase.

Definition at line 107 of file SgPyDataModel.cxx.

110 {
111 RootUtils::PyGILStateEnsure gil;
112 // if regular PyObject, meaningless
113 if ( m_clid == PyCLID ) {
114 return 0;
115 }
116
117 // if requested type is same than myself ==> no conversion needed
118 TClass* tcls = objectIsA (m_pyObj);
119 if ( tcls && (tinfo == *(tcls->GetTypeInfo())) ) {
121 }
122 void* address = CPPInstance_ASVOIDPTR(m_pyObj);
123
124 // try SG-based conversion functions
125 {
126 void* o = m_bib ? m_bib->cast(address, tinfo) : 0;
127 if ( o ) { return o; }
128 }
129
130 // this will be a conversion for a class instance only (see below:
131 // verified that only a CPPInstance is expected), so bind with cast
132 TClass* clsnew = TClass::GetClass (tinfo);
133 if (!clsnew) {
134 PyErr_SetString
135 ( PyExc_RuntimeError,
136 "SG::PyDataBucket::cast() can't find TClass" );
137 return 0;
138 }
139 TClass* act_class = clsnew->GetActualClass (address);
140 PyObject* value = TPython::CPPInstance_FromVoidPtr (address, act_class->GetName());
141 PyErr_Clear();
142
143 if ( value && TPython::CPPInstance_Check(value) ) {
144 return CPPInstance_ASVOIDPTR(value);
145 }
146 Py_XDECREF(value);
147 //throw PyROOT::TPyException();
148 return 0;
149 }
virtual const std::type_info & tinfo() const override
Return the type_info for the stored object.
TClass * objectIsA(PyObject *obj)
Definition Utility.cxx:103

◆ cast() [4/4]

template<class T>
T * DataBucketBase::cast ( SG::IRegisterTransient * irt = 0,
bool isConst = true )

Return the contents of the DataBucket, converted to type T.

Note that only derived->base conversions are allowed here. T must have a valid Class ID for this to work.

Parameters
irtTo be called if we make a new instance.
isConstTrue if the object being converted is regarded as const.

◆ clID()

virtual const CLID & SG::PyDataBucket::clID ( ) const
inlineoverridevirtual

Retrieve reference to class definition structure.

Definition at line 157 of file SgPyDataModel.h.

157{ return m_clid; }

◆ lock()

void SG::PyDataBucket::lock ( )
overridevirtual

If the held object derives from ILockable, call lock() on it.

Implements DataBucketBase.

Definition at line 151 of file SgPyDataModel.cxx.

152 {
153 RootUtils::PyGILStateEnsure gil;
154 if (!m_pyObj) return;
155 if (!PyObject_HasAttrString (m_pyObj, "lock"))
156 return;
157 PyObject* lock = PyObject_GetAttrString (m_pyObj, "lock");
158 if (!lock) return;
159 if (PyCallable_Check (lock)) {
160 PyObject* ret = PyObject_CallObject (lock, NULL);
161 Py_DECREF (ret);
162 }
163 Py_DECREF (lock);
164 }
virtual void lock() override
If the held object derives from ILockable, call lock() on it.

◆ object()

virtual void * SG::PyDataBucket::object ( )
inlineoverridevirtual

Return the held object.

Implements DataBucketBase.

Definition at line 119 of file SgPyDataModel.h.

120 {
121 return m_clid != PyCLID
123 : m_pyObj;
124 }

◆ relinquish()

virtual void SG::PyDataBucket::relinquish ( )
inlineoverridevirtual

Give up ownership of the DataBucket contents.

Implements DataBucketBase.

Definition at line 170 of file SgPyDataModel.h.

171 {
172 Py_DECREF( m_pyObj );
173 }

◆ tinfo()

virtual const std::type_info & SG::PyDataBucket::tinfo ( ) const
inlineoverridevirtual

Return the type_info for the stored object.

Implements DataBucketBase.

Definition at line 162 of file SgPyDataModel.h.

163 {
164 return m_bib->typeinfo();
165 }

Member Data Documentation

◆ m_bib

const SG::BaseInfoBase* SG::PyDataBucket::m_bib
private

pointer to the SG::BaseInfoBase structure holding the converter functions for objects held by StoreGate

Definition at line 189 of file SgPyDataModel.h.

◆ m_clid

CLID SG::PyDataBucket::m_clid
private

The class ID of the wrapped object.

Definition at line 185 of file SgPyDataModel.h.

◆ m_pyObj

PyObject* SG::PyDataBucket::m_pyObj
private

Pointer to the held pyroot object (or 'regular' PyObject).

Definition at line 182 of file SgPyDataModel.h.


The documentation for this class was generated from the following files: