ROOT
master
Reference Guide
Loading...
Searching...
No Matches
DispatchPtr.h
Go to the documentation of this file.
1
#ifndef CPYCPPYY_DISPATCHPTR_H
2
#define CPYCPPYY_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 "
CPyCppyy/CommonDefs.h
"
19
20
21
namespace
CPyCppyy
{
22
23
class
CPYCPPYY_CLASS_EXTERN
DispatchPtr
{
24
public
:
25
// Default constructor: only ever created from C++, as by definition, creation
26
// from the Python side makes the relevant Python instance available. Calls to
27
// the default ctor happen eg. in STL containers. It is expected that the
28
// pointer to the Python object is filled in later, eg. through assign().
29
DispatchPtr
() : fPyHardRef(nullptr), fPyWeakRef(nullptr) {}
30
31
// Conversion constructor: called with C++ object construction when the PyObject
32
// is known (eg. when instantiating from Python), with pyobj the Python-side
33
// representation of the C++ object.
34
explicit
DispatchPtr
(
PyObject
* pyobj,
bool
strong =
false
);
35
36
// Copy constructor: only ever called from C++. The Python object needs to be
37
// copied, in case it has added state, and rebound to the new C++ instance.
38
DispatchPtr
(
const
DispatchPtr
& other,
void
* cppinst);
39
40
// Assignment: only ever called from C++. Similarly to the copy constructor, the
41
// Pythonb object needs to be copied and rebound.
42
DispatchPtr
& assign(
const
DispatchPtr
& other,
void
* cppinst);
43
44
// Do not otherwise allow straight copies/assignment.
45
DispatchPtr
(
DispatchPtr
&) =
delete
;
46
DispatchPtr
(
DispatchPtr
&&) =
delete
;
47
DispatchPtr
&
operator=
(
const
DispatchPtr
& other) =
delete
;
48
49
// lifetime is directly bound to the lifetime of the dispatcher object
50
~DispatchPtr
();
51
52
// either C++ owns the Python object through a reference count (on fPyHardRef) or
53
// Python owns the C++ object and we only have a weak reference (through fPyWeakRef)
54
void
PythonOwns();
55
void
CppOwns();
56
57
// access to underlying object: cast and dereferencing
58
operator
PyObject
*()
const
{
59
return
Get();
60
}
61
62
PyObject
*
operator->
()
const
{
63
return
Get();
64
}
65
66
private
:
67
PyObject
* Get()
const
;
68
69
private
:
70
PyObject
*
fPyHardRef
;
71
PyObject
*
fPyWeakRef
;
72
};
73
74
}
// namespace CPyCppyy
75
76
#endif
// !CPYCPPYY_DISPATCHPTR_H
PyObject
_object PyObject
Definition
PyMethodBase.h:43
CommonDefs.h
CPYCPPYY_CLASS_EXTERN
#define CPYCPPYY_CLASS_EXTERN
Definition
CommonDefs.h:29
CPyCppyy::DispatchPtr
Definition
DispatchPtr.h:23
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr()
Definition
DispatchPtr.h:29
CPyCppyy::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition
DispatchPtr.h:71
CPyCppyy::DispatchPtr::operator->
PyObject * operator->() const
Definition
DispatchPtr.h:62
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr(DispatchPtr &)=delete
CPyCppyy::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition
DispatchPtr.h:70
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr(DispatchPtr &&)=delete
CPyCppyy::DispatchPtr::operator=
DispatchPtr & operator=(const DispatchPtr &other)=delete
CPyCppyy
Definition
callcontext.h:10
bindings
pyroot
cppyy
CPyCppyy
include
CPyCppyy
DispatchPtr.h
ROOT master - Reference Guide Generated on Tue Nov 5 2024 09:40:00 (GVA Time) using Doxygen 1.9.8