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
76class UCharAsIntConverter : public UCharConverter {
77public:
78 using UCharConverter::UCharConverter;
79 virtual PyObject* FromMemory(void*);
80};
96
117
118class VoidConverter : public Converter {
119public:
120 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
121};
122
123class CStringConverter : public Converter {
124public:
125 CStringConverter(std::string::size_type maxSize = std::string::npos) : fMaxSize(maxSize) {}
126
127public:
128 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
129 virtual PyObject* FromMemory(void* address);
130 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
131 virtual bool HasState() { return true; }
132
133protected:
134 std::string fBuffer;
135 std::string::size_type fMaxSize;
136};
137
138class NonConstCStringConverter : public CStringConverter {
139public:
140 using CStringConverter::CStringConverter;
141
142public:
143 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
144 virtual PyObject* FromMemory(void* address);
145};
146
147class WCStringConverter : public Converter {
148public:
149 WCStringConverter(std::wstring::size_type maxSize = std::wstring::npos) :
150 fBuffer(nullptr), fMaxSize(maxSize) {}
151 WCStringConverter(const WCStringConverter&) = delete;
152 WCStringConverter& operator=(const WCStringConverter&) = delete;
153 virtual ~WCStringConverter() { free(fBuffer); }
154
155public:
156 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
157 virtual PyObject* FromMemory(void* address);
158 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
159 virtual bool HasState() { return true; }
160
161protected:
162 wchar_t* fBuffer;
163 std::wstring::size_type fMaxSize;
164};
165
166class CString16Converter : public Converter {
167public:
168 CString16Converter(std::wstring::size_type maxSize = std::wstring::npos) :
169 fBuffer(nullptr), fMaxSize(maxSize) {}
170 CString16Converter(const CString16Converter&) = delete;
171 CString16Converter& operator=(const CString16Converter&) = delete;
172 virtual ~CString16Converter() { free(fBuffer); }
173
174public:
175 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
176 virtual PyObject* FromMemory(void* address);
177 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
178 virtual bool HasState() { return true; }
179
180protected:
181 char16_t* fBuffer;
182 std::wstring::size_type fMaxSize;
183};
184
185class CString32Converter : public Converter {
186public:
187 CString32Converter(std::wstring::size_type maxSize = std::wstring::npos) :
188 fBuffer(nullptr), fMaxSize(maxSize) {}
189 CString32Converter(const CString32Converter&) = delete;
190 CString32Converter& operator=(const CString32Converter&) = delete;
191 virtual ~CString32Converter() { free(fBuffer); }
192
193public:
194 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
195 virtual PyObject* FromMemory(void* address);
196 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
197 virtual bool HasState() { return true; }
198
199protected:
200 char32_t* fBuffer;
201 std::wstring::size_type fMaxSize;
202};
203
204// pointer/array conversions
208#if __cplusplus > 201402L
210#endif
226
227class CStringArrayConverter : public SCharArrayConverter {
228public:
229 CStringArrayConverter(cdims_t dims, bool fixed) : SCharArrayConverter(dims) {
230 fIsFixed = fixed; // overrides SCharArrayConverter decision
231 }
232 using SCharArrayConverter::SCharArrayConverter;
233 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
234 virtual PyObject* FromMemory(void* address);
235 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
236
237private:
238 std::vector<const char*> fBuffer;
239};
240
241class NonConstCStringArrayConverter : public CStringArrayConverter {
242public:
243 using CStringArrayConverter::CStringArrayConverter;
244 virtual PyObject* FromMemory(void* address);
245};
246
247// converters for special cases
248class NullptrConverter : public Converter {
249public:
250 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
251};
252
253class InstanceConverter : public StrictInstancePtrConverter {
254public:
255 using StrictInstancePtrConverter::StrictInstancePtrConverter;
256 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
257 virtual PyObject* FromMemory(void*);
258 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
259};
260
261class InstanceRefConverter : public Converter {
262public:
263 InstanceRefConverter(Cppyy::TCppType_t klass, bool isConst) :
264 fClass(klass), fIsConst(isConst) {}
265
266public:
267 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
268 virtual PyObject* FromMemory(void* address);
269 virtual bool HasState() { return true; }
270
271protected:
274};
275
276class InstanceMoveConverter : public InstanceRefConverter {
277public:
278 InstanceMoveConverter(Cppyy::TCppType_t klass) : InstanceRefConverter(klass, true) {}
279 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
280};
281
282template <bool ISREFERENCE>
283class InstancePtrPtrConverter : public InstancePtrConverter<false> {
284public:
286
287public:
288 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
289 virtual PyObject* FromMemory(void* address);
290 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
291};
292
293class InstanceArrayConverter : public InstancePtrConverter<false> {
294public:
295 InstanceArrayConverter(Cppyy::TCppType_t klass, cdims_t dims, bool keepControl = false) :
296 InstancePtrConverter<false>(klass, keepControl), fShape(dims) { }
297 InstanceArrayConverter(const InstanceArrayConverter&) = delete;
298 InstanceArrayConverter& operator=(const InstanceArrayConverter&) = delete;
299
300public:
301 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
302 virtual PyObject* FromMemory(void* address);
303 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
304
305protected:
307};
308
309
310class ComplexDConverter: public InstanceConverter {
311public:
312 ComplexDConverter(bool keepControl = false);
313
314public:
315 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
316 virtual PyObject* FromMemory(void* address);
317 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
318 virtual bool HasState() { return true; }
319
320private:
321 std::complex<double> fBuffer;
322};
323
324
325// CLING WORKAROUND -- classes for STL iterators are completely undefined in that
326// they come in a bazillion different guises, so just do whatever
327class STLIteratorConverter : public Converter {
328public:
329 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
330};
331// -- END CLING WORKAROUND
332
333
334class VoidPtrRefConverter : public Converter {
335public:
336 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
337};
338
339class VoidPtrPtrConverter : public Converter {
340public:
341 VoidPtrPtrConverter(cdims_t dims);
342
343public:
344 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
345 virtual PyObject* FromMemory(void* address);
346 virtual bool HasState() { return true; }
347
348protected:
350 bool fIsFixed;
351};
352
354
355
356#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
357class name##Converter : public InstanceConverter { \
358public: \
359 name##Converter(bool keepControl = true); \
360 \
361public: \
362 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
363 virtual PyObject* FromMemory(void* address); \
364 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
365 virtual bool HasState() { return true; } \
366 \
367protected: \
368 strtype fBuffer; \
369}
370
372CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
373CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
374#if __cplusplus > 201402L
375CPPYY_DECLARE_STRING_CONVERTER(STLStringView, std::string_view);
376#endif
377
378class STLStringMoveConverter : public STLStringConverter {
379public:
380 using STLStringConverter::STLStringConverter;
381
382public:
383 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
384};
385
386
387// function pointers
388class FunctionPointerConverter : public Converter {
389public:
390 FunctionPointerConverter(const std::string& ret, const std::string& sig) :
391 fRetType(ret), fSignature(sig) {}
392
393public:
394 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
395 virtual PyObject* FromMemory(void* address);
396 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
397 virtual bool HasState() { return true; }
398
399protected:
400 std::string fRetType;
401 std::string fSignature;
402};
403
404// std::function
405class StdFunctionConverter : public FunctionPointerConverter {
406public:
407 StdFunctionConverter(Converter* cnv, const std::string& ret, const std::string& sig) :
408 FunctionPointerConverter(ret, sig), fConverter(cnv) {}
409 StdFunctionConverter(const StdFunctionConverter&) = delete;
410 StdFunctionConverter& operator=(const StdFunctionConverter&) = delete;
411 virtual ~StdFunctionConverter() { delete fConverter; }
412
413public:
414 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
415 virtual PyObject* FromMemory(void* address);
416 virtual bool ToMemory(PyObject* value, void* address, PyObject* = nullptr);
417
418protected:
419 Converter* fConverter;
420};
421
422
423// smart pointer converter
424class SmartPtrConverter : public Converter {
425public:
426 SmartPtrConverter(Cppyy::TCppType_t smart,
427 Cppyy::TCppType_t underlying,
428 bool keepControl = false,
429 bool isRef = false)
430 : fSmartPtrType(smart), fUnderlyingType(underlying),
431 fKeepControl(keepControl), fIsRef(isRef) {}
432
433public:
434 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
435 virtual PyObject* FromMemory(void* address);
436 //virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
437 virtual bool HasState() { return true; }
438
439protected:
440 virtual bool GetAddressSpecialCase(PyObject*, void*&) { return false; }
441
445 bool fIsRef;
446};
447
448
449// initializer lists
450class InitializerListConverter : public InstanceConverter {
451public:
452 InitializerListConverter(Cppyy::TCppType_t klass, std::string const& value_type);
453 InitializerListConverter(const InitializerListConverter&) = delete;
454 InitializerListConverter& operator=(const InitializerListConverter&) = delete;
455 virtual ~InitializerListConverter();
456
457public:
458 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
459 virtual bool HasState() { return true; }
460
461protected:
462 void Clear();
463
464protected:
465 void* fBuffer = nullptr;
466 std::vector<Converter*> fConverters;
467 std::string fValueTypeName;
470};
471
472
473// raising converter to take out overloads
474class NotImplementedConverter : public Converter {
475public:
476 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
477};
478
479} // unnamed namespace
480
481} // namespace CPyCppyy
482
483#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