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