Logo ROOT  
Reference Guide
TPyArg.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#define CPYCPPYY_INTERNAL 1
4#include "CPyCppyy/TPyArg.h"
5#undef CPYCPPYY_INTERNAL
6
7
8//______________________________________________________________________________
9// Generic wrapper for arguments
10// =============================
11//
12// Transport class for bringing C++ values and objects from Cling to Python. It
13// provides, from the selected constructor, the proper conversion to a PyObject.
14// In principle, there should be no need to use this class directly: it relies
15// on implicit conversions.
16
17
18//- constructor dispatcher ---------------------------------------------------
20 PyObject*& pyself, PyObject* pyclass, const std::vector<TPyArg>& args)
21{
22 int nArgs = (int)args.size();
23 PyObject* pyargs = PyTuple_New(nArgs);
24 for (int i = 0; i < nArgs; ++i)
25 PyTuple_SET_ITEM(pyargs, i, (PyObject*)args[i]);
26 pyself = PyObject_Call(pyclass, pyargs, nullptr);
27 Py_DECREF(pyargs);
28}
29
30//----------------------------------------------------------------------------
31void CallConstructor(PyObject*& pyself, PyObject* pyclass)
32{
33 PyObject* pyargs = PyTuple_New(0);
34 pyself = PyObject_Call(pyclass, pyargs, nullptr);
35 Py_DECREF(pyargs);
36}
37
38//- generic dispatcher -------------------------------------------------------
39PyObject* TPyArg::CallMethod(PyObject* pymeth, const std::vector<TPyArg>& args)
40{
41 int nArgs = (int)args.size();
42 PyObject* pyargs = PyTuple_New(nArgs);
43 for (int i = 0; i < nArgs; ++i)
44 PyTuple_SET_ITEM(pyargs, i, (PyObject*)args[i]);
45 PyObject* result = PyObject_Call(pymeth, pyargs, nullptr);
46 Py_DECREF(pyargs);
47 return result;
48}
49
50//- destructor dispatcher ----------------------------------------------------
51void TPyArg::CallDestructor(PyObject*& pyself, PyObject*, const std::vector<TPyArg>&)
52{
53 Py_XDECREF(pyself); // calls actual dtor if ref-count down to 0
54}
55
56//----------------------------------------------------------------------------
58{
59 Py_XDECREF(pyself);
60}
61
62//- constructors/destructor --------------------------------------------------
63TPyArg::TPyArg(PyObject* pyobject)
64{
65// Construct a TPyArg from a python object.
66 Py_XINCREF(pyobject);
67 fPyObject = pyobject;
68}
69
70//----------------------------------------------------------------------------
71TPyArg::TPyArg(int value)
72{
73// Construct a TPyArg from an integer value.
75}
76
77//----------------------------------------------------------------------------
78TPyArg::TPyArg(long value)
79{
80// Construct a TPyArg from an integer value.
81 fPyObject = PyLong_FromLong(value);
82}
83
84//----------------------------------------------------------------------------
85TPyArg::TPyArg(double value)
86{
87// Construct a TPyArg from a double value.
89}
90
91//----------------------------------------------------------------------------
92TPyArg::TPyArg(const char* value)
93{
94// Construct a TPyArg from a C-string.
96}
97
98//----------------------------------------------------------------------------
100{
101// Copy constructor.
102 Py_XINCREF(s.fPyObject);
103 fPyObject = s.fPyObject;
104}
105
106//----------------------------------------------------------------------------
108{
109// Assignment operator.
110 if (this != &s) {
111 Py_XINCREF(s.fPyObject);
112 fPyObject = s.fPyObject;
113 }
114 return *this;
115}
116
117//----------------------------------------------------------------------------
119{
120// Done with held PyObject.
121 Py_XDECREF(fPyObject);
122 fPyObject = nullptr;
123}
124
125//- public members -----------------------------------------------------------
126TPyArg::operator PyObject*() const
127{
128// Extract the python object.
129 Py_XINCREF(fPyObject);
130 return fPyObject;
131}
#define CPyCppyy_PyText_FromString
Definition: CPyCppyy.h:102
PyFloat_FromDouble
Definition: Converters.cxx:921
PyInt_FromLong
Definition: Converters.cxx:858
_object PyObject
Definition: PyMethodBase.h:41
Definition: TPyArg.h:33
static PyObject * CallMethod(PyObject *pymeth, const std::vector< TPyArg > &args)
Definition: TPyArg.cxx:48
static void CallDestructor(PyObject *&pyself, PyObject *pymeth, const std::vector< TPyArg > &args)
Definition: TPyArg.cxx:60
PyObject * fPyObject
Definition: TPyArg.h:59
TPyArg(PyObject *)
Definition: TPyArg.cxx:72
TPyArg & operator=(const TPyArg &)
Assignment operator.
Definition: TPyArg.cxx:123
virtual ~TPyArg()
Done with held PyObject.
Definition: TPyArg.cxx:135
static void CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector< TPyArg > &args)
Definition: TPyArg.cxx:29
static constexpr double s
void CallConstructor(PyObject *&pyself, PyObject *pyclass)
Definition: TPyArg.cxx:40