ROOT  6.06/09
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 // ROOT
9 #include "TObject.h"
10 
11 
12 //______________________________________________________________________________
13 // Generic wrapper for arguments
14 // =============================
15 //
16 // Transport class for bringing C++ values and objects from Cling to Python. It
17 // provides, from the selected constructor, the proper conversion to a PyObject.
18 // In principle, there should be no need to use this class directly: it relies
19 // on implicit conversions.
20 
21 
22 //- data ---------------------------------------------------------------------
24 
25 //- constructor dispatcher ---------------------------------------------------
26 void TPyArg::CallConstructor( PyObject*& pyself, PyObject* pyclass, const std::vector<TPyArg>& args )
27 {
28  int nArgs = args.size();
29  PyObject* pyargs = PyTuple_New( nArgs );
30  for ( int i = 0; i < nArgs; ++i )
31  PyTuple_SET_ITEM( pyargs, i, (PyObject*)args[i] );
32  pyself = PyObject_Call( pyclass, pyargs, NULL );
33  Py_DECREF( pyargs );
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 
38 void CallConstructor( PyObject*& pyself, PyObject* pyclass )
39 {
40  PyObject* pyargs = PyTuple_New( 0 );
41  pyself = PyObject_Call( pyclass, pyargs, NULL );
42  Py_DECREF( pyargs );
43 }
44 
45 //- generic dispatcher -------------------------------------------------------
46 PyObject* TPyArg::CallMethod( PyObject* pymeth, const std::vector<TPyArg>& args )
47 {
48  int nArgs = args.size();
49  PyObject* pyargs = PyTuple_New( nArgs );
50  for ( int i = 0; i < nArgs; ++i )
51  PyTuple_SET_ITEM( pyargs, i, (PyObject*)args[i] );
52  PyObject* result = PyObject_Call( pymeth, pyargs, NULL );
53  Py_DECREF( pyargs );
54  return result;
55 }
56 
57 //- constructors/destructor --------------------------------------------------
59 {
60 // Construct a TPyArg from a python object.
61  Py_XINCREF( pyobject );
62  fPyObject = pyobject;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Construct a TPyArg from an integer value.
67 
69 {
70  fPyObject = PyInt_FromLong( value );
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Construct a TPyArg from an integer value.
75 
77 {
78  fPyObject = PyLong_FromLong( value );
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Construct a TPyArg from a double value.
83 
85 {
86  fPyObject = PyFloat_FromDouble( value );
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Construct a TPyArg from a C-string.
91 
92 TPyArg::TPyArg( const char* value )
93 {
94  fPyObject = PyROOT_PyUnicode_FromString( value );
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Copy constructor.
99 
101 {
102  Py_XINCREF( s.fPyObject );
103  fPyObject = s.fPyObject;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Assignment operator.
108 
110 {
111  if ( &s != this ) {
112  Py_XINCREF( s.fPyObject );
113  fPyObject = s.fPyObject;
114  }
115  return *this;
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Done with held PyObject.
120 
122 {
123  Py_XDECREF( fPyObject );
124  fPyObject = NULL;
125 }
126 
127 //- public members -----------------------------------------------------------
128 TPyArg::operator PyObject*() const
129 {
130 // Extract the python object.
131  Py_XINCREF( fPyObject );
132  return fPyObject;
133 }
#define PyROOT_PyUnicode_FromString
Definition: PyROOT.h:71
TPyArg & operator=(const TPyArg &)
Assignment operator.
Definition: TPyArg.cxx:109
void CallConstructor(PyObject *&pyself, PyObject *pyclass)
Definition: TPyArg.cxx:38
TPyArg(PyObject *)
Definition: TPyArg.cxx:58
int Int_t
Definition: RtypesCore.h:41
STL namespace.
static PyObject * CallMethod(PyObject *pymeth, const std::vector< TPyArg > &args)
Definition: TPyArg.cxx:46
Definition: TPyArg.h:29
virtual ~TPyArg()
Done with held PyObject.
Definition: TPyArg.cxx:121
long Long_t
Definition: RtypesCore.h:50
double Double_t
Definition: RtypesCore.h:55
typedef void((*Func_t)())
#define NULL
Definition: Rtypes.h:82
double result[121]
float value
Definition: math.cpp:443
_object PyObject
Definition: TPyArg.h:22
ClassImp(TPyArg) void TPyArg
Definition: TPyArg.cxx:23