Logo ROOT   master
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 
13 
14 namespace PyROOT {
15 
16  class ObjectProxy;
17  struct TParameter;
18  struct TCallContext;
19 
20  class TConverter {
21  public:
22  virtual ~TConverter() {}
23 
24  public:
25  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 ) = 0;
26  virtual PyObject* FromMemory( void* address );
27  virtual Bool_t ToMemory( PyObject* value, void* address );
28  };
29 
30 #define PYROOT_DECLARE_BASIC_CONVERTER( name ) \
31  class T##name##Converter : public TConverter { \
32  public: \
33  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
34  virtual PyObject* FromMemory( void* ); \
35  virtual Bool_t ToMemory( PyObject*, void* ); \
36  }; \
37  \
38  class TConst##name##RefConverter : public TConverter { \
39  public: \
40  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
41  }
42 
43 
44 #define PYROOT_DECLARE_BASIC_CONVERTER2( name, base ) \
45  class T##name##Converter : public T##base##Converter { \
46  public: \
47  virtual PyObject* FromMemory( void* ); \
48  virtual Bool_t ToMemory( PyObject*, void* ); \
49  }; \
50  \
51  class TConst##name##RefConverter : public TConverter { \
52  public: \
53  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
54  }
55 
56 #define PYROOT_DECLARE_REF_CONVERTER( name ) \
57  class T##name##RefConverter : public TConverter { \
58  public: \
59  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
60  };
61 
62 #define PYROOT_DECLARE_ARRAY_CONVERTER( name ) \
63  class T##name##Converter : public TConverter { \
64  public: \
65  T##name##Converter( Py_ssize_t size = -1 ) { fSize = size; } \
66  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
67  virtual PyObject* FromMemory( void* ); \
68  virtual Bool_t ToMemory( PyObject*, void* ); \
69  private: \
70  Py_ssize_t fSize; \
71  }; \
72  \
73  class T##name##RefConverter : public T##name##Converter { \
74  public: \
75  using T##name##Converter::T##name##Converter; \
76  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
77  }
78 
79 // converters for built-ins
90  PYROOT_DECLARE_BASIC_CONVERTER( ULongLong );
93  PYROOT_DECLARE_BASIC_CONVERTER( LongDouble );
94 
98 
99  class TVoidConverter : public TConverter {
100  public:
101  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
102  };
103 
104  class TCStringConverter : public TConverter {
105  public:
106  TCStringConverter( UInt_t maxSize = UINT_MAX ) : fMaxSize( maxSize ) {}
107 
108  public:
109  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
110  virtual PyObject* FromMemory( void* address );
111  virtual Bool_t ToMemory( PyObject* value, void* address );
112 
113  protected:
114  std::string fBuffer;
116  };
117 
119  public:
120  TNonConstCStringConverter( UInt_t maxSize = UINT_MAX ) : TCStringConverter( maxSize ) {}
121 
122  public:
123  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
124  virtual PyObject* FromMemory( void* address );
125  };
126 
128  public:
129  TNonConstUCStringConverter( UInt_t maxSize = UINT_MAX ) : TNonConstCStringConverter( maxSize ) {}
130 
131  public:
132  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
133  };
134 
135 // pointer/array conversions
137  public:
138  TVoidArrayConverter( Bool_t keepControl = kTRUE ) { fKeepControl = keepControl; }
139  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
140  virtual PyObject* FromMemory( void* address );
141  virtual Bool_t ToMemory( PyObject* value, void* address );
142 
143  protected:
144  virtual Bool_t GetAddressSpecialCase( PyObject* pyobject, void*& address );
145 
146  protected:
148 
149  private:
151  };
152 
153  PYROOT_DECLARE_ARRAY_CONVERTER( BoolArray );
154  PYROOT_DECLARE_ARRAY_CONVERTER( ShortArray );
155  PYROOT_DECLARE_ARRAY_CONVERTER( UShortArray );
156  PYROOT_DECLARE_ARRAY_CONVERTER( IntArray );
157  PYROOT_DECLARE_ARRAY_CONVERTER( UIntArray );
158  PYROOT_DECLARE_ARRAY_CONVERTER( LongArray );
159  PYROOT_DECLARE_ARRAY_CONVERTER( ULongArray );
160  PYROOT_DECLARE_ARRAY_CONVERTER( FloatArray );
161  PYROOT_DECLARE_ARRAY_CONVERTER( DoubleArray );
162 
164  public:
165  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
166  };
167 
168 // converters for special cases
170  public:
172  TVoidArrayConverter( keepControl ), fClass( klass ), fObjProxy(nullptr) {}
173 
174  public:
175  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
176  virtual PyObject* FromMemory( void* address );
177  virtual Bool_t ToMemory( PyObject* value, void* address );
178 
179  protected:
182  };
183 
185  public:
187 
188  protected:
189  virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
190  };
191 
193  public:
194  using TStrictCppObjectConverter::TStrictCppObjectConverter;
195 
196  public:
197  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
198  };
199 
201  public:
202  TRefCppObjectConverter( Cppyy::TCppType_t klass ) : fClass( klass ), fObjProxy(nullptr) {}
203 
204  public:
205  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
206 
207  protected:
210  };
211 
212  template <bool ISREFERENCE>
214  public:
216 
217  public:
218  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
219  virtual PyObject* FromMemory( void* address );
220  virtual Bool_t ToMemory( PyObject* value, void* address );
221  };
222 
223  extern template class TCppObjectPtrConverter<true>;
224  extern template class TCppObjectPtrConverter<false>;
225 
227  public:
228  TCppObjectArrayConverter( Cppyy::TCppType_t klass, size_t size, Bool_t keepControl = kFALSE ) :
229  TCppObjectConverter( klass, keepControl ), m_size( size ) {}
230 
231  public:
232  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
233  virtual PyObject* FromMemory( void* address );
234  virtual Bool_t ToMemory( PyObject* value, void* address );
235 
236  protected:
237  size_t m_size;
238  };
239 
240 // CLING WORKAROUND -- classes for STL iterators are completely undefined in that
241 // they come in a bazillion different guises, so just do whatever
243  public:
244  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
245  };
246 // -- END CLING WORKAROUND
247 
249  public:
250  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
251  };
252 
254  public:
255  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
256  virtual PyObject* FromMemory( void* address );
257  };
258 
260 
261 #define PYROOT_DECLARE_STRING_CONVERTER( name, strtype ) \
262  class T##name##Converter : public TCppObjectConverter { \
263  public: \
264  T##name##Converter( Bool_t keepControl = kTRUE ); \
265  public: \
266  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
267  virtual PyObject* FromMemory( void* address ); \
268  virtual Bool_t ToMemory( PyObject* value, void* address ); \
269  private: \
270  strtype fBuffer; \
271  }
272 
274  PYROOT_DECLARE_STRING_CONVERTER( STLString, std::string );
275  PYROOT_DECLARE_STRING_CONVERTER( STLStringView, std::string_view );
276 
278  public:
279  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* = 0 );
280  };
281 
282 // smart pointer converter
284  public:
286  Cppyy::TCppType_t rawPtrType,
287  Cppyy::TCppMethod_t deref,
288  Bool_t keepControl = kFALSE,
289  Bool_t handlePtr = kFALSE )
290  : fClass( klass ), fRawPtrType( rawPtrType ), fDereferencer( deref ),
291  fKeepControl( keepControl ), fHandlePtr( handlePtr ) {}
292 
293  public:
294  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
295  virtual PyObject* FromMemory( void* address );
296  //virtual Bool_t ToMemory( PyObject* value, void* address );
297 
298  protected:
299  virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
300 
306  };
307 
308 // create converter from fully qualified type
309  TConverter* CreateConverter( const std::string& fullType, Long_t size = -1 );
310 
311 } // namespace PyROOT
312 
313 #endif // !PYROOT_CONVERTERS_H
Cppyy::TCppType_t fClass
Definition: Converters.h:180
TCppScope_t TCppType_t
Definition: Cppyy.h:16
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:146
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
Definition: Converters.cxx:579
const char * Int
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance**, set arg for call
virtual Bool_t ToMemory(PyObject *value, void *address)
just convert pointer if it is a ROOT object
Definition: Converters.cxx:799
virtual PyObject * FromMemory(void *address)
nothing sensible can be done, just return <address> as pylong
Definition: Converters.cxx:787
TVoidArrayConverter(Bool_t keepControl=kTRUE)
Definition: Converters.h:138
const char * Long
Basic string class.
Definition: TString.h:131
const char * Char
Cppyy::TCppMethod_t fDereferencer
Definition: Converters.h:303
const char * UShort
const char * UInt
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:626
PYROOT_DECLARE_REF_CONVERTER(Int)
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:609
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:136
TRefCppObjectConverter(Cppyy::TCppType_t klass)
Definition: Converters.h:202
TNonConstUCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:129
TCppObjectConverter(Cppyy::TCppType_t klass, Bool_t keepControl=kFALSE)
Definition: Converters.h:171
const char * UChar
const char * Float
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
virtual ~TConverter()
Definition: Converters.h:22
TSmartPtrCppObjectConverter(Cppyy::TCppType_t klass, Cppyy::TCppType_t rawPtrType, Cppyy::TCppMethod_t deref, Bool_t keepControl=kFALSE, Bool_t handlePtr=kFALSE)
Definition: Converters.h:285
Named parameter, streamable and storable.
Definition: TParameter.h:37
virtual Bool_t ToMemory(PyObject *value, void *address)
convert to C++ instance*, write it at <address>
ptrdiff_t TCppMethod_t
Definition: Cppyy.h:18
virtual Bool_t GetAddressSpecialCase(PyObject *pyobject, void *&address)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.cxx:711
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:697
virtual Bool_t ToMemory(PyObject *value, void *address)
convert to C++ instance, write it at <address>
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.h:189
PYROOT_DECLARE_BASIC_CONVERTER2(UInt, ULong)
Cppyy::TCppType_t fClass
Definition: Converters.h:208
virtual PyObject * FromMemory(void *address)
assume this is a buffer access if the size is known; otherwise assume string
Definition: Converters.cxx:687
PYROOT_DECLARE_ARRAY_CONVERTER(BoolArray)
unsigned int UInt_t
Definition: RtypesCore.h:42
const char * ULong
const char * Double
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance*, set arg for call
Definition: Converters.cxx:966
virtual PyObject * FromMemory(void *address)
construct python object from C++ instance* read at <address>
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)=0
PYROOT_DECLARE_BASIC_CONVERTER(Long)
TCppObjectArrayConverter(Cppyy::TCppType_t klass, size_t size, Bool_t keepControl=kFALSE)
Definition: Converters.h:228
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:673
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
just convert pointer if it is a ROOT object
Definition: Converters.cxx:751
virtual PyObject * FromMemory(void *address)
construct python object from C++ instance read at <address>
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance, set arg for call
const char * Bool
PYROOT_DECLARE_STRING_CONVERTER(TString, TString)
TCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:106
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
Definition: Converters.h:299
TNonConstCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:120
const Bool_t kTRUE
Definition: RtypesCore.h:87
_object PyObject
Definition: TPyArg.h:20
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
convert pyobject to C++ instance&, set arg for call
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
can&#39;t happen (unless a type is mapped wrongly), but implemented for completeness
Definition: Converters.cxx:493
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)
just convert pointer if it is a ROOT object
const char * Short