Logo ROOT   6.12/07
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 
15 namespace 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
91  PYROOT_DECLARE_BASIC_CONVERTER( ULongLong );
94  PYROOT_DECLARE_BASIC_CONVERTER( LongDouble );
95 
99 
100  class TVoidConverter : public TConverter {
101  public:
102  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
103  };
104 
105  class TCStringConverter : public TConverter {
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:
148  Bool_t KeepControl() { return fKeepControl; }
149 
150  private:
152  };
153 
154  PYROOT_DECLARE_ARRAY_CONVERTER( BoolArray );
155  PYROOT_DECLARE_ARRAY_CONVERTER( ShortArray );
156  PYROOT_DECLARE_ARRAY_CONVERTER( UShortArray );
157  PYROOT_DECLARE_ARRAY_CONVERTER( IntArray );
158  PYROOT_DECLARE_ARRAY_CONVERTER( UIntArray );
159  PYROOT_DECLARE_ARRAY_CONVERTER( LongArray );
160  PYROOT_DECLARE_ARRAY_CONVERTER( ULongArray );
161  PYROOT_DECLARE_ARRAY_CONVERTER( FloatArray );
162  PYROOT_DECLARE_ARRAY_CONVERTER( DoubleArray );
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:
203  TRefCppObjectConverter( Cppyy::TCppType_t klass ) : fClass( klass ), fObjProxy(nullptr) {}
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,
288  Cppyy::TCppMethod_t deref,
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
Cppyy::TCppType_t fClass
Definition: Converters.h:181
TCppScope_t TCppType_t
Definition: Cppyy.h:13
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:145
basic_string_view< char > string_view
Definition: RStringView.h:35
const char * Int
TVoidArrayConverter(Bool_t keepControl=kTRUE)
Definition: Converters.h:139
const char * Long
Basic string class.
Definition: TString.h:125
bool Bool_t
Definition: RtypesCore.h:59
const char * Char
Cppyy::TCppMethod_t fDereferencer
Definition: Converters.h:304
const char * UShort
const char * UInt
PYROOT_DECLARE_REF_CONVERTER(Int)
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:135
TRefCppObjectConverter(Cppyy::TCppType_t klass)
Definition: Converters.h:203
TNonConstUCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:130
TCppObjectConverter(Cppyy::TCppType_t klass, Bool_t keepControl=kFALSE)
Definition: Converters.h:172
const char * UChar
const char * Float
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
virtual ~TConverter()
Definition: Converters.h:23
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
Named parameter, streamable and storable.
Definition: TParameter.h:37
ptrdiff_t TCppMethod_t
Definition: Cppyy.h:15
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.h:190
PYROOT_DECLARE_BASIC_CONVERTER2(UInt, ULong)
Cppyy::TCppType_t fClass
Definition: Converters.h:209
PYROOT_DECLARE_ARRAY_CONVERTER(BoolArray)
unsigned int UInt_t
Definition: RtypesCore.h:42
const char * ULong
const char * Double
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:229
const char * Bool
PYROOT_DECLARE_STRING_CONVERTER(TString, TString)
TCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:107
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
Definition: Converters.h:300
TNonConstCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:121
const Bool_t kTRUE
Definition: RtypesCore.h:87
_object PyObject
Definition: TPyArg.h:20
const char * Short