ROOT
master
Reference Guide
Loading...
Searching...
No Matches
DispatchPtr.h
Go to the documentation of this file.
1
#ifndef CPYRT_DISPATCHPTR_H
2
#define CPYRT_DISPATCHPTR_H
3
4
//////////////////////////////////////////////////////////////////////////////
5
// //
6
// DispatchPtr //
7
// //
8
// Smart pointer for reference management and C++ instance tracking when //
9
// cross-inheriting. The carried pointer is always expected to be derived //
10
// from CPPInstance, and the DispatchPtr to be embedded in the C++ instance //
11
// derived dispatcher to which it points (ownership is two-way; life-times //
12
// are equal). The C++ dispatcher then uses the DispatchPtr to call Python //
13
// functions for virtual methods. //
14
// //
15
//////////////////////////////////////////////////////////////////////////////
16
17
// Bindings
18
#include <Python.h>
19
#include "
cpyrt/CommonDefs.h
"
20
21
namespace
cppjit::cpyrt
{
22
class
PythonGILRAII
{
23
PyGILState_STATE
state
;
24
25
public
:
26
PythonGILRAII
() :
state
(PyGILState_Ensure()) {}
27
~PythonGILRAII
() { PyGILState_Release(
state
); }
28
};
29
30
class
CPYRT_CLASS_EXTERN
DispatchPtr
{
31
public
:
32
// Default constructor: only ever created from C++, as by definition, creation
33
// from the Python side makes the relevant Python instance available. Calls to
34
// the default ctor happen eg. in STL containers. It is expected that the
35
// pointer to the Python object is filled in later, eg. through assign().
36
DispatchPtr
() : fPyHardRef(nullptr), fPyWeakRef(nullptr) {}
37
38
// Conversion constructor: called with C++ object construction when the
39
// PyObject is known (eg. when instantiating from Python), with pyobj the
40
// Python-side representation of the C++ object.
41
explicit
DispatchPtr
(
PyObject
* pyobj,
bool
strong =
false
);
42
43
// Copy constructor: only ever called from C++. The Python object needs to be
44
// copied, in case it has added state, and rebound to the new C++ instance.
45
DispatchPtr
(
const
DispatchPtr
& other,
void
* cppinst);
46
47
// Assignment: only ever called from C++. Similarly to the copy constructor,
48
// the Pythonb object needs to be copied and rebound.
49
DispatchPtr
& assign(
const
DispatchPtr
& other,
void
* cppinst);
50
51
// Do not otherwise allow straight copies/assignment.
52
DispatchPtr
(
DispatchPtr
&) =
delete
;
53
DispatchPtr
(
DispatchPtr
&&) =
delete
;
54
DispatchPtr
&
operator=
(
const
DispatchPtr
& other) =
delete
;
55
56
// lifetime is directly bound to the lifetime of the dispatcher object
57
~DispatchPtr
();
58
59
// either C++ owns the Python object through a reference count (on fPyHardRef)
60
// or Python owns the C++ object and we only have a weak reference (through
61
// fPyWeakRef)
62
void
PythonOwns();
63
void
CppOwns();
64
65
// access to underlying object: cast and dereferencing
66
operator
PyObject
*()
const
{
return
Get(); }
67
68
PyObject
*
operator->
()
const
{
return
Get(); }
69
70
private
:
71
PyObject
* Get(
bool
borrowed =
true
)
const
;
72
73
private
:
74
PyObject
*
fPyHardRef
;
75
PyObject
*
fPyWeakRef
;
76
};
77
78
}
// namespace cppjit::cpyrt
79
80
#endif
// !CPYRT_DISPATCHPTR_H
PyObject
_object PyObject
Definition
PyMethodBase.h:36
CommonDefs.h
CPYRT_CLASS_EXTERN
#define CPYRT_CLASS_EXTERN
Definition
CommonDefs.h:29
cppjit::cpyrt::DispatchPtr
Definition
DispatchPtr.h:30
cppjit::cpyrt::DispatchPtr::operator=
DispatchPtr & operator=(const DispatchPtr &other)=delete
cppjit::cpyrt::DispatchPtr::operator->
PyObject * operator->() const
Definition
DispatchPtr.h:68
cppjit::cpyrt::DispatchPtr::DispatchPtr
DispatchPtr()
Definition
DispatchPtr.h:36
cppjit::cpyrt::DispatchPtr::DispatchPtr
DispatchPtr(DispatchPtr &&)=delete
cppjit::cpyrt::DispatchPtr::DispatchPtr
DispatchPtr(DispatchPtr &)=delete
cppjit::cpyrt::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition
DispatchPtr.h:75
cppjit::cpyrt::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition
DispatchPtr.h:74
cppjit::cpyrt::PythonGILRAII
Definition
DispatchPtr.h:22
cppjit::cpyrt::PythonGILRAII::PythonGILRAII
PythonGILRAII()
Definition
DispatchPtr.h:26
cppjit::cpyrt::PythonGILRAII::state
PyGILState_STATE state
Definition
DispatchPtr.h:23
cppjit::cpyrt::PythonGILRAII::~PythonGILRAII
~PythonGILRAII()
Definition
DispatchPtr.h:27
cppjit::cpyrt
Definition
API.cxx:34
bindings
pyroot
cppjit
src
cpyrt
DispatchPtr.h
ROOTmaster - Reference Guide Generated on Tue Oct 6 2026 15:26:18 (GVA Time) using Doxygen 1.10.0