Logo ROOT   6.18/05
Reference Guide
Converters.h
Go to the documentation of this file.
1// @(#)root/pyroot:$Id$
2// Author: Wim Lavrijsen, Jan 2005
3#ifndef PYROOT_CONVERTERS_H
4#define PYROOT_CONVERTERS_H
5
6// ROOT
7#include "TString.h"
8
9// Standard
10#include <limits.h>
11#include <string>
12#include <map>
13
14
15namespace PyROOT {
16
17 class ObjectProxy;
18 struct TParameter;
19 struct TCallContext;
20
21 class TConverter {
22 public:
23 virtual ~TConverter() {}
24
25 public:
26 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 ) = 0;
27 virtual PyObject* FromMemory( void* address );
28 virtual Bool_t ToMemory( PyObject* value, void* address );
29 };
30
31#define PYROOT_DECLARE_BASIC_CONVERTER( name ) \
32 class T##name##Converter : public TConverter { \
33 public: \
34 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
35 virtual PyObject* FromMemory( void* ); \
36 virtual Bool_t ToMemory( PyObject*, void* ); \
37 }; \
38 \
39 class TConst##name##RefConverter : public TConverter { \
40 public: \
41 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
42 }
43
44
45#define PYROOT_DECLARE_BASIC_CONVERTER2( name, base ) \
46 class T##name##Converter : public T##base##Converter { \
47 public: \
48 virtual PyObject* FromMemory( void* ); \
49 virtual Bool_t ToMemory( PyObject*, void* ); \
50 }; \
51 \
52 class TConst##name##RefConverter : public TConverter { \
53 public: \
54 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
55 }
56
57#define PYROOT_DECLARE_REF_CONVERTER( name ) \
58 class T##name##RefConverter : public TConverter { \
59 public: \
60 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
61 };
62
63#define PYROOT_DECLARE_ARRAY_CONVERTER( name ) \
64 class T##name##Converter : public TConverter { \
65 public: \
66 T##name##Converter( Py_ssize_t size = -1 ) { fSize = size; } \
67 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
68 virtual PyObject* FromMemory( void* ); \
69 virtual Bool_t ToMemory( PyObject*, void* ); \
70 private: \
71 Py_ssize_t fSize; \
72 }; \
73 \
74 class T##name##RefConverter : public T##name##Converter { \
75 public: \
76 using T##name##Converter::T##name##Converter; \
77 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
78 }
79
80// converters for built-ins
95
99
100 class TVoidConverter : public TConverter {
101 public:
102 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
103 };
104
106 public:
107 TCStringConverter( UInt_t maxSize = UINT_MAX ) : fMaxSize( maxSize ) {}
108
109 public:
110 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
111 virtual PyObject* FromMemory( void* address );
112 virtual Bool_t ToMemory( PyObject* value, void* address );
113
114 protected:
115 std::string fBuffer;
117 };
118
120 public:
121 TNonConstCStringConverter( UInt_t maxSize = UINT_MAX ) : TCStringConverter( maxSize ) {}
122
123 public:
124 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
125 virtual PyObject* FromMemory( void* address );
126 };
127
129 public:
130 TNonConstUCStringConverter( UInt_t maxSize = UINT_MAX ) : TNonConstCStringConverter( maxSize ) {}
131
132 public:
133 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
134 };
135
136// pointer/array conversions
138 public:
139 TVoidArrayConverter( Bool_t keepControl = kTRUE ) { fKeepControl = keepControl; }
140 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
141 virtual PyObject* FromMemory( void* address );
142 virtual Bool_t ToMemory( PyObject* value, void* address );
143
144 protected:
145 virtual Bool_t GetAddressSpecialCase( PyObject* pyobject, void*& address );
146
147 protected:
149
150 private:
152 };
153
163
165 public:
166 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
167 };
168
169// converters for special cases
171 public:
173 TVoidArrayConverter( keepControl ), fClass( klass ), fObjProxy(nullptr) {}
174
175 public:
176 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
177 virtual PyObject* FromMemory( void* address );
178 virtual Bool_t ToMemory( PyObject* value, void* address );
179
180 protected:
183 };
184
186 public:
188
189 protected:
190 virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
191 };
192
194 public:
195 using TStrictCppObjectConverter::TStrictCppObjectConverter;
196
197 public:
198 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
199 };
200
202 public:
204
205 public:
206 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
207
208 protected:
211 };
212
213 template <bool ISREFERENCE>
215 public:
217
218 public:
219 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
220 virtual PyObject* FromMemory( void* address );
221 virtual Bool_t ToMemory( PyObject* value, void* address );
222 };
223
224 extern template class TCppObjectPtrConverter<true>;
225 extern template class TCppObjectPtrConverter<false>;
226
228 public:
229 TCppObjectArrayConverter( Cppyy::TCppType_t klass, size_t size, Bool_t keepControl = kFALSE ) :
230 TCppObjectConverter( klass, keepControl ), m_size( size ) {}
231
232 public:
233 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
234 virtual PyObject* FromMemory( void* address );
235 virtual Bool_t ToMemory( PyObject* value, void* address );
236
237 protected:
238 size_t m_size;
239 };
240
241// CLING WORKAROUND -- classes for STL iterators are completely undefined in that
242// they come in a bazillion different guises, so just do whatever
244 public:
245 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
246 };
247// -- END CLING WORKAROUND
248
250 public:
251 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
252 };
253
255 public:
256 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
257 virtual PyObject* FromMemory( void* address );
258 };
259
261
262#define PYROOT_DECLARE_STRING_CONVERTER( name, strtype ) \
263 class T##name##Converter : public TCppObjectConverter { \
264 public: \
265 T##name##Converter( Bool_t keepControl = kTRUE ); \
266 public: \
267 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
268 virtual PyObject* FromMemory( void* address ); \
269 virtual Bool_t ToMemory( PyObject* value, void* address ); \
270 private: \
271 strtype fBuffer; \
272 }
273
275 PYROOT_DECLARE_STRING_CONVERTER( STLString, std::string );
277
279 public:
280 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* = 0 );
281 };
282
283// smart pointer converter
285 public:
287 Cppyy::TCppType_t rawPtrType,
289 Bool_t keepControl = kFALSE,
290 Bool_t handlePtr = kFALSE )
291 : fClass( klass ), fRawPtrType( rawPtrType ), fDereferencer( deref ),
292 fKeepControl( keepControl ), fHandlePtr( handlePtr ) {}
293
294 public:
295 virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
296 virtual PyObject* FromMemory( void* address );
297 //virtual Bool_t ToMemory( PyObject* value, void* address );
298
299 protected:
300 virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
301
307 };
308
309// create converter from fully qualified type
310 TConverter* CreateConverter( const std::string& fullType, Long_t size = -1 );
311
312} // namespace PyROOT
313
314#endif // !PYROOT_CONVERTERS_H
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
_object PyObject
Definition: TPyArg.h:20
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:589
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:572
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
construct a new string and copy it in new memory
Definition: Converters.cxx:551
TCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:107
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:145
virtual ~TConverter()
Definition: Converters.h:23
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)=0
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:135
TCppObjectArrayConverter(Cppyy::TCppType_t klass, size_t size, Bool_t keepControl=kFALSE)
Definition: Converters.h:229
Cppyy::TCppType_t fClass
Definition: Converters.h:181
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance*, set arg for call
Definition: Converters.cxx:897
virtual PyObject * FromMemory(void *address)
construct python object from C++ instance read at <address>
Definition: Converters.cxx:940
virtual Bool_t ToMemory(PyObject *value, void *address)
convert to C++ instance, write it at <address>
Definition: Converters.cxx:948
TCppObjectConverter(Cppyy::TCppType_t klass, Bool_t keepControl=kFALSE)
Definition: Converters.h:172
virtual Bool_t ToMemory(PyObject *value, void *address)
convert to C++ instance*, write it at <address>
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance**, set arg for call
virtual PyObject * FromMemory(void *address)
construct python object from C++ instance* read at <address>
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
just convert pointer if it is a ROOT object
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
attempt base class first (i.e. passing a string), but if that fails, try a buffer
Definition: Converters.cxx:636
TNonConstCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:121
virtual PyObject * FromMemory(void *address)
assume this is a buffer access if the size is known; otherwise assume string
Definition: Converters.cxx:650
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
attempt base class first (i.e. passing a string), but if that fails, try a buffer
Definition: Converters.cxx:660
TNonConstUCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:130
TRefCppObjectConverter(Cppyy::TCppType_t klass)
Definition: Converters.h:203
Cppyy::TCppType_t fClass
Definition: Converters.h:209
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance&, set arg for call
Cppyy::TCppMethod_t fDereferencer
Definition: Converters.h:304
TSmartPtrCppObjectConverter(Cppyy::TCppType_t klass, Cppyy::TCppType_t rawPtrType, Cppyy::TCppMethod_t deref, Bool_t keepControl=kFALSE, Bool_t handlePtr=kFALSE)
Definition: Converters.h:286
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
Definition: Converters.h:300
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.h:190
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance, set arg for call
Definition: Converters.cxx:985
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
just convert pointer if it is a ROOT object
Definition: Converters.cxx:704
virtual Bool_t GetAddressSpecialCase(PyObject *pyobject, void *&address)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.cxx:674
virtual Bool_t ToMemory(PyObject *value, void *address)
just convert pointer if it is a ROOT object
Definition: Converters.cxx:752
virtual PyObject * FromMemory(void *address)
nothing sensible can be done, just return <address> as pylong
Definition: Converters.cxx:740
TVoidArrayConverter(Bool_t keepControl=kTRUE)
Definition: Converters.h:139
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
can't happen (unless a type is mapped wrongly), but implemented for completeness
Definition: Converters.cxx:477
Named parameter, streamable and storable.
Definition: TParameter.h:37
Basic string class.
Definition: TString.h:131
basic_string_view< char > string_view
TCppScope_t TCppType_t
Definition: Cppyy.h:16
ptrdiff_t TCppMethod_t
Definition: Cppyy.h:18
PYROOT_DECLARE_STRING_CONVERTER(TString, TString)
PYROOT_DECLARE_ARRAY_CONVERTER(BoolArray)
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
PYROOT_DECLARE_BASIC_CONVERTER(Long)
PYROOT_DECLARE_REF_CONVERTER(Int)
PYROOT_DECLARE_BASIC_CONVERTER2(UInt, ULong)
const char * Long
const char * ULong
const char * Float
const char * Double
const char * UChar
const char * Short
const char * Int
const char * UInt
const char * UShort
const char * Bool
const char * Char