Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TClassPyz.cxx
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 02/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 "CPyCppyy/API.h"
14
17
18#include "PyROOTPythonize.h"
19
20// ROOT
21#include "TClass.h"
22
23using namespace CPyCppyy;
24
25namespace PyROOT{
26void GetBuffer(PyObject *pyobject, void *&buf);
27}
28
29// Cast the void* returned by TClass::DynamicCast to the right type
31{
32 // Parse arguments
33 PyObject *pyclass = nullptr;
34 PyObject *pyobject = nullptr;
35 int up = 1;
36 if (!PyArg_ParseTuple(args, "OO|i:DynamicCast", &pyclass, &pyobject, &up))
37 return nullptr;
38
41 if (!type) {
42 return nullptr;
43 }
46 const char *nameStr = name ? PyUnicode_AsUTF8AndSize(name, nullptr) : nullptr;
47 if (nameStr) {
48 PyErr_Format(PyExc_TypeError, "DynamicCast argument 1 must be a cppyy instance, got '%s'", nameStr);
49 } else {
50 PyErr_SetString(PyExc_TypeError, "DynamicCast argument 1 must be a cppyy instance");
51 }
53 return nullptr;
54 }
55
56 // Perform actual cast - calls default implementation of DynamicCast
59
60 void *address = cl1->DynamicCast(cl2, CPyCppyy::Instance_AsVoidPtr(pyobject), up);
61
64 } else {
65 long long value = PyLong_AsLongLong(pyobject);
66 if (!PyErr_Occurred()) {
67 address = (void *)value; // pyobject was indeed a PyLong
68 } else {
71 }
72 }
73
74 // Now use binding to return a usable class. Upcast: result is a base.
75 // Downcast: result is a derived.
78
79 return CPyCppyy::Instance_FromVoidPtr(address, klass->GetName());
80}
81
82////////////////////////////////////////////////////////////////////////////
83/// \brief Add pythonization for TClass::DynamicCast.
84/// \param[in] self Always null, since this is a module function.
85/// \param[in] args Pointer to a Python tuple object containing the arguments
86/// received from Python.
87///
88/// TClass::DynamicCast returns a void* that the user still has to cast (it
89/// will have the proper offset, though). Fix this by providing the requested
90/// binding if the cast succeeded.
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
_object PyObject
PyObject * TClassDynamicCastPyz(PyObject *self, PyObject *args)
Definition TClassPyz.cxx:30
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:148
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
static TClass * Class()
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2994
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:185
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:178
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:133
CPYCPPYY_EXTERN std::string Instance_GetScopedFinalName(PyObject *pyobject)
Definition API.cxx:106
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:118
void GetBuffer(PyObject *pyobject, void *&buf)
PyObject * AddTClassDynamicCastPyz(PyObject *self, PyObject *args)
Add pythonization for TClass::DynamicCast.
Definition TClassPyz.cxx:91