Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPPOperator.cxx
Go to the documentation of this file.
1// Bindings
2#include "cpyrt.h"
3
4using namespace cppjit;
5#include "CPPInstance.h"
6#include "CPPOperator.h"
7#include "Utility.h"
8
9//- constructor --------------------------------------------------------------
12 const std::string& name)
13 : CPPMethod(scope, method) {
14 // a bit silly but doing it this way allows decoupling the initialization
15 // order
16 if (name == "__mul__")
17 fStub = CPPInstance_Type.tp_as_number->nb_multiply;
18 else if (name == "__truediv__")
19 fStub = CPPInstance_Type.tp_as_number->nb_true_divide;
20 else if (name == "__add__")
21 fStub = CPPInstance_Type.tp_as_number->nb_add;
22 else if (name == "__sub__")
23 fStub = CPPInstance_Type.tp_as_number->nb_subtract;
24 else
25 fStub = nullptr;
26}
27
28//-----------------------------------------------------------------------------
30 size_t nargsf, PyObject* kwds,
31 CallContext* ctxt) {
32 // some operators can be a mix of global and class overloads; this method will
33 // first try class overloads (the existence of this method means that such
34 // were defined) and if failed, fall back on the global stubs
35 // TODO: the fact that this is a method and not an overload means that the
36 // global ones are tried for each method that fails during the overload
37 // 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 (cpyrt_PyArgs_GET_SIZE(args, nargsf) != 1) {
44 if ((cpyrt_PyArgs_GET_SIZE(args, nargsf) == 2 &&
45 cpyrt_PyArgs_GET_ITEM(args, 0) == (PyObject*)self))
46 idx_other = 1;
47 else
48 return result;
49 }
50
51 // fetch the current error, resetting the error buffer
52 auto error = cpyrt::Utility::FetchPyError();
53
54 result = fStub((PyObject*)self, cpyrt_PyArgs_GET_ITEM(args, idx_other));
55
56 // if there was still a problem, restore the Python error buffer
57 if (!result) {
59 }
60
61 return result;
62}
_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:142
PyObject * Call(CPPInstance *&self, cpyrt_PyArgs_t args, size_t nargsf, PyObject *kwds, CallContext *ctxt=nullptr) override
CPPOperator(interop::TCppScope_t scope, interop::TCppMethod_t method, const std::string &name)
PyObject * Call(CPPInstance *&self, cpyrt_PyArgs_t args, size_t nargsf, PyObject *kwds, CallContext *ctxt=nullptr) override
PyObject *const * cpyrt_PyArgs_t
Definition cpyrt.h:81
static Py_ssize_t cpyrt_PyArgs_GET_SIZE(cpyrt_PyArgs_t, size_t nargsf)
Definition cpyrt.h:90
static PyObject * cpyrt_PyArgs_GET_ITEM(cpyrt_PyArgs_t args, Py_ssize_t i)
Definition cpyrt.h:82
void RestorePyError(PyError_t &error)
Definition Utility.cxx:1492
PyError_t FetchPyError()
Definition Utility.cxx:1473
PyTypeObject CPPInstance_Type
Cpp::FuncRef TCppMethod_t
Definition API.h:44
Cpp::DeclRef TCppScope_t
Definition API.h:41
Definition API.cxx:34