Logo ROOT   6.10/09
Reference Guide
Converters.h
Go to the documentation of this file.
1 // @(#)root/pyroot:$Id$
2 // Author: Wim Lavrijsen, Jan 2005
3 #ifndef PYROOT_CONVERTERS_H
4 #define PYROOT_CONVERTERS_H
5 
6 // ROOT
7 #include "TString.h"
8 
9 // Standard
10 #include <limits.h>
11 #include <string>
12 #include <map>
13 
14 
15 namespace PyROOT {
16 
17  struct TParameter;
18  struct TCallContext;
19 
20  class TConverter {
21  public:
22  virtual ~TConverter() {}
23 
24  public:
25  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 ) = 0;
26  virtual PyObject* FromMemory( void* address );
27  virtual Bool_t ToMemory( PyObject* value, void* address );
28  };
29 
30 #define PYROOT_DECLARE_BASIC_CONVERTER( name ) \
31  class T##name##Converter : public TConverter { \
32  public: \
33  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
34  virtual PyObject* FromMemory( void* ); \
35  virtual Bool_t ToMemory( PyObject*, void* ); \
36  }; \
37  \
38  class TConst##name##RefConverter : public TConverter { \
39  public: \
40  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
41  }
42 
43 
44 #define PYROOT_DECLARE_BASIC_CONVERTER2( name, base ) \
45  class T##name##Converter : public T##base##Converter { \
46  public: \
47  virtual PyObject* FromMemory( void* ); \
48  virtual Bool_t ToMemory( PyObject*, void* ); \
49  }; \
50  \
51  class TConst##name##RefConverter : public TConverter { \
52  public: \
53  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
54  }
55 
56 #define PYROOT_DECLARE_REF_CONVERTER( name ) \
57  class T##name##RefConverter : public TConverter { \
58  public: \
59  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
60  };
61 
62 #define PYROOT_DECLARE_ARRAY_CONVERTER( name ) \
63  class T##name##Converter : public TConverter { \
64  public: \
65  T##name##Converter( Py_ssize_t size = -1 ) { fSize = size; } \
66  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
67  virtual PyObject* FromMemory( void* ); \
68  virtual Bool_t ToMemory( PyObject*, void* ); \
69  private: \
70  Py_ssize_t fSize; \
71  }; \
72  \
73  class T##name##RefConverter : public T##name##Converter { \
74  public: \
75  using T##name##Converter::T##name##Converter; \
76  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
77  }
78 
79 // converters for built-ins
90  PYROOT_DECLARE_BASIC_CONVERTER( ULongLong );
93  PYROOT_DECLARE_BASIC_CONVERTER( LongDouble );
94 
98 
99  class TVoidConverter : public TConverter {
100  public:
101  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
102  };
103 
104  class TCStringConverter : public TConverter {
105  public:
106  TCStringConverter( UInt_t maxSize = UINT_MAX ) : fMaxSize( maxSize ) {}
107 
108  public:
109  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
110  virtual PyObject* FromMemory( void* address );
111  virtual Bool_t ToMemory( PyObject* value, void* address );
112 
113  protected:
114  std::string fBuffer;
116  };
117 
119  public:
120  TNonConstCStringConverter( UInt_t maxSize = UINT_MAX ) : TCStringConverter( maxSize ) {}
121 
122  public:
123  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
124  virtual PyObject* FromMemory( void* address );
125  };
126 
128  public:
129  TNonConstUCStringConverter( UInt_t maxSize = UINT_MAX ) : TNonConstCStringConverter( maxSize ) {}
130 
131  public:
132  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
133  };
134 
135 // pointer/array conversions
137  public:
138  TVoidArrayConverter( Bool_t keepControl = kTRUE ) { fKeepControl = keepControl; }
139  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
140  virtual PyObject* FromMemory( void* address );
141  virtual Bool_t ToMemory( PyObject* value, void* address );
142 
143  protected:
144  virtual Bool_t GetAddressSpecialCase( PyObject* pyobject, void*& address );
145 
146  protected:
147  Bool_t KeepControl() { return fKeepControl; }
148 
149  private:
151  };
152 
153  PYROOT_DECLARE_ARRAY_CONVERTER( BoolArray );
154  PYROOT_DECLARE_ARRAY_CONVERTER( ShortArray );
155  PYROOT_DECLARE_ARRAY_CONVERTER( UShortArray );
156  PYROOT_DECLARE_ARRAY_CONVERTER( IntArray );
157  PYROOT_DECLARE_ARRAY_CONVERTER( UIntArray );
158  PYROOT_DECLARE_ARRAY_CONVERTER( LongArray );
159  PYROOT_DECLARE_ARRAY_CONVERTER( ULongArray );
160  PYROOT_DECLARE_ARRAY_CONVERTER( FloatArray );
161  PYROOT_DECLARE_ARRAY_CONVERTER( DoubleArray );
162 
164  public:
165  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
166  };
167 
168 // converters for special cases
170  public:
172  TVoidArrayConverter( keepControl ), fClass( klass ) {}
173 
174  public:
175  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
176  virtual PyObject* FromMemory( void* address );
177  virtual Bool_t ToMemory( PyObject* value, void* address );
178 
179  protected:
181  };
182 
184  public:
186 
187  protected:
188  virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
189  };
190 
192  public:
193  using TStrictCppObjectConverter::TStrictCppObjectConverter;
194 
195  public:
196  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
197  };
198 
200  public:
201  TRefCppObjectConverter( Cppyy::TCppType_t klass ) : fClass( klass ) {}
202 
203  public:
204  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
205 
206  protected:
208  };
209 
210  template <bool ISREFERENCE>
212  public:
214 
215  public:
216  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
217  virtual PyObject* FromMemory( void* address );
218  virtual Bool_t ToMemory( PyObject* value, void* address );
219  };
220 
221  extern template class TCppObjectPtrConverter<true>;
222  extern template class TCppObjectPtrConverter<false>;
223 
225  public:
226  TCppObjectArrayConverter( Cppyy::TCppType_t klass, size_t size, Bool_t keepControl = kFALSE ) :
227  TCppObjectConverter( klass, keepControl ), m_size( size ) {}
228 
229  public:
230  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
231  virtual PyObject* FromMemory( void* address );
232  virtual Bool_t ToMemory( PyObject* value, void* address );
233 
234  protected:
235  size_t m_size;
236  };
237 
238 // CLING WORKAROUND -- classes for STL iterators are completely undefined in that
239 // they come in a bazillion different guises, so just do whatever
241  public:
242  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
243  };
244 // -- END CLING WORKAROUND
245 
247  public:
248  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
249  };
250 
252  public:
253  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
254  virtual PyObject* FromMemory( void* address );
255  };
256 
258 
259 #define PYROOT_DECLARE_STRING_CONVERTER( name, strtype ) \
260  class T##name##Converter : public TCppObjectConverter { \
261  public: \
262  T##name##Converter( Bool_t keepControl = kTRUE ); \
263  public: \
264  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );\
265  virtual PyObject* FromMemory( void* address ); \
266  virtual Bool_t ToMemory( PyObject* value, void* address ); \
267  private: \
268  strtype fBuffer; \
269  }
270 
272  PYROOT_DECLARE_STRING_CONVERTER( STLString, std::string );
273  PYROOT_DECLARE_STRING_CONVERTER( STLStringView, std::string_view );
274 
276  public:
277  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* = 0 );
278  };
279 
280 // smart pointer converter
282  public:
284  Cppyy::TCppType_t rawPtrType,
285  Cppyy::TCppMethod_t deref,
286  Bool_t keepControl = kFALSE,
287  Bool_t handlePtr = kFALSE )
288  : fClass( klass ), fRawPtrType( rawPtrType ), fDereferencer( deref ),
289  fKeepControl( keepControl ), fHandlePtr( handlePtr ) {}
290 
291  public:
292  virtual Bool_t SetArg( PyObject*, TParameter&, TCallContext* ctxt = 0 );
293  virtual PyObject* FromMemory( void* address );
294  //virtual Bool_t ToMemory( PyObject* value, void* address );
295 
296  protected:
297  virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
298 
304  };
305 
306 // create converter from fully qualified type
307  TConverter* CreateConverter( const std::string& fullType, Long_t size = -1 );
308 
309 } // namespace PyROOT
310 
311 #endif // !PYROOT_CONVERTERS_H
Cppyy::TCppType_t fClass
Definition: Converters.h:180
TCppScope_t TCppType_t
Definition: Cppyy.h:13
virtual Bool_t ToMemory(PyObject *value, void *address)
could happen if no derived class override
Definition: Converters.cxx:145
const char * Int
TVoidArrayConverter(Bool_t keepControl=kTRUE)
Definition: Converters.h:138
const char * Long
Basic string class.
Definition: TString.h:129
bool Bool_t
Definition: RtypesCore.h:59
const char * Char
Cppyy::TCppMethod_t fDereferencer
Definition: Converters.h:301
const char * UShort
const char * UInt
PYROOT_DECLARE_REF_CONVERTER(Int)
virtual PyObject * FromMemory(void *address)
Definition: Converters.cxx:135
TRefCppObjectConverter(Cppyy::TCppType_t klass)
Definition: Converters.h:201
TNonConstUCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:129
TCppObjectConverter(Cppyy::TCppType_t klass, Bool_t keepControl=kFALSE)
Definition: Converters.h:171
const char * UChar
const char * Float
TConverter * CreateConverter(const std::string &fullType, Long_t size=-1)
virtual ~TConverter()
Definition: Converters.h:22
TSmartPtrCppObjectConverter(Cppyy::TCppType_t klass, Cppyy::TCppType_t rawPtrType, Cppyy::TCppMethod_t deref, Bool_t keepControl=kFALSE, Bool_t handlePtr=kFALSE)
Definition: Converters.h:283
Named parameter, streamable and storable.
Definition: TParameter.h:37
ptrdiff_t TCppMethod_t
Definition: Cppyy.h:15
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
(1): "null pointer" or C++11 style nullptr
Definition: Converters.h:188
PYROOT_DECLARE_BASIC_CONVERTER2(UInt, ULong)
Cppyy::TCppType_t fClass
Definition: Converters.h:207
PYROOT_DECLARE_ARRAY_CONVERTER(BoolArray)
unsigned int UInt_t
Definition: RtypesCore.h:42
const char * ULong
const char * Double
const Bool_t kFALSE
Definition: RtypesCore.h:92
long Long_t
Definition: RtypesCore.h:50
virtual Bool_t SetArg(PyObject *, TParameter &, TCallContext *ctxt=0)=0
PYROOT_DECLARE_BASIC_CONVERTER(Long)
TCppObjectArrayConverter(Cppyy::TCppType_t klass, size_t size, Bool_t keepControl=kFALSE)
Definition: Converters.h:226
const char * Bool
PYROOT_DECLARE_STRING_CONVERTER(TString, TString)
TCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:106
virtual Bool_t GetAddressSpecialCase(PyObject *, void *&)
Definition: Converters.h:297
TNonConstCStringConverter(UInt_t maxSize=UINT_MAX)
Definition: Converters.h:120
const Bool_t kTRUE
Definition: RtypesCore.h:91
_object PyObject
Definition: TPyArg.h:20
const char * Short