Logo ROOT  
Reference Guide
DispatchPtr.cxx
Go to the documentation of this file.
1 // Bindings
2 #include "CPyCppyy.h"
3 #define CPYCPPYY_INTERNAL 1
4 #include "CPyCppyy/DispatchPtr.h"
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 //-----------------------------------------------------------------------------
18 CPyCppyy::DispatchPtr::DispatchPtr(PyObject* pyobj) : fPyHardRef(nullptr)
19 {
20  ((CPPInstance*)pyobj)->SetDispatchPtr(this);
21  fPyWeakRef = PyWeakref_NewRef(pyobj, nullptr);
22 }
23 
24 //-----------------------------------------------------------------------------
25 CPyCppyy::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 }
CPPInstance.h
CPyCppyy::DispatchPtr::fPyHardRef
PyObject * fPyHardRef
Definition: DispatchPtr.h:73
PyObject
_object PyObject
Definition: PyMethodBase.h:41
CPyCppyy::DispatchPtr::DispatchPtr
DispatchPtr()
Definition: DispatchPtr.h:29
ROOT::Math::GSLSimAn::Copy
void Copy(void *source, void *dest)
Definition: GSLSimAnnealing.cxx:149
CPyCppyy::CPPInstance
Definition: CPPInstance.h:26
CPyCppyy::DispatchPtr::Get
PyObject * Get() const
Definition: DispatchPtr.cxx:10
CPyCppyy::DispatchPtr::assign
DispatchPtr & assign(const DispatchPtr &other, void *cppinst)
Definition: DispatchPtr.cxx:33
CPyCppyy::DispatchPtr
Definition: DispatchPtr.h:23
CPyCppyy::DispatchPtr::fPyWeakRef
PyObject * fPyWeakRef
Definition: DispatchPtr.h:74
CPyCppyy.h
CPyCppyy::DispatchPtr::CppOwns
void CppOwns()
Definition: DispatchPtr.cxx:56
DispatchPtr.h
CPyCppyy::DispatchPtr::PythonOwns
void PythonOwns()
Definition: DispatchPtr.cxx:46