@@ -1250,4 +1250,68 @@ private static T DefaultGetter(IntPtr op)
1250
1250
// }
1251
1251
//}
1252
1252
}
1253
+
1254
+
1255
+ static class PyValueConverterHelper
1256
+ {
1257
+ internal static Dictionary < Type , Func < object , IntPtr > > ConvertMap = new Dictionary < Type , Func < object , IntPtr > > ( ) ;
1258
+
1259
+ static PyValueConverterHelper ( )
1260
+ {
1261
+ PyValueConverter < sbyte > . Convert = ( value ) => Runtime . PyInt_FromInt32 ( value ) ;
1262
+ PyValueConverter < byte > . Convert = ( value ) => Runtime . PyInt_FromInt32 ( value ) ;
1263
+ PyValueConverter < short > . Convert = ( value ) => Runtime . PyInt_FromInt32 ( value ) ;
1264
+ PyValueConverter < ushort > . Convert = ( value ) => Runtime . PyInt_FromInt32 ( value ) ;
1265
+ PyValueConverter < int > . Convert = Runtime . PyInt_FromInt32 ;
1266
+ PyValueConverter < Int64 > . Convert = Runtime . PyLong_FromLongLong ;
1267
+
1268
+ PyValueConverter < uint > . Convert = Runtime . PyLong_FromUnsignedLong ;
1269
+ PyValueConverter < UInt64 > . Convert = Runtime . PyLong_FromUnsignedLongLong ;
1270
+
1271
+ PyValueConverter < float > . Convert = ( value ) => Runtime . PyFloat_FromDouble ( value ) ;
1272
+ PyValueConverter < double > . Convert = Runtime . PyFloat_FromDouble ;
1273
+
1274
+ PyValueConverter < string > . Convert = Runtime . PyUnicode_FromString ;
1275
+ PyValueConverter < bool > . Convert = ( value ) =>
1276
+ {
1277
+ if ( value )
1278
+ {
1279
+ Runtime . Py_IncRef ( Runtime . PyTrue ) ;
1280
+ return Runtime . PyTrue ;
1281
+ }
1282
+ Runtime . XIncref ( Runtime . PyFalse ) ;
1283
+ return Runtime . PyFalse ;
1284
+ } ;
1285
+ }
1286
+
1287
+ internal static IntPtr Convert ( object value )
1288
+ {
1289
+ Func < object , IntPtr > converter ;
1290
+ if ( ConvertMap . TryGetValue ( value . GetType ( ) , out converter ) )
1291
+ {
1292
+ return converter ( value ) ;
1293
+ }
1294
+ return PyValueConverter < object > . Convert ( value ) ;
1295
+ }
1296
+ }
1297
+
1298
+
1299
+ static class PyValueConverter < T >
1300
+ {
1301
+ public static Func < T , IntPtr > Convert = DefaultConverter ;
1302
+
1303
+ static IntPtr DefaultConverter ( T value )
1304
+ {
1305
+ if ( value is IEnumerable )
1306
+ {
1307
+ IntPtr list = Runtime . PyList_New ( 0 ) ;
1308
+ foreach ( var item in ( IEnumerable ) value )
1309
+ {
1310
+ IntPtr pyValue = PyValueConverterHelper . Convert ( item ) ;
1311
+ Runtime . PyList_Append ( list , pyValue ) ;
1312
+ }
1313
+ }
1314
+ return CLRObject . GetInstHandle ( value ) ;
1315
+ }
1316
+ }
1253
1317
}
0 commit comments