-
Notifications
You must be signed in to change notification settings - Fork 23
/
topyobj.h
389 lines (344 loc) · 9.26 KB
/
topyobj.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef MICROPYTHON_WRAP_DETAIL_TOPYOBJ_H
#define MICROPYTHON_WRAP_DETAIL_TOPYOBJ_H
#include "micropython.h"
#include "util.h"
#include <algorithm>
#include <cstring>
#include <map>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#if UPYWRAP_HAS_CPP17
#include <optional>
#endif
#if UPYWRAP_THROW_ERROR_CODE
#include <system_error>
#endif
namespace upywrap
{
template< class T >
struct SelectToPyObj;
//Create mp_obj_t from Ret
template< class Ret >
struct ToPyObj : std::false_type
{
};
template<>
struct ToPyObj< void > : std::true_type
{
static mp_obj_t Convert()
{
return mp_const_none;
}
};
template<>
struct ToPyObj< mp_obj_t > : std::true_type
{
static mp_obj_t Convert( mp_obj_t arg )
{
return arg;
}
};
template<>
struct ToPyObj< mp_int_t > : std::true_type
{
static mp_obj_t Convert( mp_int_t a )
{
return mp_obj_new_int( a );
}
};
template<>
struct ToPyObj< mp_uint_t > : std::true_type
{
static mp_obj_t Convert( mp_uint_t a )
{
return mp_obj_new_int_from_uint( a );
}
};
template<>
struct ToPyObj< bool > : std::true_type
{
static mp_obj_t Convert( bool a )
{
return a ? mp_const_true : mp_const_false;
}
};
template<>
struct ToPyObj< std::int16_t > : std::true_type
{
static mp_obj_t Convert( std::int16_t arg )
{
return ToPyObj< mp_int_t >::Convert( static_cast< std::int16_t >( arg ) );
}
};
template<>
struct ToPyObj< std::uint16_t > : std::true_type
{
static mp_obj_t Convert( std::uint16_t arg )
{
return ToPyObj< mp_uint_t >::Convert( static_cast< std::uint16_t >( arg ) );
}
};
#if defined( __LP64__ ) || defined( _WIN64 )
//64bit build, we can safely cast 32bit integers to 64bit 'native' uPy integer
static_assert( std::is_same< mp_int_t, std::int64_t >::value, "unsupported integer type" );
static_assert( std::is_same< mp_uint_t, std::uint64_t >::value, "unsupported integer type" );
template<>
struct ToPyObj< std::int32_t > : std::true_type
{
static mp_obj_t Convert( std::int32_t arg )
{
return ToPyObj< mp_int_t >::Convert( static_cast< mp_int_t >( arg ) );
}
};
template<>
struct ToPyObj< std::uint32_t > : std::true_type
{
static mp_obj_t Convert( std::uint32_t arg )
{
return ToPyObj< mp_uint_t >::Convert( static_cast< mp_uint_t >( arg ) );
}
};
#else
//32bit build, 64bit integers are handled through mpz
static_assert( std::is_same< mp_int_t, std::int32_t >::value, "Expected 32bit uPy integer type" );
static_assert( std::is_same< mp_uint_t, std::uint32_t >::value, "Expected 32bit uPy integer type" );
template<>
struct ToPyObj< std::int64_t > : std::true_type
{
static mp_obj_t Convert( std::int64_t arg )
{
static_assert( std::is_same< long long, std::int64_t >::value, "Expected 64bit long long" );
return mp_obj_new_int_from_ll( arg );
}
};
template<>
struct ToPyObj< std::uint64_t > : std::true_type
{
static mp_obj_t Convert( std::uint64_t arg )
{
static_assert( std::is_same< unsigned long long, std::uint64_t >::value, "Expected 64bit long long" );
return mp_obj_new_int_from_ull( arg );
}
};
#endif
template<>
struct ToPyObj< double > : std::true_type
{
static mp_obj_t Convert( double a )
{
return mp_obj_new_float( a );
}
};
template<>
struct ToPyObj< float > : std::true_type
{
static mp_obj_t Convert( float a )
{
return mp_obj_new_float( static_cast< double >( a ) );
}
};
template<>
struct ToPyObj< std::string > : std::true_type
{
static mp_obj_t Convert( const std::string& a )
{
return mp_obj_new_str( reinterpret_cast< const char* >( a.data() ), a.length() );
}
};
#if UPYWRAP_HAS_CPP17
template<>
struct ToPyObj< std::string_view > : std::true_type
{
static mp_obj_t Convert( const std::string_view& a )
{
return mp_obj_new_str( a.data(), a.length() );
}
};
#endif
template<>
struct ToPyObj< const char* > : std::true_type
{
static mp_obj_t Convert( const char* a )
{
return mp_obj_new_str( a, ::strlen( a ) );
}
};
#if UPYWRAP_HAS_CPP17
template< class T >
struct ToPyObj< std::optional< T > > : std::true_type
{
static mp_obj_t Convert( const std::optional< T >& arg )
{
if( arg )
{
return SelectToPyObj< T >::type::Convert( *arg );
}
return mp_const_none;
}
};
#endif
#if UPYWRAP_THROW_ERROR_CODE
template<>
struct ToPyObj< std::error_code > : std::true_type
{
static mp_obj_t Convert( const std::error_code& ec )
{
if( ec )
{
throw std::runtime_error( ec.message() );
}
return mp_const_none;
}
};
inline bool HasErrorCode()
{
return true;
}
#else
inline bool HasErrorCode()
{
return false;
}
#endif
//Generic conversion of pair of iterators to uPy list, so external code
//can use this to build converters for more types than just vector.
template< class It, class Transform >
static mp_obj_t ConvertToList( It begin, size_t numItems, Transform transform )
{
auto list = reinterpret_cast< mp_obj_list_t* >( MP_OBJ_TO_PTR( mp_obj_new_list( numItems, nullptr ) ) );
std::transform( begin, begin + numItems, list->items, transform );
return list;
}
template< class T >
struct ToPyObj< std::vector< T > > : std::true_type
{
static mp_obj_t Convert( const std::vector< T >& a )
{
return ConvertToList( a.cbegin(), a.size(), SelectToPyObj< T >::type::Convert );
}
};
template< class K, class V >
struct ToPyObj< std::map< K, V > > : std::true_type
{
static mp_obj_t Convert( const std::map< K, V >& a )
{
const auto numItems = a.size();
auto dict = mp_obj_new_dict( numItems );
std::for_each( a.cbegin(), a.cend(), [&dict] ( decltype( *a.cbegin() )& p )
{
mp_obj_dict_store( dict, SelectToPyObj< K >::type::Convert( p.first ), SelectToPyObj< V >::type::Convert( p.second ) );
} );
return dict;
}
};
namespace detail
{
struct AddConvertedToVec
{
AddConvertedToVec( mp_obj_t* items ) :
items( items ),
next( 0 )
{
}
template< class T >
void operator ()( const T& a )
{
items[ next ] = SelectToPyObj< T >::type::Convert( a );
++next;
}
private:
mp_obj_t* items;
size_t next;
};
}
template< class... A >
struct ToPyObj< std::tuple< A... > > : std::true_type
{
typedef std::tuple< A... > tuple_type;
static mp_obj_t Convert( const tuple_type& a )
{
const auto numItems = sizeof...( A );
auto tuple = reinterpret_cast< mp_obj_tuple_t* >( MP_OBJ_TO_PTR( mp_obj_new_tuple( numItems, nullptr ) ) );
detail::AddConvertedToVec addtoVec( tuple->items );
apply( addtoVec, a );
return tuple;
}
};
template< class A, class B >
struct ToPyObj< std::pair< A, B > > : std::true_type
{
static mp_obj_t Convert( const std::pair< A, B >& p )
{
return ToPyObj< std::tuple< A, B > >::Convert( p );
}
};
#if UPYWRAP_PASSCONSTREF
template< class T >
struct IsSupportedToPyObjQualifier : std::integral_constant
<
bool,
std::is_same< T, typename remove_all< T >::type >::value ||
std::is_same< T, const T& >::value
>
{
};
#else
template< class T >
struct IsSupportedToPyObjQualifier : std::is_same< T, typename remove_all< T >::type >
{
};
#endif
//Store ClassWrapper in mp_obj_t
template< class T >
struct ClassToPyObj;
//Select bewteen ToPyObj and ClassToPyObj
template< class T >
struct SelectToPyObj
{
typedef ToPyObj< typename remove_all< T >::type > builtin_type;
typedef ClassToPyObj< T > class_type;
typedef typename std::conditional< builtin_type::value, IsSupportedToPyObjQualifier< T >, std::true_type >::type is_valid_builtinq;
static_assert( is_valid_builtinq::value, "Unsupported qualifier for builtin uPy types (must be returned by value)" );
typedef typename std::conditional< builtin_type::value, builtin_type, class_type >::type type;
};
#if UPYWRAP_USE_CHARSTRING
template<>
struct SelectToPyObj< const char* >
{
typedef ToPyObj< const char* > type;
};
#endif
template<>
struct SelectToPyObj< mp_obj_t >
{
typedef ToPyObj< mp_obj_t > type;
};
template< class T >
mp_obj_t ToPy( const T& arg )
{
return SelectToPyObj< T >::type::Convert( arg );
}
#if UPYWRAP_USE_CHARSTRING
inline mp_obj_t ToPy( const char* arg )
{
return SelectToPyObj< const char* >::type::Convert( arg );
}
#endif
inline mp_obj_t ToPy( mp_obj_t arg )
{
return SelectToPyObj< mp_obj_t >::type::Convert( arg );
}
template< class T >
mp_obj_t ToPy( T& arg, typename std::enable_if< !ToPyObj< typename remove_all< T >::type >::value && !is_shared_ptr< T >::value >::type* = nullptr )
{
return SelectToPyObj< T& >::type::Convert( arg );
}
template< class T >
mp_obj_t ToPy( std::shared_ptr< T > arg, typename std::enable_if< !ToPyObj< typename remove_all< T >::type >::value && !std::is_reference< T >::value >::type* = nullptr )
{
return SelectToPyObj< std::shared_ptr< T > >::type::Convert( arg );
}
}
#endif //#ifndef MICROPYTHON_WRAP_DETAIL_TOPYOBJ_H