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
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*); \
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*); \
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); \
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; } \
66protected: \
67 Py_ssize_t* fShape; \
68}; \
69 \
70class name##ArrayPtrConverter : public name##ArrayConverter { \
71public: \
72 using name##ArrayConverter::name##ArrayConverter; \
73 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
74};
75
76
77// converters for built-ins
82class UCharAsIntConverter : public UCharConverter {
83public:
84 using UCharConverter::UCharConverter;
85 virtual PyObject* FromMemory(void*);
86};
102
123
124class VoidConverter : public Converter {
125public:
126 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
127};
128
129class CStringConverter : public Converter {
130public:
131 CStringConverter(long maxSize = -1) : fMaxSize(maxSize) {}
132
133public:
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
139protected:
140 std::string fBuffer;
142};
143
144class NonConstCStringConverter : public CStringConverter {
145public:
146 NonConstCStringConverter(long maxSize = -1) : CStringConverter(maxSize) {}
147
148public:
149 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
150 virtual PyObject* FromMemory(void* address);
151};
152
153class WCStringConverter : public Converter {
154public:
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
160public:
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
166protected:
167 wchar_t* fBuffer;
168 long fMaxSize;
169};
170
171class CString16Converter : public Converter {
172public:
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
178public:
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
184protected:
185 char16_t* fBuffer;
186 long fMaxSize;
187};
188
189class CString32Converter : public Converter {
190public:
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
196public:
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
202protected:
203 char32_t* fBuffer;
204 long fMaxSize;
205};
206
207// pointer/array conversions
211#if __cplusplus > 201402L
213#endif
226
227class CStringArrayConverter : public SCharArrayPtrConverter {
228public:
229 using SCharArrayPtrConverter::SCharArrayPtrConverter;
230 virtual PyObject* FromMemory(void* address);
231};
232
233
234// converters for special cases
235class NullptrConverter : public Converter {
236public:
237 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
238};
239
240class InstanceConverter : public StrictInstancePtrConverter {
241public:
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
248class InstanceRefConverter : public Converter {
249public:
250 InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
251 fClass(klass), fIsConst(isConst) {}
252
253public:
254 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
255 virtual PyObject* FromMemory(void* address);
256 virtual bool HasState() { return true; }
257
258protected:
261};
262
263class InstanceMoveConverter : public InstanceRefConverter {
264public:
265 InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
266 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
267};
268
269template <bool ISREFERENCE>
270class InstancePtrPtrConverter : public InstancePtrConverter {
271public:
273
274public:
275 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
276 virtual PyObject* FromMemory(void* address);
277 virtual bool ToMemory(PyObject* value, void* address);
278};
279
280class InstanceArrayConverter : public InstancePtrConverter {
281public:
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
296public:
297 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
298 virtual PyObject* FromMemory(void* address);
299 virtual bool ToMemory(PyObject* value, void* address);
300
301protected:
303};
304
305
306class ComplexDConverter: public InstanceConverter {
307public:
308 ComplexDConverter(bool keepControl = false);
309
310public:
311 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
312 virtual PyObject* FromMemory(void* address);
313 virtual bool ToMemory(PyObject* value, void* address);
314
315private:
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
322class STLIteratorConverter : public Converter {
323public:
324 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
325};
326// -- END CLING WORKAROUND
327
328
329class VoidPtrRefConverter : public Converter {
330public:
331 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
332};
333
334class VoidPtrPtrConverter : public Converter {
335public:
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
341protected:
342 size_t fSize;
343};
344
346
347
348#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
349class name##Converter : public InstanceConverter { \
350public: \
351 name##Converter(bool keepControl = true); \
352public: \
353 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
354 virtual PyObject* FromMemory(void* address); \
355 virtual bool ToMemory(PyObject* value, void* address); \
356protected: \
357 strtype fBuffer; \
358}
359
361CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
363class STLStringViewConverter : public STLStringViewBaseConverter {
364public:
365 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
366};
367CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
368
369class STLStringMoveConverter : public STLStringConverter {
370public:
371 using STLStringConverter::STLStringConverter;
372
373public:
374 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
375};
376
377
378// function pointers
379class FunctionPointerConverter : public Converter {
380public:
381 FunctionPointerConverter(const std::string& ret, const std::string& sig) :
382 fRetType(ret), fSignature(sig) {}
383
384public:
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
390protected:
391 std::string fRetType;
392 std::string fSignature;
393};
394
395// std::function
396class StdFunctionConverter : public FunctionPointerConverter {
397public:
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
404public:
405 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
406 virtual PyObject* FromMemory(void* address);
407 virtual bool ToMemory(PyObject* value, void* address);
408
409protected:
410 Converter* fConverter;
412};
413
414
415// smart pointer converter
416class SmartPtrConverter : public Converter {
417public:
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
425public:
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
431protected:
432 virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
433
437 bool fIsRef;
438};
439
440
441// initializer lists
442class InitializerListConverter : public Converter {
443public:
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
450public:
451 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
452 virtual bool HasState() { return true; }
453
454protected:
455 Converter* fConverter;
457};
458
459
460// raising converter to take out overloads
461class NotImplementedConverter : public Converter {
462public:
463 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
464};
465
466} // unnamed namespace
467
468} // namespace CPyCppyy
469
470#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
std::string fBuffer
bool fKeepControl
#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
Definition: PyMethodBase.h:41
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:131
basic_string_view< char > string_view
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
const char * Long
const char * ULong
const char * Float
const char * Double
const char * UChar
const char * Short
const char * Int
const char * UInt
const char * UShort
const char * Bool
const char * Char