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};
105
130
131class VoidConverter : public Converter {
132public:
133 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
134};
135
136class CStringConverter : public Converter {
137public:
138 CStringConverter(std::string::size_type maxSize = std::string::npos) : fMaxSize(maxSize) {}
139
140public:
141 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
142 virtual PyObject* FromMemory(void* address);
143 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
144 virtual bool HasState() { return true; }
145
146protected:
147 std::string fBuffer;
148 std::string::size_type fMaxSize;
149};
150
151class NonConstCStringConverter : public CStringConverter {
152public:
153 using CStringConverter::CStringConverter;
154
155public:
156 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
157 virtual PyObject* FromMemory(void* address);
158};
159
160class WCStringConverter : public Converter {
161public:
162 WCStringConverter(std::wstring::size_type maxSize = std::wstring::npos) :
163 fBuffer(nullptr), fMaxSize(maxSize) {}
164 WCStringConverter(const WCStringConverter&) = delete;
165 WCStringConverter& operator=(const WCStringConverter&) = delete;
166 virtual ~WCStringConverter() { free(fBuffer); }
167
168public:
169 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
170 virtual PyObject* FromMemory(void* address);
171 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
172 virtual bool HasState() { return true; }
173
174protected:
175 wchar_t* fBuffer;
176 std::wstring::size_type fMaxSize;
177};
178
179class CString16Converter : public Converter {
180public:
181 CString16Converter(std::wstring::size_type maxSize = std::wstring::npos) :
182 fBuffer(nullptr), fMaxSize(maxSize) {}
183 CString16Converter(const CString16Converter&) = delete;
184 CString16Converter& operator=(const CString16Converter&) = delete;
185 virtual ~CString16Converter() { free(fBuffer); }
186
187public:
188 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
189 virtual PyObject* FromMemory(void* address);
190 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
191 virtual bool HasState() { return true; }
192
193protected:
194 char16_t* fBuffer;
195 std::wstring::size_type fMaxSize;
196};
197
198class CString32Converter : public Converter {
199public:
200 CString32Converter(std::wstring::size_type maxSize = std::wstring::npos) :
201 fBuffer(nullptr), fMaxSize(maxSize) {}
202 CString32Converter(const CString32Converter&) = delete;
203 CString32Converter& operator=(const CString32Converter&) = delete;
204 virtual ~CString32Converter() { free(fBuffer); }
205
206public:
207 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
208 virtual PyObject* FromMemory(void* address);
209 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
210 virtual bool HasState() { return true; }
211
212protected:
213 char32_t* fBuffer;
214 std::wstring::size_type fMaxSize;
215};
216
217// pointer/array conversions
221#if (__cplusplus > 201402L) || (defined(_MSC_VER) && _MSVC_LANG > 201402L)
223#endif
243
244class CStringArrayConverter : public SCharArrayConverter {
245public:
246 CStringArrayConverter(cdims_t dims, bool fixed) : SCharArrayConverter(dims) {
247 fIsFixed = fixed; // overrides SCharArrayConverter decision
248 }
249 using SCharArrayConverter::SCharArrayConverter;
250 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
251 virtual PyObject* FromMemory(void* address);
252 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
253
254private:
255 std::vector<const char*> fBuffer;
256};
257
258class NonConstCStringArrayConverter : public CStringArrayConverter {
259public:
260 using CStringArrayConverter::CStringArrayConverter;
261 virtual PyObject* FromMemory(void* address);
262};
263
264// converters for special cases
265class NullptrConverter : public Converter {
266public:
267 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
268};
269
270class InstanceConverter : public StrictInstancePtrConverter {
271public:
272 using StrictInstancePtrConverter::StrictInstancePtrConverter;
273 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
274 virtual PyObject* FromMemory(void*);
275 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
276};
277
278class InstanceRefConverter : public Converter {
279public:
280 InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
282
283public:
284 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
285 virtual PyObject* FromMemory(void* address);
286 virtual bool HasState() { return true; }
287
288protected:
291};
292
293class InstanceMoveConverter : public InstanceRefConverter {
294public:
295 InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
296 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
297};
298
299template <bool ISREFERENCE>
300class InstancePtrPtrConverter : public InstancePtrConverter<false> {
301public:
303
304public:
305 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
306 virtual PyObject* FromMemory(void* address);
307 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
308};
309
310class InstanceArrayConverter : public InstancePtrConverter<false> {
311public:
312 InstanceArrayConverter(Cppyy::TCppType_t klass, cdims_t dims, bool keepControl = false) :
313 InstancePtrConverter<false>(klass, keepControl), fShape(dims) { }
314 InstanceArrayConverter(const InstanceArrayConverter&) = delete;
315 InstanceArrayConverter& operator=(const InstanceArrayConverter&) = delete;
316
317public:
318 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
319 virtual PyObject* FromMemory(void* address);
320 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
321
322protected:
324};
325
326
327class ComplexDConverter: public InstanceConverter {
328public:
329 ComplexDConverter(bool keepControl = false);
330
331public:
332 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
333 virtual PyObject* FromMemory(void* address);
334 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
335 virtual bool HasState() { return true; }
336
337private:
338 std::complex<double> fBuffer;
339};
340
341
342// CLING WORKAROUND -- classes for STL iterators are completely undefined in that
343// they come in a bazillion different guises, so just do whatever
344class STLIteratorConverter : public Converter {
345public:
346 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
347};
348// -- END CLING WORKAROUND
349
350
351class VoidPtrRefConverter : public Converter {
352public:
353 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
354};
355
356class VoidPtrPtrConverter : public Converter {
357public:
358 VoidPtrPtrConverter(cdims_t dims);
359
360public:
361 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
362 virtual PyObject* FromMemory(void* address);
363 virtual bool HasState() { return true; }
364
365protected:
367 bool fIsFixed;
368};
369
371
372
373#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
374class name##Converter : public InstanceConverter { \
375public: \
376 name##Converter(bool keepControl = true); \
377 \
378public: \
379 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
380 virtual PyObject* FromMemory(void* address); \
381 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
382 virtual bool HasState() { return true; } \
383 \
384protected: \
385 strtype fBuffer; \
386}
387
391#if (__cplusplus > 201402L) || (defined(_MSC_VER) && _MSVC_LANG > 201402L)
393#endif
394
395class STLStringMoveConverter : public STLStringConverter {
396public:
397 using STLStringConverter::STLStringConverter;
398
399public:
400 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
401};
402
403
404// function pointers
405class FunctionPointerConverter : public Converter {
406public:
407 FunctionPointerConverter(const std::string& ret, const std::string& sig) :
408 fRetType(ret), fSignature(sig) {}
409
410public:
411 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
412 virtual PyObject* FromMemory(void* address);
413 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
414 virtual bool HasState() { return true; }
415
416protected:
417 std::string fRetType;
418 std::string fSignature;
419};
420
421// std::function
422class StdFunctionConverter : public FunctionPointerConverter {
423public:
424 StdFunctionConverter(Converter* cnv, const std::string& ret, const std::string& sig) :
425 FunctionPointerConverter(ret, sig), fConverter(cnv) {}
426 StdFunctionConverter(const StdFunctionConverter&) = delete;
427 StdFunctionConverter& operator=(const StdFunctionConverter&) = delete;
428 virtual ~StdFunctionConverter() { delete fConverter; }
429
430public:
431 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
432 virtual PyObject* FromMemory(void* address);
433 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
434
435protected:
436 Converter* fConverter;
437};
438
439
440// smart pointer converter
441class SmartPtrConverter : public Converter {
442public:
443 SmartPtrConverter(Cppyy::TCppType_t smart,
445 bool keepControl = false,
446 bool isRef = false)
449
450public:
451 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
452 virtual PyObject* FromMemory(void* address);
453 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
454 virtual bool HasState() { return true; }
455
456protected:
457 virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
458
462 bool fIsRef;
463};
464
465
466// initializer lists
467class InitializerListConverter : public InstanceConverter {
468public:
469 InitializerListConverter(Cppyy::TCppType_t klass, std::string const& value_type);
470 InitializerListConverter(const InitializerListConverter&) = delete;
471 InitializerListConverter& operator=(const InitializerListConverter&) = delete;
473
474public:
475 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
476 virtual bool HasState() { return true; }
477
478protected:
479 void Clear();
480
481protected:
482 void* fBuffer = nullptr;
483 std::vector<Converter*> fConverters;
484 std::string fValueTypeName;
487};
488
489
490// raising converter to take out overloads
491class NotImplementedConverter : public Converter {
492public:
493 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
494};
495
496} // unnamed namespace
497
498} // namespace CPyCppyy
499
500#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
void operator=(const TProof &)
#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