Logo ROOT  
Reference Guide
DeclareConverters.h
Go to the documentation of this file.
1 #ifndef CPYCPPYY_DECLARECONVERTERS_H
2 #define CPYCPPYY_DECLARECONVERTERS_H
3 
4 // Bindings
5 #include "Converters.h"
6 
7 // Standard
8 #include <complex>
9 #include <string>
10 
11 // ROOT
12 #include "ROOT/RStringView.hxx"
13 #include "TString.h"
14 
15 
16 namespace CPyCppyy {
17 
18 namespace {
19 
20 #define CPPYY_DECLARE_BASIC_CONVERTER(name) \
21 class name##Converter : public Converter { \
22 public: \
23  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
24  virtual PyObject* FromMemory(void*); \
25  virtual bool ToMemory(PyObject*, void*); \
26 }; \
27  \
28 class Const##name##RefConverter : public Converter { \
29 public: \
30  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
31  virtual PyObject* FromMemory(void*); \
32 }
33 
34 
35 #define CPPYY_DECLARE_BASIC_CONVERTER2(name, base) \
36 class name##Converter : public base##Converter { \
37 public: \
38  virtual PyObject* FromMemory(void*); \
39  virtual bool ToMemory(PyObject*, void*); \
40 }; \
41  \
42 class Const##name##RefConverter : public Converter { \
43 public: \
44  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
45  virtual PyObject* FromMemory(void*); \
46 }
47 
48 #define CPPYY_DECLARE_REFCONVERTER(name) \
49 class name##RefConverter : public Converter { \
50 public: \
51  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
52  virtual PyObject* FromMemory(void*); \
53 };
54 
55 #define CPPYY_DECLARE_ARRAY_CONVERTER(name) \
56 class name##ArrayConverter : public Converter { \
57 public: \
58  name##ArrayConverter(dims_t shape); \
59  name##ArrayConverter(const name##ArrayConverter&) = delete; \
60  name##ArrayConverter& operator=(const name##ArrayConverter&) = delete; \
61  virtual ~name##ArrayConverter() { delete [] fShape; } \
62  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
63  virtual PyObject* FromMemory(void*); \
64  virtual bool ToMemory(PyObject*, void*); \
65  virtual bool HasState() { return true; } \
66 protected: \
67  Py_ssize_t* fShape; \
68 }; \
69  \
70 class name##ArrayPtrConverter : public name##ArrayConverter { \
71 public: \
72  using name##ArrayConverter::name##ArrayConverter; \
73  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
74 };
75 
76 
77 // converters for built-ins
82 class UCharAsIntConverter : public UCharConverter {
83 public:
84  using UCharConverter::UCharConverter;
85  virtual PyObject* FromMemory(void*);
86 };
102 
123 
124 class VoidConverter : public Converter {
125 public:
126  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
127 };
128 
129 class CStringConverter : public Converter {
130 public:
131  CStringConverter(long maxSize = -1) : fMaxSize(maxSize) {}
132 
133 public:
134  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
135  virtual PyObject* FromMemory(void* address);
136  virtual bool ToMemory(PyObject* value, void* address);
137  virtual bool HasState() { return true; }
138 
139 protected:
140  std::string fBuffer;
141  long fMaxSize;
142 };
143 
144 class NonConstCStringConverter : public CStringConverter {
145 public:
146  NonConstCStringConverter(long maxSize = -1) : CStringConverter(maxSize) {}
147 
148 public:
149  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
150  virtual PyObject* FromMemory(void* address);
151 };
152 
153 class WCStringConverter : public Converter {
154 public:
155  WCStringConverter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
156  WCStringConverter(const WCStringConverter&) = delete;
157  WCStringConverter& operator=(const WCStringConverter&) = delete;
158  virtual ~WCStringConverter() { free(fBuffer); }
159 
160 public:
161  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
162  virtual PyObject* FromMemory(void* address);
163  virtual bool ToMemory(PyObject* value, void* address);
164  virtual bool HasState() { return true; }
165 
166 protected:
167  wchar_t* fBuffer;
168  long fMaxSize;
169 };
170 
171 class CString16Converter : public Converter {
172 public:
173  CString16Converter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
174  CString16Converter(const CString16Converter&) = delete;
175  CString16Converter& operator=(const CString16Converter&) = delete;
176  virtual ~CString16Converter() { free(fBuffer); }
177 
178 public:
179  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
180  virtual PyObject* FromMemory(void* address);
181  virtual bool ToMemory(PyObject* value, void* address);
182  virtual bool HasState() { return true; }
183 
184 protected:
185  char16_t* fBuffer;
186  long fMaxSize;
187 };
188 
189 class CString32Converter : public Converter {
190 public:
191  CString32Converter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
192  CString32Converter(const CString32Converter&) = delete;
193  CString32Converter& operator=(const CString32Converter&) = delete;
194  virtual ~CString32Converter() { free(fBuffer); }
195 
196 public:
197  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
198  virtual PyObject* FromMemory(void* address);
199  virtual bool ToMemory(PyObject* value, void* address);
200  virtual bool HasState() { return true; }
201 
202 protected:
203  char32_t* fBuffer;
204  long fMaxSize;
205 };
206 
207 // pointer/array conversions
211 #if __cplusplus > 201402L
213 #endif
226 
227 class CStringArrayConverter : public SCharArrayPtrConverter {
228 public:
229  using SCharArrayPtrConverter::SCharArrayPtrConverter;
230  virtual PyObject* FromMemory(void* address);
231 };
232 
233 
234 // converters for special cases
235 class NullptrConverter : public Converter {
236 public:
237  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
238 };
239 
240 class InstanceConverter : public StrictInstancePtrConverter {
241 public:
242  using StrictInstancePtrConverter::StrictInstancePtrConverter;
243  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
244  virtual PyObject* FromMemory(void*);
245  virtual bool ToMemory(PyObject*, void*);
246 };
247 
248 class InstanceRefConverter : public Converter {
249 public:
250  InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
251  fClass(klass), fIsConst(isConst) {}
252 
253 public:
254  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
255  virtual PyObject* FromMemory(void* address);
256  virtual bool HasState() { return true; }
257 
258 protected:
260  bool fIsConst;
261 };
262 
263 class InstanceMoveConverter : public InstanceRefConverter {
264 public:
265  InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
266  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
267 };
268 
269 template <bool ISREFERENCE>
270 class InstancePtrPtrConverter : public InstancePtrConverter {
271 public:
273 
274 public:
275  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
276  virtual PyObject* FromMemory(void* address);
277  virtual bool ToMemory(PyObject* value, void* address);
278 };
279 
280 class InstanceArrayConverter : public InstancePtrConverter {
281 public:
282  InstanceArrayConverter(Cppyy::TCppType_t klass, dims_t dims, bool keepControl = false) :
283  InstancePtrConverter(klass, keepControl) {
284  dim_t size = (dims && 0 < dims[0]) ? dims[0]+1: 1;
285  m_dims = new dim_t[size];
286  if (dims) {
287  for (int i = 0; i < size; ++i) m_dims[i] = dims[i];
288  } else {
289  m_dims[0] = -1;
290  }
291  }
292  InstanceArrayConverter(const InstanceArrayConverter&) = delete;
293  InstanceArrayConverter& operator=(const InstanceArrayConverter&) = delete;
294  virtual ~InstanceArrayConverter() { delete [] m_dims; }
295 
296 public:
297  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
298  virtual PyObject* FromMemory(void* address);
299  virtual bool ToMemory(PyObject* value, void* address);
300 
301 protected:
303 };
304 
305 
306 class ComplexDConverter: public InstanceConverter {
307 public:
308  ComplexDConverter(bool keepControl = false);
309 
310 public:
311  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
312  virtual PyObject* FromMemory(void* address);
313  virtual bool ToMemory(PyObject* value, void* address);
314 
315 private:
316  std::complex<double> fBuffer;
317 };
318 
319 
320 // CLING WORKAROUND -- classes for STL iterators are completely undefined in that
321 // they come in a bazillion different guises, so just do whatever
322 class STLIteratorConverter : public Converter {
323 public:
324  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
325 };
326 // -- END CLING WORKAROUND
327 
328 
329 class VoidPtrRefConverter : public Converter {
330 public:
331  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
332 };
333 
334 class VoidPtrPtrConverter : public Converter {
335 public:
336  VoidPtrPtrConverter(size_t size) { fSize = size; }
337  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
338  virtual PyObject* FromMemory(void* address);
339  virtual bool HasState() { return true; }
340 
341 protected:
342  size_t fSize;
343 };
344 
346 
347 
348 #define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
349 class name##Converter : public InstanceConverter { \
350 public: \
351  name##Converter(bool keepControl = true); \
352 public: \
353  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
354  virtual PyObject* FromMemory(void* address); \
355  virtual bool ToMemory(PyObject* value, void* address); \
356 protected: \
357  strtype fBuffer; \
358 }
359 
361 CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
363 class STLStringViewConverter : public STLStringViewBaseConverter {
364 public:
365  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
366 };
367 CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
368 
369 class STLStringMoveConverter : public STLStringConverter {
370 public:
371  using STLStringConverter::STLStringConverter;
372 
373 public:
374  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
375 };
376 
377 
378 // function pointers
379 class FunctionPointerConverter : public Converter {
380 public:
381  FunctionPointerConverter(const std::string& ret, const std::string& sig) :
382  fRetType(ret), fSignature(sig) {}
383 
384 public:
385  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
386  virtual PyObject* FromMemory(void* address);
387  virtual bool ToMemory(PyObject*, void*);
388  virtual bool HasState() { return true; }
389 
390 protected:
391  std::string fRetType;
392  std::string fSignature;
393 };
394 
395 // std::function
396 class StdFunctionConverter : public FunctionPointerConverter {
397 public:
398  StdFunctionConverter(Converter* cnv, const std::string& ret, const std::string& sig) :
399  FunctionPointerConverter(ret, sig), fConverter(cnv), fFuncWrap(nullptr) {}
400  StdFunctionConverter(const StdFunctionConverter&) = delete;
401  StdFunctionConverter& operator=(const StdFunctionConverter&) = delete;
402  virtual ~StdFunctionConverter() { Py_XDECREF(fFuncWrap); delete fConverter; }
403 
404 public:
405  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
406  virtual PyObject* FromMemory(void* address);
407  virtual bool ToMemory(PyObject* value, void* address);
408 
409 protected:
410  Converter* fConverter;
412 };
413 
414 
415 // smart pointer converter
416 class SmartPtrConverter : public Converter {
417 public:
418  SmartPtrConverter(Cppyy::TCppType_t smart,
419  Cppyy::TCppType_t underlying,
420  bool keepControl = false,
421  bool isRef = false)
422  : fSmartPtrType(smart), fUnderlyingType(underlying),
423  fKeepControl(keepControl), fIsRef(isRef) {}
424 
425 public:
426  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
427  virtual PyObject* FromMemory(void* address);
428  //virtual bool ToMemory(PyObject* value, void* address);
429  virtual bool HasState() { return true; }
430 
431 protected:
432  virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
433 
437  bool fIsRef;
438 };
439 
440 
441 // initializer lists
442 class InitializerListConverter : public Converter {
443 public:
444  InitializerListConverter(Converter* cnv, size_t sz) :
445  fConverter(cnv), fValueSize(sz) {}
446  InitializerListConverter(const InitializerListConverter&) = delete;
447  InitializerListConverter& operator=(const InitializerListConverter&) = delete;
448  virtual ~InitializerListConverter();
449 
450 public:
451  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
452  virtual bool HasState() { return true; }
453 
454 protected:
455  Converter* fConverter;
456  size_t fValueSize;
457 };
458 
459 
460 // raising converter to take out overloads
461 class NotImplementedConverter : public Converter {
462 public:
463  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
464 };
465 
466 } // unnamed namespace
467 
468 } // namespace CPyCppyy
469 
470 #endif // !CPYCPPYY_DECLARECONVERTERS_H
CPyCppyy
Set of helper functions that are invoked from the pythonizors, on the Python side.
Definition: TPyClassGenerator.cxx:31
sqlio::Bool
const char * Bool
Definition: TSQLStructure.cxx:69
PyObject
_object PyObject
Definition: PyMethodBase.h:42
fBuffer
std::string fBuffer
Definition: DeclareConverters.h:140
string_view
basic_string_view< char > string_view
Definition: libcpp_string_view.h:785
operator=
Binding & operator=(OUT(*fun)(void))
Definition: TRInterface_Binding.h:11
CPPYY_DECLARE_STRING_CONVERTER
#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype)
Definition: DeclareConverters.h:348
fRetType
std::string fRetType
Definition: DeclareConverters.h:391
CPPYY_DECLARE_REFCONVERTER
#define CPPYY_DECLARE_REFCONVERTER(name)
Definition: DeclareConverters.h:48
sqlio::UInt
const char * UInt
Definition: TSQLStructure.cxx:79
TString
Definition: TString.h:136
TString.h
fFuncWrap
PyObject * fFuncWrap
Definition: DeclareConverters.h:411
sqlio::Double
const char * Double
Definition: TSQLStructure.cxx:76
dim_t
Py_ssize_t dim_t
Definition: CPyCppyy.h:64
sqlio::Int
const char * Int
Definition: TSQLStructure.cxx:72
sqlio::UShort
const char * UShort
Definition: TSQLStructure.cxx:78
sqlio::Short
const char * Short
Definition: TSQLStructure.cxx:71
CPPYY_DECLARE_ARRAY_CONVERTER
#define CPPYY_DECLARE_ARRAY_CONVERTER(name)
Definition: DeclareConverters.h:55
RStringView.hxx
sqlio::Long
const char * Long
Definition: TSQLStructure.cxx:73
m_dims
dims_t m_dims
Definition: DeclareConverters.h:302
fIsConst
bool fIsConst
Definition: DeclareConverters.h:260
fClass
Cppyy::TCppType_t fClass
Definition: DeclareConverters.h:259
fKeepControl
bool fKeepControl
Definition: DeclareConverters.h:436
Cppyy::TCppType_t
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
sqlio::UChar
const char * UChar
Definition: TSQLStructure.cxx:77
CPPYY_DECLARE_BASIC_CONVERTER2
#define CPPYY_DECLARE_BASIC_CONVERTER2(name, base)
Definition: DeclareConverters.h:35
sqlio::Float
const char * Float
Definition: TSQLStructure.cxx:75
fConverter
Converter * fConverter
Definition: DeclareConverters.h:410
sqlio::ULong
const char * ULong
Definition: TSQLStructure.cxx:80
fUnderlyingType
Cppyy::TCppType_t fUnderlyingType
Definition: DeclareConverters.h:435
fIsRef
bool fIsRef
Definition: DeclareConverters.h:437
fSmartPtrType
Cppyy::TCppType_t fSmartPtrType
Definition: DeclareConverters.h:434
fValueSize
size_t fValueSize
Definition: DeclareConverters.h:456
fSize
size_t fSize
Definition: DeclareConverters.h:342
fSignature
std::string fSignature
Definition: DeclareConverters.h:392
sqlio::Char
const char * Char
Definition: TSQLStructure.cxx:70
free
#define free
Definition: civetweb.c:1539
fMaxSize
long fMaxSize
Definition: DeclareConverters.h:141
CPyCppyy::InstancePtrConverter::InstancePtrConverter
InstancePtrConverter(Cppyy::TCppType_t klass, bool keepControl=false)
Definition: Converters.h:53
CPyCppyy::Parameter
Definition: callcontext.h:13
CPPYY_DECLARE_BASIC_CONVERTER
#define CPPYY_DECLARE_BASIC_CONVERTER(name)
Definition: DeclareConverters.h:20
Converters.h
int