Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPPOperator.cxx
Go to the documentation of this file.
1// Bindings
2#include "CPyCppyy.h"
3#include "CPPOperator.h"
4#include "CPPInstance.h"
5
6
7//- constructor --------------------------------------------------------------
9 Cppyy::TCppScope_t scope, Cppyy::TCppMethod_t method, const std::string& name)
10 : CPPMethod(scope, method)
11{
12// a bit silly but doing it this way allows decoupling the initialization order
13 if (name == "__mul__")
14 fStub = CPPInstance_Type.tp_as_number->nb_multiply;
15 else if (name == CPPYY__div__)
16#if PY_VERSION_HEX < 0x03000000
17 fStub = CPPInstance_Type.tp_as_number->nb_divide;
18#else
19 fStub = CPPInstance_Type.tp_as_number->nb_true_divide;
20#endif
21 else if (name == "__add__")
22 fStub = CPPInstance_Type.tp_as_number->nb_add;
23 else if (name == "__sub__")
24 fStub = CPPInstance_Type.tp_as_number->nb_subtract;
25 else
26 fStub = nullptr;
27}
28
29//-----------------------------------------------------------------------------
31 CPyCppyy_PyArgs_t args, size_t nargsf, PyObject* kwds, CallContext* ctxt)
32{
33// some operators can be a mix of global and class overloads; this method will
34// first try class overloads (the existence of this method means that such were
35// defined) and if failed, fall back on the global stubs
36// TODO: the fact that this is a method and not an overload means that the global
37// ones are tried for each method that fails during the overload resolution
38 PyObject* result = this->CPPMethod::Call(self, args, nargsf, kwds, ctxt);
39 if (result || !fStub || !self)
40 return result;
41
42 Py_ssize_t idx_other = 0;
43 if (CPyCppyy_PyArgs_GET_SIZE(args, nargsf) != 1) {
44#if PY_VERSION_HEX >= 0x03080000
45 if ((CPyCppyy_PyArgs_GET_SIZE(args, nargsf) == 2 && CPyCppyy_PyArgs_GET_ITEM(args, 0) == (PyObject*)self))
46 idx_other = 1;
47 else
48#endif
49 return result;
50 }
51
52 PyObject* pytype = 0, *pyvalue = 0, *pytrace = 0;
53 PyErr_Fetch(&pytype, &pyvalue, &pytrace);
54
55 result = fStub((PyObject*)self, CPyCppyy_PyArgs_GET_ITEM(args, idx_other));
56
57 if (!result)
58 PyErr_Restore(pytype, pyvalue, pytrace);
59 else {
60 Py_XDECREF(pytype);
61 Py_XDECREF(pyvalue);
62 Py_XDECREF(pytrace);
63 }
64
65 return result;
66}
#define CPPYY__div__
Definition CPyCppyy.h:111
static Py_ssize_t CPyCppyy_PyArgs_GET_SIZE(CPyCppyy_PyArgs_t args, size_t)
Definition CPyCppyy.h:337
PyObject * CPyCppyy_PyArgs_t
Definition CPyCppyy.h:330
static PyObject * CPyCppyy_PyArgs_GET_ITEM(CPyCppyy_PyArgs_t args, Py_ssize_t i)
Definition CPyCppyy.h:331
_object PyObject
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
char name[80]
Definition TGX11.cxx:110
virtual PyObject * Call(CPPInstance *&self, CPyCppyy_PyArgs_t args, size_t nargsf, PyObject *kwds, CallContext *ctxt=nullptr)
virtual PyObject * Call(CPPInstance *&self, CPyCppyy_PyArgs_t args, size_t nargsf, PyObject *kwds, CallContext *ctxt=nullptr)
CPPOperator(Cppyy::TCppScope_t scope, Cppyy::TCppMethod_t method, const std::string &name)
PyTypeObject CPPInstance_Type
intptr_t TCppMethod_t
Definition cpp_cppyy.h:22
size_t TCppScope_t
Definition cpp_cppyy.h:18