Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPyArg.cxx
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 08/2019
2// Original PyROOT code by Wim Lavrijsen, LBL
3//
4// /*************************************************************************
5// * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6// * All rights reserved. *
7// * *
8// * For the licensing terms see $ROOTSYS/LICENSE. *
9// * For the list of contributors see $ROOTSYS/README/CREDITS. *
10// *************************************************************************/
11
12// Bindings
13#include "CPyCppyy.h"
14#include "TPyArg.h"
15
16//______________________________________________________________________________
17// Generic wrapper for arguments
18// =============================
19//
20// Transport class for bringing C++ values and objects from Cling to Python. It
21// provides, from the selected constructor, the proper conversion to a PyObject.
22// In principle, there should be no need to use this class directly: it relies
23// on implicit conversions.
24
25//- data ---------------------------------------------------------------------
27
28//- constructor dispatcher ---------------------------------------------------
29void TPyArg::CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector<TPyArg> &args)
30{
31 int nArgs = args.size();
32 PyObject *pyargs = PyTuple_New(nArgs);
33 for (int i = 0; i < nArgs; ++i)
34 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
35 pyself = PyObject_Call(pyclass, pyargs, NULL);
36 Py_DECREF(pyargs);
37}
38
39////////////////////////////////////////////////////////////////////////////////
40void CallConstructor(PyObject *&pyself, PyObject *pyclass)
41{
42 PyObject *pyargs = PyTuple_New(0);
43 pyself = PyObject_Call(pyclass, pyargs, NULL);
44 Py_DECREF(pyargs);
45}
46
47//- generic dispatcher -------------------------------------------------------
48PyObject *TPyArg::CallMethod(PyObject *pymeth, const std::vector<TPyArg> &args)
49{
50 int nArgs = args.size();
51 PyObject *pyargs = PyTuple_New(nArgs);
52 for (int i = 0; i < nArgs; ++i)
53 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
54 PyObject *result = PyObject_Call(pymeth, pyargs, NULL);
55 Py_DECREF(pyargs);
56 return result;
57}
58
59//- denstructor dispatcher ----------------------------------------------------
60void TPyArg::CallDestructor(PyObject *&pyself, PyObject *, const std::vector<TPyArg> &)
61{
62 Py_XDECREF(pyself); // calls actual dtor if ref-count down to 0
63}
64
65////////////////////////////////////////////////////////////////////////////////
67{
68 Py_XDECREF(pyself);
69}
70
71//- constructors/destructor --------------------------------------------------
73{
74 // Construct a TPyArg from a python object.
75 Py_XINCREF(pyobject);
76 fPyObject = pyobject;
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Construct a TPyArg from an integer value.
81
83{
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Construct a TPyArg from an integer value.
89
91{
92 fPyObject = PyLong_FromLong(value);
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Construct a TPyArg from a double value.
97
99{
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Construct a TPyArg from a C-string.
105
106TPyArg::TPyArg(const char *value)
107{
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Copy constructor.
113
115{
116 Py_XINCREF(s.fPyObject);
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Assignment operator.
122
124{
125 if (&s != this) {
126 Py_XINCREF(s.fPyObject);
128 }
129 return *this;
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Done with held PyObject.
134
136{
137 Py_XDECREF(fPyObject);
138 fPyObject = NULL;
139}
140
141//- public members -----------------------------------------------------------
142TPyArg::operator PyObject *() const
143{
144 // Extract the python object.
145 Py_XINCREF(fPyObject);
146 return fPyObject;
147}
#define CPyCppyy_PyText_FromString
Definition CPyCppyy.h:102
PyFloat_FromDouble
PyInt_FromLong
_object PyObject
long Long_t
Definition RtypesCore.h:54
double Double_t
Definition RtypesCore.h:59
#define ClassImp(name)
Definition Rtypes.h:364
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
_object PyObject
Definition TPyArg.h:14
void CallConstructor(PyObject *&pyself, PyObject *pyclass)
Definition TPyArg.cxx:31