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 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
343 virtual PyObject* FromMemory(void* address);
344 virtual bool HasState() { return true; }
345
346protected:
348 bool fIsFixed;
349};
350
352
353
354#define CPPYY_DECLARE_STRING_CONVERTER(name, strtype) \
355class name##Converter : public InstanceConverter { \
356public: \
357 name##Converter(bool keepControl = true); \
358 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr); \
359 virtual PyObject* FromMemory(void* address); \
360 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr); \
361 virtual bool HasState() { return true; } \
362protected: \
363 strtype fStringBuffer; \
364}
365
367CPPYY_DECLARE_STRING_CONVERTER(STLString, std::string);
368#if __cplusplus > 201402L
369// The buffer type needs to be std::string also in the string_view case,
370// otherwise the pointed-to string might not live long enough. See also:
371// https://github.com/wlav/CPyCppyy/issues/13
372CPPYY_DECLARE_STRING_CONVERTER(STLStringViewBase, std::string);
373class STLStringViewConverter : public STLStringViewBaseConverter {
374public:
375 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr);
376 virtual bool ToMemory(PyObject*, void*, PyObject* = nullptr);
377private:
378 std::string_view fStringView;
379};
380#endif
381CPPYY_DECLARE_STRING_CONVERTER(STLWString, std::wstring);
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:64
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