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
15#include "../../cppyy/CPyCppyy/src/CPyCppyy.h"
16#include "../../cppyy/CPyCppyy/src/CPPInstance.h"
17#include "../../cppyy/CPyCppyy/src/Utility.h"
18
19#include "PyROOTPythonize.h"
20
21// ROOT
22#include "TClass.h"
23
24using namespace CPyCppyy;
25
26// Cast the void* returned by TClass::DynamicCast to the right type
28{
29 // Parse arguments
30 PyObject *pyclass = nullptr;
31 PyObject *pyobject = nullptr;
32 int up = 1;
33 if (!PyArg_ParseTuple(args, "O!O|i:DynamicCast", &CPPInstance_Type, &pyclass, &pyobject, &up))
34 return nullptr;
35
36 // Perform actual cast - calls default implementation of DynamicCast
39
40 void *address = cl1->DynamicCast(cl2, CPyCppyy::Instance_AsVoidPtr(pyobject), up);
41
42 if (CPyCppyy::Instance_Check(pyobject)) {
43 address = CPyCppyy::Instance_AsVoidPtr(pyobject);
44 } else if (PyInt_Check(pyobject) || PyLong_Check(pyobject)) {
45 address = (void *)PyLong_AsLongLong(pyobject);
46 } else {
47 Utility::GetBuffer(pyobject, '*', 1, address, false);
48 }
49
50 // Now use binding to return a usable class. Upcast: result is a base.
51 // Downcast: result is a derived.
52 Cppyy::TCppType_t cpptype = ((CPyCppyy::CPPInstance *)(up ? pyclass : self))->ObjectIsA();
53 TClass *tcl = TClass::GetClass(Cppyy::GetScopedFinalName(cpptype).c_str());
54 TClass *klass = (TClass *)tcl->DynamicCast(TClass::Class(), up ? CPyCppyy::Instance_AsVoidPtr(pyclass) : cl1);
55
56 return CPyCppyy::Instance_FromVoidPtr(address, klass->GetName());
57}
58
59////////////////////////////////////////////////////////////////////////////
60/// \brief Add pythonization for TClass::DynamicCast.
61/// \param[in] self Always null, since this is a module function.
62/// \param[in] args Pointer to a Python tuple object containing the arguments
63/// received from Python.
64///
65/// TClass::DynamicCast returns a void* that the user still has to cast (it
66/// will have the proper offset, though). Fix this by providing the requested
67/// binding if the cast succeeded.
69{
70 PyObject *pyclass = PyTuple_GetItem(args, 0);
71 Utility::AddToClass(pyclass, "DynamicCast", (PyCFunction)TClassDynamicCastPyz);
73}
#define Py_RETURN_NONE
Definition CPyCppyy.h:268
_object PyObject
PyObject * TClassDynamicCastPyz(PyObject *self, PyObject *args)
Definition TClassPyz.cxx:27
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
static TClass * Class()
void * DynamicCast(const TClass *base, void *obj, Bool_t up=kTRUE)
Cast obj of this class type up to baseclass cl if up is true.
Definition TClass.cxx:4915
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:2968
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Py_ssize_t GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, bool check=true)
Definition Utility.cxx:808
bool AddToClass(PyObject *pyclass, const char *label, PyCFunction cfunc, int flags=METH_VARARGS)
Definition Utility.cxx:182
PyTypeObject CPPInstance_Type
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:163
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:118
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:103
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19
RPY_EXPORTED std::string GetScopedFinalName(TCppType_t type)
PyObject * AddTClassDynamicCastPyz(PyObject *self, PyObject *args)
Add pythonization for TClass::DynamicCast.
Definition TClassPyz.cxx:68