Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DispatchPtr.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#define CPYCPPYY_INTERNAL 1
5#undef CPYCPPYY_INTERNAL
6#include "CPPInstance.h"
7
8
9//-----------------------------------------------------------------------------
11{
12 if (fPyHardRef) return fPyHardRef;
13 if (fPyWeakRef) return PyWeakref_GetObject(fPyWeakRef);
14 return nullptr;
15}
16
17//-----------------------------------------------------------------------------
18CPyCppyy::DispatchPtr::DispatchPtr(PyObject* pyobj) : fPyHardRef(nullptr)
19{
20 ((CPPInstance*)pyobj)->SetDispatchPtr(this);
21 fPyWeakRef = PyWeakref_NewRef(pyobj, nullptr);
22}
23
24//-----------------------------------------------------------------------------
25CPyCppyy::DispatchPtr::DispatchPtr(const DispatchPtr& other, void* cppinst) : fPyWeakRef(nullptr)
26{
27 PyObject* pyobj = other.Get();
28 fPyHardRef = pyobj ? (PyObject*)((CPPInstance*)pyobj)->Copy(cppinst) : nullptr;
29 if (fPyHardRef) ((CPPInstance*)fPyHardRef)->SetDispatchPtr(this);
30}
31
32//-----------------------------------------------------------------------------
34{
35 if (this != &other) {
36 Py_XDECREF(fPyWeakRef); fPyWeakRef = nullptr;
37 Py_XDECREF(fPyHardRef);
38 PyObject* pyobj = other.Get();
39 fPyHardRef = pyobj ? (PyObject*)((CPPInstance*)pyobj)->Copy(cppinst) : nullptr;
40 if (fPyHardRef) ((CPPInstance*)fPyHardRef)->SetDispatchPtr(this);
41 }
42 return *this;
43}
44
45//-----------------------------------------------------------------------------
47{
48// Python maintains the hardref, so only allowed a weakref here
49 if (fPyHardRef) {
50 fPyWeakRef = PyWeakref_NewRef(fPyHardRef, nullptr);
51 Py_DECREF(fPyHardRef); fPyHardRef = nullptr;
52 }
53}
54
55//-----------------------------------------------------------------------------
57{
58// C++ maintains the hardref, keeping the PyObject alive w/o outstanding ref
59 if (fPyWeakRef) {
60 fPyHardRef = PyWeakref_GetObject(fPyWeakRef);
61 Py_XINCREF(fPyHardRef);
62 Py_DECREF(fPyWeakRef); fPyWeakRef = nullptr;
63 }
64}
_object PyObject
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
PyObject * Get() const