Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
16namespace CPyCppyy {
17
18namespace {
19
20#define CPPYY_DECLARE_BASIC_CONVERTER(name) \
21class name##Converter : public Converter { \
22public: \
23 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
24 virtual PyObject* FromMemory(void*); \
25 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
26}; \
27 \
28class Const##name##RefConverter : public Converter { \
29public: \
30 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
31 virtual PyObject* FromMemory(void*); \
32}
33
34
35#define CPPYY_DECLARE_BASIC_CONVERTER2(name, base) \
36class name##Converter : public base##Converter { \
37public: \
38 virtual PyObject* FromMemory(void*); \
39 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
40}; \
41 \
42class Const##name##RefConverter : public Converter { \
43public: \
44 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
45 virtual PyObject* FromMemory(void*); \
46}
47
48#define CPPYY_DECLARE_REFCONVERTER(name) \
49class name##RefConverter : public Converter { \
50public: \
51 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
52 virtual PyObject* FromMemory(void*); \
53};
54
55#define CPPYY_DECLARE_ARRAY_CONVERTER(name) \
56class name##ArrayConverter : public Converter { \
57public: \
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; } \
66protected: \
67 Py_ssize_t* fShape; \
68 bool fIsFixed; \
69}; \
70 \
71class name##ArrayPtrConverter : public name##ArrayConverter { \
72public: \
73 using name##ArrayConverter::name##ArrayConverter; \
74 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
75};
76
77
78// converters for built-ins
83class UCharAsIntConverter : public UCharConverter {
84public:
85 using UCharConverter::UCharConverter;
86 virtual PyObject* FromMemory(void*);
87};
103
124
125class VoidConverter : public Converter {
126public:
127 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
128};
129
130class CStringConverter : public Converter {
131public:
132 CStringConverter(long maxSize = -1) : fMaxSize(maxSize) {}
133
134public:
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
140protected:
141 std::string fBuffer;
143};
144
145class NonConstCStringConverter : public CStringConverter {
146public:
147 NonConstCStringConverter(long maxSize = -1) : CStringConverter(maxSize) {}
148
149public:
150 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
151 virtual PyObject* FromMemory(void* address);
152};
153
154class WCStringConverter : public Converter {
155public:
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
161public:
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
167protected:
168 wchar_t* fBuffer;
169 long fMaxSize;
170};
171
172class CString16Converter : public Converter {
173public:
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
179public:
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
185protected:
186 char16_t* fBuffer;
187 long fMaxSize;
188};
189
190class CString32Converter : public Converter {
191public:
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
197public:
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
203protected:
204 char32_t* fBuffer;
205 long fMaxSize;
206};
207
208// pointer/array conversions
212#if __cplusplus > 201402L
214#endif
227
228class CStringArrayConverter : public SCharArrayPtrConverter {
229public:
230 using SCharArrayPtrConverter::SCharArrayPtrConverter;
231 virtual PyObject* FromMemory(void* address);
232};
233
234
235// converters for special cases
236class NullptrConverter : public Converter {
237public:
238 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
239};
240
241class InstanceConverter : public StrictInstancePtrConverter {
242public:
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
249class InstanceRefConverter : public Converter {
250public:
251 InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
252 fClass(klass), fIsConst(isConst) {}
253
254public:
255 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
256 virtual PyObject* FromMemory(void* address);
257 virtual bool HasState() { return true; }
258
259protected:
262};
263
264class InstanceMoveConverter : public InstanceRefConverter {
265public:
266 InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
267 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
268};
269
270template <bool ISREFERENCE>
271class InstancePtrPtrConverter : public InstancePtrConverter {
272public:
274
275public:
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
281class InstanceArrayConverter : public InstancePtrConverter {
282public:
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
297public:
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
302protected:
304};
305
306
307class ComplexDConverter: public InstanceConverter {
308public:
309 ComplexDConverter(bool keepControl = false);
310
311public:
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
316private:
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
323class STLIteratorConverter : public Converter {
324public:
325 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
326};
327// -- END CLING WORKAROUND
328
329
330class VoidPtrRefConverter : public Converter {
331public:
332 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
333};
334
335class VoidPtrPtrConverter : public Converter {
336public:
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
342protected:
343 size_t fSize;
344};
345
347
348
349#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
350class name##Converter : public InstanceConverter { \
351public: \
352 name##Converter(bool keepControl = true); \
353public: \
354 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
355 virtual PyObject* FromMemory(void* address); \
356 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
357protected: \
358 strtype fBuffer; \
359}
360
362CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
363CPPYY_DECLARE_STRING_CONVERTER(STLStringViewBase, std::string_view);
364class STLStringViewConverter : public STLStringViewBaseConverter {
365public:
366 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
367};
368CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
369
370class STLStringMoveConverter : public STLStringConverter {
371public:
372 using STLStringConverter::STLStringConverter;
373
374public:
375 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
376};
377
378
379// function pointers
380class FunctionPointerConverter : public Converter {
381public:
382 FunctionPointerConverter(const std::string& ret, const std::string& sig) :
383 fRetType(ret), fSignature(sig) {}
384
385public:
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
391protected:
392 std::string fRetType;
393 std::string fSignature;
394};
395
396// std::function
397class StdFunctionConverter : public FunctionPointerConverter {
398public:
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
405public:
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
410protected:
411 Converter* fConverter;
413};
414
415
416// smart pointer converter
417class SmartPtrConverter : public Converter {
418public:
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
426public:
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
432protected:
433 virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
434
438 bool fIsRef;
439};
440
441
442// initializer lists
443class InitializerListConverter : public Converter {
444public:
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
451public:
452 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
453 virtual bool HasState() { return true; }
454
455protected:
456 Converter* fConverter;
458};
459
460
461// raising converter to take out overloads
462class NotImplementedConverter : public Converter {
463public:
464 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
465};
466
467} // unnamed namespace
468
469} // namespace CPyCppyy
470
471#endif // !CPYCPPYY_DECLARECONVERTERS_H
Py_ssize_t dim_t
Definition CPyCppyy.h:64
std::string fRetType
size_t fSize
#define CPPYY_DECLARE_ARRAY_CONVERTER(name)
long fMaxSize
size_t fValueSize
#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype)
bool fIsConst
bool fKeepControl
std::string fBuffer
#define CPPYY_DECLARE_BASIC_CONVERTER(name)
bool fIsRef
#define CPPYY_DECLARE_REFCONVERTER(name)
PyObject * fFuncWrap
Cppyy::TCppType_t fClass
dims_t m_dims
Cppyy::TCppType_t fSmartPtrType
std::string fSignature
Cppyy::TCppType_t fUnderlyingType
Converter * fConverter
#define CPPYY_DECLARE_BASIC_CONVERTER2(name, base)
_object PyObject
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Binding & operator=(OUT(*fun)(void))
#define free
Definition civetweb.c:1539
InstancePtrConverter(Cppyy::TCppType_t klass, bool keepControl=false)
Definition Converters.h:53
Basic string class.
Definition TString.h:139
Set of helper functions that are invoked from the pythonizors, on the Python side.
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19