Logo ROOT   6.16/01
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//= PyROOT custom tuple-like array type ======================================
39PyTypeObject TTupleOfInstances_Type = {
40 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
41 (char*)"ROOT.InstancesArray", // tp_name
42 0, // tp_basicsize
43 0, // tp_itemsize
44 0, // tp_dealloc
45 0, // tp_print
46 0, // tp_getattr
47 0, // tp_setattr
48 0, // tp_compare
49 0, // tp_repr
50 0, // tp_as_number
51 0, // tp_as_sequence
52 0, // tp_as_mapping
53 0, // tp_hash
54 0, // tp_call
55 0, // tp_str
56 0, // tp_getattro
57 0, // tp_setattro
58 0, // tp_as_buffer
59 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
60 Py_TPFLAGS_BASETYPE, // tp_flags
61 (char*)"PyROOT long object for pass by reference", // tp_doc
62 0, // tp_traverse
63 0, // tp_clear
64 0, // tp_richcompare
65 0, // tp_weaklistoffset
66 0, // tp_iter
67 0, // tp_iternext
68 0, // tp_methods
69 0, // tp_members
70 0, // tp_getset
71 &PyTuple_Type, // tp_base
72 0, // tp_dict
73 0, // tp_descr_get
74 0, // tp_descr_set
75 0, // tp_dictoffset
76 0, // tp_init
77 0, // tp_alloc
78 0, // tp_new
79 0, // tp_free
80 0, // tp_is_gc
81 0, // tp_bases
82 0, // tp_mro
83 0, // tp_cache
84 0, // tp_subclasses
85 0 // tp_weaklist
86#if PY_VERSION_HEX >= 0x02030000
87 , 0 // tp_del
88#endif
89#if PY_VERSION_HEX >= 0x02060000
90 , 0 // tp_version_tag
91#endif
92#if PY_VERSION_HEX >= 0x03040000
93 , 0 // tp_finalize
94#endif
95};
96
97} // 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.