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 "Python.h"
14
15#include "TPyArg.h"
16
17//______________________________________________________________________________
18// Generic wrapper for arguments
19// =============================
20//
21// Transport class for bringing C++ values and objects from Cling to Python. It
22// provides, from the selected constructor, the proper conversion to a PyObject.
23// In principle, there should be no need to use this class directly: it relies
24// on implicit conversions.
25
26//- data ---------------------------------------------------------------------
28
29//- constructor dispatcher ---------------------------------------------------
30void TPyArg::CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector<TPyArg> &args)
31{
32 int nArgs = args.size();
33 PyObject *pyargs = PyTuple_New(nArgs);
34 for (int i = 0; i < nArgs; ++i)
35 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
36 pyself = PyObject_Call(pyclass, pyargs, NULL);
37 Py_DECREF(pyargs);
38}
39
40////////////////////////////////////////////////////////////////////////////////
41void CallConstructor(PyObject *&pyself, PyObject *pyclass)
42{
43 PyObject *pyargs = PyTuple_New(0);
44 pyself = PyObject_Call(pyclass, pyargs, NULL);
45 Py_DECREF(pyargs);
46}
47
48//- generic dispatcher -------------------------------------------------------
49PyObject *TPyArg::CallMethod(PyObject *pymeth, const std::vector<TPyArg> &args)
50{
51 int nArgs = args.size();
52 PyObject *pyargs = PyTuple_New(nArgs);
53 for (int i = 0; i < nArgs; ++i)
54 PyTuple_SET_ITEM(pyargs, i, (PyObject *)args[i]);
55 PyObject *result = PyObject_Call(pymeth, pyargs, NULL);
56 Py_DECREF(pyargs);
57 return result;
58}
59
60//- denstructor dispatcher ----------------------------------------------------
61void TPyArg::CallDestructor(PyObject *&pyself, PyObject *, const std::vector<TPyArg> &)
62{
63 Py_XDECREF(pyself); // calls actual dtor if ref-count down to 0
64}
65
66////////////////////////////////////////////////////////////////////////////////
68{
69 Py_XDECREF(pyself);
70}
71
72//- constructors/destructor --------------------------------------------------
74{
75 // Construct a TPyArg from a python object.
76 Py_XINCREF(pyobject);
77 fPyObject = pyobject;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Construct a TPyArg from an integer value.
82
84{
85 fPyObject = PyLong_FromLong(value);
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// Construct a TPyArg from an integer value.
90
92{
93 fPyObject = PyLong_FromLong(value);
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Construct a TPyArg from a double value.
98
100{
101 fPyObject = PyFloat_FromDouble(value);
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// Construct a TPyArg from a C-string.
106
108{
109 fPyObject = PyUnicode_FromString(value);
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Copy constructor.
114
116{
117 Py_XINCREF(s.fPyObject);
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Assignment operator.
123
125{
126 if (&s != this) {
127 Py_XINCREF(s.fPyObject);
129 }
130 return *this;
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Done with held PyObject.
135
137{
138 Py_XDECREF(fPyObject);
139 fPyObject = NULL;
140}
141
142//- public members -----------------------------------------------------------
143TPyArg::operator PyObject *() const
144{
145 // Extract the python object.
146 Py_XINCREF(fPyObject);
147 return fPyObject;
148}
_object PyObject
long Long_t
Definition RtypesCore.h:54
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
static PyObject * CallMethod(PyObject *pymeth, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:49
static void CallDestructor(PyObject *&pyself, PyObject *pymeth, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:61
PyObject * fPyObject
Definition TPyArg.h:59
TPyArg(PyObject *)
Definition TPyArg.cxx:73
TPyArg & operator=(const TPyArg &)
Assignment operator.
Definition TPyArg.cxx:124
virtual ~TPyArg()
Done with held PyObject.
Definition TPyArg.cxx:136
static void CallConstructor(PyObject *&pyself, PyObject *pyclass, const std::vector< TPyArg > &args)
Definition TPyArg.cxx:30
_object PyObject
Definition TPyArg.h:14
void CallConstructor(PyObject *&pyself, PyObject *pyclass)
Definition TPyArg.cxx:31