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*, PyObject* = nullptr); \
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*, PyObject* = nullptr); \
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, bool init = true); \
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*, PyObject* = nullptr); \
65  virtual bool HasState() { return true; } \
66 protected: \
67  Py_ssize_t* fShape; \
68  bool fIsFixed; \
69 }; \
70  \
71 class name##ArrayPtrConverter : public name##ArrayConverter { \
72 public: \
73  using name##ArrayConverter::name##ArrayConverter; \
74  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
75 };
76 
77 
78 // converters for built-ins
83 class UCharAsIntConverter : public UCharConverter {
84 public:
85  using UCharConverter::UCharConverter;
86  virtual PyObject* FromMemory(void*);
87 };
103 
124 
125 class VoidConverter : public Converter {
126 public:
127  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
128 };
129 
130 class CStringConverter : public Converter {
131 public:
132  CStringConverter(long maxSize = -1) : fMaxSize(maxSize) {}
133 
134 public:
135  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
136  virtual PyObject* FromMemory(void* address);
137  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
138  virtual bool HasState() { return true; }
139 
140 protected:
141  std::string fBuffer;
142  long fMaxSize;
143 };
144 
145 class NonConstCStringConverter : public CStringConverter {
146 public:
147  NonConstCStringConverter(long maxSize = -1) : CStringConverter(maxSize) {}
148 
149 public:
150  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
151  virtual PyObject* FromMemory(void* address);
152 };
153 
154 class WCStringConverter : public Converter {
155 public:
156  WCStringConverter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
157  WCStringConverter(const WCStringConverter&) = delete;
158  WCStringConverter& operator=(const WCStringConverter&) = delete;
159  virtual ~WCStringConverter() { free(fBuffer); }
160 
161 public:
162  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
163  virtual PyObject* FromMemory(void* address);
164  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
165  virtual bool HasState() { return true; }
166 
167 protected:
168  wchar_t* fBuffer;
169  long fMaxSize;
170 };
171 
172 class CString16Converter : public Converter {
173 public:
174  CString16Converter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
175  CString16Converter(const CString16Converter&) = delete;
176  CString16Converter& operator=(const CString16Converter&) = delete;
177  virtual ~CString16Converter() { free(fBuffer); }
178 
179 public:
180  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
181  virtual PyObject* FromMemory(void* address);
182  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
183  virtual bool HasState() { return true; }
184 
185 protected:
186  char16_t* fBuffer;
187  long fMaxSize;
188 };
189 
190 class CString32Converter : public Converter {
191 public:
192  CString32Converter(long maxSize = -1) : fBuffer(nullptr), fMaxSize(maxSize) {}
193  CString32Converter(const CString32Converter&) = delete;
194  CString32Converter& operator=(const CString32Converter&) = delete;
195  virtual ~CString32Converter() { free(fBuffer); }
196 
197 public:
198  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
199  virtual PyObject* FromMemory(void* address);
200  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
201  virtual bool HasState() { return true; }
202 
203 protected:
204  char32_t* fBuffer;
205  long fMaxSize;
206 };
207 
208 // pointer/array conversions
212 #if __cplusplus > 201402L
214 #endif
227 
228 class CStringArrayConverter : public SCharArrayPtrConverter {
229 public:
230  using SCharArrayPtrConverter::SCharArrayPtrConverter;
231  virtual PyObject* FromMemory(void* address);
232 };
233 
234 
235 // converters for special cases
236 class NullptrConverter : public Converter {
237 public:
238  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
239 };
240 
241 class InstanceConverter : public StrictInstancePtrConverter {
242 public:
243  using StrictInstancePtrConverter::StrictInstancePtrConverter;
244  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
245  virtual PyObject* FromMemory(void*);
246  virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
247 };
248 
249 class InstanceRefConverter : public Converter {
250 public:
251  InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
252  fClass(klass), fIsConst(isConst) {}
253 
254 public:
255  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
256  virtual PyObject* FromMemory(void* address);
257  virtual bool HasState() { return true; }
258 
259 protected:
261  bool fIsConst;
262 };
263 
264 class InstanceMoveConverter : public InstanceRefConverter {
265 public:
266  InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
267  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
268 };
269 
270 template <bool ISREFERENCE>
271 class InstancePtrPtrConverter : public InstancePtrConverter {
272 public:
274 
275 public:
276  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
277  virtual PyObject* FromMemory(void* address);
278  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
279 };
280 
281 class InstanceArrayConverter : public InstancePtrConverter {
282 public:
283  InstanceArrayConverter(Cppyy::TCppType_t klass, dims_t dims, bool keepControl = false) :
284  InstancePtrConverter(klass, keepControl) {
285  dim_t size = (dims && 0 < dims[0]) ? dims[0]+1: 1;
286  m_dims = new dim_t[size];
287  if (dims) {
288  for (int i = 0; i < size; ++i) m_dims[i] = dims[i];
289  } else {
290  m_dims[0] = -1;
291  }
292  }
293  InstanceArrayConverter(const InstanceArrayConverter&) = delete;
294  InstanceArrayConverter& operator=(const InstanceArrayConverter&) = delete;
295  virtual ~InstanceArrayConverter() { delete [] m_dims; }
296 
297 public:
298  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
299  virtual PyObject* FromMemory(void* address);
300  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
301 
302 protected:
304 };
305 
306 
307 class ComplexDConverter: public InstanceConverter {
308 public:
309  ComplexDConverter(bool keepControl = false);
310 
311 public:
312  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
313  virtual PyObject* FromMemory(void* address);
314  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
315 
316 private:
317  std::complex<double> fBuffer;
318 };
319 
320 
321 // CLING WORKAROUND -- classes for STL iterators are completely undefined in that
322 // they come in a bazillion different guises, so just do whatever
323 class STLIteratorConverter : public Converter {
324 public:
325  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
326 };
327 // -- END CLING WORKAROUND
328 
329 
330 class VoidPtrRefConverter : public Converter {
331 public:
332  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
333 };
334 
335 class VoidPtrPtrConverter : public Converter {
336 public:
337  VoidPtrPtrConverter(size_t size) { fSize = size; }
338  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
339  virtual PyObject* FromMemory(void* address);
340  virtual bool HasState() { return true; }
341 
342 protected:
343  size_t fSize;
344 };
345 
347 
348 
349 #define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
350 class name##Converter : public InstanceConverter { \
351 public: \
352  name##Converter(bool keepControl = true); \
353 public: \
354  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
355  virtual PyObject* FromMemory(void* address); \
356  virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
357 protected: \
358  strtype fBuffer; \
359 }
360 
362 CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
364 class STLStringViewConverter : public STLStringViewBaseConverter {
365 public:
366  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
367 };
368 CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
369 
370 class STLStringMoveConverter : public STLStringConverter {
371 public:
372  using STLStringConverter::STLStringConverter;
373 
374 public:
375  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
376 };
377 
378 
379 // function pointers
380 class FunctionPointerConverter : public Converter {
381 public:
382  FunctionPointerConverter(const std::string& ret, const std::string& sig) :
383  fRetType(ret), fSignature(sig) {}
384 
385 public:
386  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
387  virtual PyObject* FromMemory(void* address);
388  virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
389  virtual bool HasState() { return true; }
390 
391 protected:
392  std::string fRetType;
393  std::string fSignature;
394 };
395 
396 // std::function
397 class StdFunctionConverter : public FunctionPointerConverter {
398 public:
399  StdFunctionConverter(Converter* cnv, const std::string& ret, const std::string& sig) :
400  FunctionPointerConverter(ret, sig), fConverter(cnv), fFuncWrap(nullptr) {}
401  StdFunctionConverter(const StdFunctionConverter&) = delete;
402  StdFunctionConverter& operator=(const StdFunctionConverter&) = delete;
403  virtual ~StdFunctionConverter() { Py_XDECREF(fFuncWrap); delete fConverter; }
404 
405 public:
406  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
407  virtual PyObject* FromMemory(void* address);
408  virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
409 
410 protected:
411  Converter* fConverter;
413 };
414 
415 
416 // smart pointer converter
417 class SmartPtrConverter : public Converter {
418 public:
419  SmartPtrConverter(Cppyy::TCppType_t smart,
420  Cppyy::TCppType_t underlying,
421  bool keepControl = false,
422  bool isRef = false)
423  : fSmartPtrType(smart), fUnderlyingType(underlying),
424  fKeepControl(keepControl), fIsRef(isRef) {}
425 
426 public:
427  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
428  virtual PyObject* FromMemory(void* address);
429  //virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
430  virtual bool HasState() { return true; }
431 
432 protected:
433  virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
434 
438  bool fIsRef;
439 };
440 
441 
442 // initializer lists
443 class InitializerListConverter : public Converter {
444 public:
445  InitializerListConverter(Converter* cnv, size_t sz) :
446  fConverter(cnv), fValueSize(sz) {}
447  InitializerListConverter(const InitializerListConverter&) = delete;
448  InitializerListConverter& operator=(const InitializerListConverter&) = delete;
449  virtual ~InitializerListConverter();
450 
451 public:
452  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
453  virtual bool HasState() { return true; }
454 
455 protected:
456  Converter* fConverter;
457  size_t fValueSize;
458 };
459 
460 
461 // raising converter to take out overloads
462 class NotImplementedConverter : public Converter {
463 public:
464  virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
465 };
466 
467 } // unnamed namespace
468 
469 } // namespace CPyCppyy
470 
471 #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:141
string_view
basic_string_view< char > string_view
Definition: libcpp_string_view.h:786
operator=
Binding & operator=(OUT(*fun)(void))
Definition: TRInterface_Binding.h:15
CPPYY_DECLARE_STRING_CONVERTER
#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype)
Definition: DeclareConverters.h:349
fRetType
std::string fRetType
Definition: DeclareConverters.h:392
CPPYY_DECLARE_REFCONVERTER
#define CPPYY_DECLARE_REFCONVERTER(name)
Definition: DeclareConverters.h:48
sqlio::UInt
const char * UInt
Definition: TSQLStructure.cxx:79
TString
Basic string class.
Definition: TString.h:136
TString.h
fFuncWrap
PyObject * fFuncWrap
Definition: DeclareConverters.h:412
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
size
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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:303
fIsConst
bool fIsConst
Definition: DeclareConverters.h:261
fClass
Cppyy::TCppType_t fClass
Definition: DeclareConverters.h:260
fKeepControl
bool fKeepControl
Definition: DeclareConverters.h:437
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:411
sqlio::ULong
const char * ULong
Definition: TSQLStructure.cxx:80
fUnderlyingType
Cppyy::TCppType_t fUnderlyingType
Definition: DeclareConverters.h:436
fIsRef
bool fIsRef
Definition: DeclareConverters.h:438
fSmartPtrType
Cppyy::TCppType_t fSmartPtrType
Definition: DeclareConverters.h:435
fValueSize
size_t fValueSize
Definition: DeclareConverters.h:457
fSize
size_t fSize
Definition: DeclareConverters.h:343
fSignature
std::string fSignature
Definition: DeclareConverters.h:393
sqlio::Char
const char * Char
Definition: TSQLStructure.cxx:70
free
#define free
Definition: civetweb.c:1539
fMaxSize
long fMaxSize
Definition: DeclareConverters.h:142
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