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",
34 &CPPInstance_Type, &pyclass,
35 &pyobject,
36 &up))
37 return nullptr;
38
39 // Perform actual cast - calls default implementation of DynamicCast
42
43 void *address = cl1->DynamicCast(cl2, CPyCppyy::Instance_AsVoidPtr(pyobject), up);
44
45 if (CPyCppyy::Instance_Check(pyobject)) {
46 address = CPyCppyy::Instance_AsVoidPtr(pyobject);
47 } else if (PyInt_Check(pyobject) || PyLong_Check(pyobject)) {
48 address = (void *)PyLong_AsLongLong(pyobject);
49 } else {
50 Utility::GetBuffer(pyobject, '*', 1, address, false);
51 }
52
53 // Now use binding to return a usable class. Upcast: result is a base.
54 // Downcast: result is a derived.
55 Cppyy::TCppType_t cpptype = ((CPyCppyy::CPPInstance*)(up ? pyclass : self))->ObjectIsA();
56 TClass *tcl = TClass::GetClass(Cppyy::GetScopedFinalName(cpptype).c_str());
57 TClass *klass = (TClass *)tcl->DynamicCast(TClass::Class(), up ? CPyCppyy::Instance_AsVoidPtr(pyclass) : cl1);
58
59 return CPyCppyy::Instance_FromVoidPtr(address, klass->GetName());
60}
61
62////////////////////////////////////////////////////////////////////////////
63/// \brief Add pythonization for TClass::DynamicCast.
64/// \param[in] self Always null, since this is a module function.
65/// \param[in] args Pointer to a Python tuple object containing the arguments
66/// received from Python.
67///
68/// TClass::DynamicCast returns a void* that the user still has to cast (it
69/// will have the proper offset, though). Fix this by providing the requested
70/// binding if the cast succeeded.
72{
73 PyObject *pyclass = PyTuple_GetItem(args, 0);
74 Utility::AddToClass(pyclass, "DynamicCast", (PyCFunction)TClassDynamicCastPyz);
76}
#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:71