Logo ROOT   6.18/05
Reference Guide
TTupleOfInstances.cxx
Go to the documentation of this file.
1// Author: Wim Lavrijsen, Dec 2006
2
3// Bindings
4#include "PyROOT.h"
5#include "TTupleOfInstances.h"
6#include "RootWrapper.h"
7
8
9namespace PyROOT {
10
11//= support for C-style arrays of objects ====================================
13 Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, Py_ssize_t nelems )
14{
15// TODO: the extra copy is inefficient, but it appears that the only way to
16// initialize a subclass of a tuple is through a sequence
17 PyObject* tup = PyTuple_New( nelems );
18 for ( int i = 0; i < nelems; ++i ) {
19 // TODO: there's an assumption here that there is no padding, which is bound
20 // to be incorrect in certain cases
21 PyTuple_SetItem( tup, i,
22 BindCppObject( (char*)address + i*Cppyy::SizeOf( klass ), klass, kFALSE /* isRef */ ) );
23 // Note: objects are bound as pointers, yet since the pointer value stays in
24 // place, updates propagate just as if they were bound by-reference
25 }
26
27 PyObject* args = PyTuple_New( 1 );
28 Py_INCREF( tup ); PyTuple_SET_ITEM( args, 0, tup );
29 PyObject* arr = PyTuple_Type.tp_new( &TTupleOfInstances_Type, args, NULL );
30 if ( PyErr_Occurred() ) PyErr_Print();
31
32 Py_DECREF( args );
33 // tup ref eaten by SET_ITEM on args
34
35 return arr;
36}
37
38#if !defined(_MSC_VER)
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41#endif
42
43//= PyROOT custom tuple-like array type ======================================
44PyTypeObject TTupleOfInstances_Type = {
45 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
46 (char*)"ROOT.InstancesArray", // tp_name
47 0, // tp_basicsize
48 0, // tp_itemsize
49 0, // tp_dealloc
50 0, // tp_print (python < 3.8)
51 // tp_vectorcall_offset (python >= 3.8)
52 0, // tp_getattr
53 0, // tp_setattr
54 0, // tp_compare
55 0, // tp_repr
56 0, // tp_as_number
57 0, // tp_as_sequence
58 0, // tp_as_mapping
59 0, // tp_hash
60 0, // tp_call
61 0, // tp_str
62 0, // tp_getattro
63 0, // tp_setattro
64 0, // tp_as_buffer
65 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
66 Py_TPFLAGS_BASETYPE, // tp_flags
67 (char*)"PyROOT long object for pass by reference", // tp_doc
68 0, // tp_traverse
69 0, // tp_clear
70 0, // tp_richcompare
71 0, // tp_weaklistoffset
72 0, // tp_iter
73 0, // tp_iternext
74 0, // tp_methods
75 0, // tp_members
76 0, // tp_getset
77 &PyTuple_Type, // tp_base
78 0, // tp_dict
79 0, // tp_descr_get
80 0, // tp_descr_set
81 0, // tp_dictoffset
82 0, // tp_init
83 0, // tp_alloc
84 0, // tp_new
85 0, // tp_free
86 0, // tp_is_gc
87 0, // tp_bases
88 0, // tp_mro
89 0, // tp_cache
90 0, // tp_subclasses
91 0 // tp_weaklist
92#if PY_VERSION_HEX >= 0x02030000
93 , 0 // tp_del
94#endif
95#if PY_VERSION_HEX >= 0x02060000
96 , 0 // tp_version_tag
97#endif
98#if PY_VERSION_HEX >= 0x03040000
99 , 0 // tp_finalize
100#endif
101#if PY_VERSION_HEX >= 0x03080000
102 , 0 // tp_vectorcall
103#if PY_VERSION_HEX < 0x03090000
104 , 0 // tp_print (python 3.8 only)
105#endif
106#endif
107};
108
109#if !defined(_MSC_VER)
110#pragma GCC diagnostic pop
111#endif
112
113} // namespace PyROOT
int Py_ssize_t
Definition: PyROOT.h:166
#define PyVarObject_HEAD_INIT(type, size)
Definition: PyROOT.h:159
const Bool_t kFALSE
Definition: RtypesCore.h:88
_object PyObject
Definition: TPyArg.h:20
size_t SizeOf(TCppType_t klass)
Definition: Cppyy.cxx:235
TCppScope_t TCppType_t
Definition: Cppyy.h:16
void * TCppObject_t
Definition: Cppyy.h:17
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE)
if the object is a null pointer, return a typed one (as needed for overloading)
PyObject * TTupleOfInstances_New(Cppyy::TCppObject_t address, Cppyy::TCppType_t klass, Py_ssize_t nelems)
PyTypeObject TTupleOfInstances_Type
Representation of C-style array of instances.