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#include "Dimensions.h"
7
8// Standard
9#include <complex>
10#include <string>
11
12// ROOT
13#include "ROOT/RStringView.hxx"
14#include "TString.h"
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(cdims_t dims); \
59 name##ArrayConverter(const name##ArrayConverter&) = delete; \
60 name##ArrayConverter& operator=(const name##ArrayConverter&) = delete; \
61 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
62 virtual PyObject* FromMemory(void*); \
63 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
64 virtual bool HasState() { return true; } \
65protected: \
66 dims_t fShape; \
67 bool fIsFixed; \
68};
69
70
71// converters for built-ins
75class SCharAsIntConverter : public CharConverter {
76public:
77 using CharConverter::CharConverter;
78 virtual PyObject* FromMemory(void*);
79};
81class UCharAsIntConverter : public UCharConverter {
82public:
83 using UCharConverter::UCharConverter;
84 virtual PyObject* FromMemory(void*);
85};
101
122
123class VoidConverter : public Converter {
124public:
125 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
126};
127
128class CStringConverter : public Converter {
129public:
130 CStringConverter(std::string::size_type maxSize = std::string::npos) : fMaxSize(maxSize) {}
131
132public:
133 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
134 virtual PyObject* FromMemory(void* address);
135 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
136 virtual bool HasState() { return true; }
137
138protected:
139 std::string fBuffer;
140 std::string::size_type fMaxSize;
141};
142
143class NonConstCStringConverter : public CStringConverter {
144public:
145 using CStringConverter::CStringConverter;
146
147public:
148 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
149 virtual PyObject* FromMemory(void* address);
150};
151
152class WCStringConverter : public Converter {
153public:
154 WCStringConverter(std::wstring::size_type maxSize = std::wstring::npos) :
155 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, PyObject* = nullptr);
164 virtual bool HasState() { return true; }
165
166protected:
167 wchar_t* fBuffer;
168 std::wstring::size_type fMaxSize;
169};
170
171class CString16Converter : public Converter {
172public:
173 CString16Converter(std::wstring::size_type maxSize = std::wstring::npos) :
174 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 std::wstring::size_type fMaxSize;
188};
189
190class CString32Converter : public Converter {
191public:
192 CString32Converter(std::wstring::size_type maxSize = std::wstring::npos) :
193 fBuffer(nullptr), fMaxSize(maxSize) {}
194 CString32Converter(const CString32Converter&) = delete;
195 CString32Converter& operator=(const CString32Converter&) = delete;
196 virtual ~CString32Converter() { free(fBuffer); }
197
198public:
199 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
200 virtual PyObject* FromMemory(void* address);
201 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
202 virtual bool HasState() { return true; }
203
204protected:
205 char32_t* fBuffer;
206 std::wstring::size_type fMaxSize;
207};
208
209// pointer/array conversions
213#if __cplusplus > 201402L
215#endif
231
232class CStringArrayConverter : public SCharArrayConverter {
233public:
234 CStringArrayConverter(cdims_t dims, bool fixed) : SCharArrayConverter(dims) {
235 fIsFixed = fixed; // overrides SCharArrayConverter decision
236 }
237 using SCharArrayConverter::SCharArrayConverter;
238 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
239 virtual PyObject* FromMemory(void* address);
240 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
241
242private:
243 std::vector<const char*> fBuffer;
244};
245
246class NonConstCStringArrayConverter : public CStringArrayConverter {
247public:
248 using CStringArrayConverter::CStringArrayConverter;
249 virtual PyObject* FromMemory(void* address);
250};
251
252// converters for special cases
253class NullptrConverter : public Converter {
254public:
255 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
256};
257
258class InstanceConverter : public StrictInstancePtrConverter {
259public:
260 using StrictInstancePtrConverter::StrictInstancePtrConverter;
261 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
262 virtual PyObject* FromMemory(void*);
263 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
264};
265
266class InstanceRefConverter : public Converter {
267public:
268 InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
269 fClass(klass), fIsConst(isConst) {}
270
271public:
272 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
273 virtual PyObject* FromMemory(void* address);
274 virtual bool HasState() { return true; }
275
276protected:
279};
280
281class InstanceMoveConverter : public InstanceRefConverter {
282public:
283 InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
284 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
285};
286
287template <bool ISREFERENCE>
288class InstancePtrPtrConverter : public InstancePtrConverter<false> {
289public:
291
292public:
293 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
294 virtual PyObject* FromMemory(void* address);
295 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
296};
297
298class InstanceArrayConverter : public InstancePtrConverter<false> {
299public:
300 InstanceArrayConverter(Cppyy::TCppType_t klass, cdims_t dims, bool keepControl = false) :
301 InstancePtrConverter<false>(klass, keepControl), fShape(dims) { }
302 InstanceArrayConverter(const InstanceArrayConverter&) = delete;
303 InstanceArrayConverter& operator=(const InstanceArrayConverter&) = delete;
304
305public:
306 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
307 virtual PyObject* FromMemory(void* address);
308 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
309
310protected:
312};
313
314
315class ComplexDConverter: public InstanceConverter {
316public:
317 ComplexDConverter(bool keepControl = false);
318
319public:
320 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
321 virtual PyObject* FromMemory(void* address);
322 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
323 virtual bool HasState() { return true; }
324
325private:
326 std::complex<double> fBuffer;
327};
328
329
330// CLING WORKAROUND -- classes for STL iterators are completely undefined in that
331// they come in a bazillion different guises, so just do whatever
332class STLIteratorConverter : public Converter {
333public:
334 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
335};
336// -- END CLING WORKAROUND
337
338
339class VoidPtrRefConverter : public Converter {
340public:
341 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
342};
343
344class VoidPtrPtrConverter : public Converter {
345public:
346 VoidPtrPtrConverter(cdims_t dims);
347
348public:
349 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
350 virtual PyObject* FromMemory(void* address);
351 virtual bool HasState() { return true; }
352
353protected:
355 bool fIsFixed;
356};
357
359
360
361#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
362class name##Converter : public InstanceConverter { \
363public: \
364 name##Converter(bool keepControl = true); \
365 \
366public: \
367 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
368 virtual PyObject* FromMemory(void* address); \
369 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
370 virtual bool HasState() { return true; } \
371 \
372protected: \
373 strtype fBuffer; \
374}
375
377CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
378CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
379#if __cplusplus > 201402L
380CPPYY_DECLARE_STRING_CONVERTER(STLStringView, std::string_view);
381#endif
382
383class STLStringMoveConverter : public STLStringConverter {
384public:
385 using STLStringConverter::STLStringConverter;
386
387public:
388 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
389};
390
391
392// function pointers
393class FunctionPointerConverter : public Converter {
394public:
395 FunctionPointerConverter(const std::string& ret, const std::string& sig) :
396 fRetType(ret), fSignature(sig) {}
397
398public:
399 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
400 virtual PyObject* FromMemory(void* address);
401 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
402 virtual bool HasState() { return true; }
403
404protected:
405 std::string fRetType;
406 std::string fSignature;
407};
408
409// std::function
410class StdFunctionConverter : public FunctionPointerConverter {
411public:
412 StdFunctionConverter(Converter* cnv, const std::string& ret, const std::string& sig) :
413 FunctionPointerConverter(ret, sig), fConverter(cnv) {}
414 StdFunctionConverter(const StdFunctionConverter&) = delete;
415 StdFunctionConverter& operator=(const StdFunctionConverter&) = delete;
416 virtual ~StdFunctionConverter() { delete fConverter; }
417
418public:
419 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
420 virtual PyObject* FromMemory(void* address);
421 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
422
423protected:
424 Converter* fConverter;
425};
426
427
428// smart pointer converter
429class SmartPtrConverter : public Converter {
430public:
431 SmartPtrConverter(Cppyy::TCppType_t smart,
432 Cppyy::TCppType_t underlying,
433 bool keepControl = false,
434 bool isRef = false)
435 : fSmartPtrType(smart), fUnderlyingType(underlying),
436 fKeepControl(keepControl), fIsRef(isRef) {}
437
438public:
439 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
440 virtual PyObject* FromMemory(void* address);
441 //virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
442 virtual bool HasState() { return true; }
443
444protected:
445 virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
446
450 bool fIsRef;
451};
452
453
454// initializer lists
455class InitializerListConverter : public InstanceConverter {
456public:
457 InitializerListConverter(Cppyy::TCppType_t klass, std::string const& value_type);
458 InitializerListConverter(const InitializerListConverter&) = delete;
459 InitializerListConverter& operator=(const InitializerListConverter&) = delete;
460 virtual ~InitializerListConverter();
461
462public:
463 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
464 virtual bool HasState() { return true; }
465
466protected:
467 void Clear();
468
469protected:
470 void* fBuffer = nullptr;
471 std::vector<Converter*> fConverters;
472 std::string fValueTypeName;
475};
476
477
478// raising converter to take out overloads
479class NotImplementedConverter : public Converter {
480public:
481 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
482};
483
484} // unnamed namespace
485
486} // namespace CPyCppyy
487
488#endif // !CPYCPPYY_DECLARECONVERTERS_H
fBuffer
std::string fRetType
#define CPPYY_DECLARE_ARRAY_CONVERTER(name)
dims_t fShape
size_t fValueSize
#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype)
bool fIsConst
std::string fValueTypeName
bool fIsFixed
Cppyy::TCppType_t fValueType
bool fKeepControl
#define CPPYY_DECLARE_BASIC_CONVERTER(name)
bool fIsRef
#define CPPYY_DECLARE_REFCONVERTER(name)
Cppyy::TCppType_t fClass
Cppyy::TCppType_t fSmartPtrType
std::string fSignature
Cppyy::TCppType_t fUnderlyingType
Converter * fConverter
#define CPPYY_DECLARE_BASIC_CONVERTER2(name, base)
std::string::size_type fMaxSize
std::vector< Converter * > fConverters
_object PyObject
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:65
Basic string class.
Definition TString.h:139
Dimensions dims_t
Definition API.h:103
const dims_t & cdims_t
Definition API.h:104
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19