# generated from XKB map no include common map 0x414 exclam 0x02 shift exclamdown 0x02 altgr onesuperior 0x02 shift altgr quotedbl 0x03 shift at 0x03 altgr twosuperior 0x03 shift altgr numbersign 0x04 shift sterling 0x04 altgr threesuperior 0x04 shift altgr currency 0x05 shift dollar 0x05 altgr onequarter 0x05 shift altgr percent 0x06 shift onehalf 0x06 altgr cent 0x06 shift altgr ampersand 0x07 shift yen 0x07 altgr fiveeighths 0x07 shift altgr slash 0x08 shift braceleft 0x08 altgr division 0x08 shift altgr parenleft 0x09 shift bracketleft 0x09 altgr guillemotleft 0x09 shift altgr parenright 0x0a shift bracketright 0x0a altgr guillemotright 0x0a shift altgr equal 0x0b shift braceright 0x0b altgr degree 0x0b shift altgr plus 0x0c question 0x0c shift plusminus 0x0c altgr questiondown 0x0c shift altgr backslash 0x0d dead_grave 0x0d shift dead_acute 0x0d altgr notsign 0x0d shift altgr Greek_OMEGA 0x10 shift altgr lstroke 0x11 altgr Lstroke 0x11 shift altgr EuroSign 0x12 altgr cent 0x12 shift altgr registered 0x13 altgr thorn 0x14 altgr THORN 0x14 shift altgr leftarrow 0x15 altgr yen 0x15 shift altgr downarrow 0x16 altgr uparrow 0x16 shift altgr rightarrow 0x17 altgr idotless 0x17 shift altgr oe 0x18 altgr OE 0x18 shift altgr thorn 0x19 altgr THORN 0x19 shift altgr aring 0x1a Aring 0x1a shift dead_diaeresis 0x1a altgr dead_abovering 0x1a shift altgr dead_diaeresis 0x1b dead_circumflex 0x1b shift asciicircum 0x01b shift dead_tilde 0x1b altgr asciitilde 0x1b altgr dead_caron 0x1b shift altgr ordfeminine 0x1e altgr masculine 0x1e shift altgr ssharp 0x1f altgr section 0x1f shift altgr eth 0x20 altgr ETH 0x20 shift altgr dstroke 0x21 altgr ordfeminine 0x21 shift altgr eng 0x22 altgr ENG 0x22 shift altgr hstroke 0x23 altgr Hstroke 0x23 shift altgr kra 0x25 altgr lstroke 0x26 altgr Lstroke 0x26 shift altgr oslash 0x27 Ooblique 0x27 shift dead_doubleacute 0x27 shift altgr ae 0x28 AE 0x28 shift dead_caron 0x28 shift altgr bar 0x29 section 0x29 shift brokenbar 0x29 altgr paragraph 0x29 shift altgr apostrophe 0x2b asterisk 0x2b shift multiply 0x2b shift altgr guillemotleft 0x2c altgr guillemotright 0x2d altgr copyright 0x2e altgr leftdoublequotemark 0x2f altgr rightdoublequotemark 0x30 altgr mu 0x32 altgr masculine 0x32 shift altgr comma 0x33 semicolon 0x33 shift dead_cedilla 0x33 altgr dead_ogonek 0x33 shift altgr period 0x34 colon 0x34 shift periodcentered 0x34 altgr dead_abovedot 0x34 shift altgr minus 0x35 underscore 0x35 shift hyphen 0x35 altgr macron 0x35 shift altgr nobreakspace 0x39 altgr onehalf 0x56 altgr threequarters 0x56 shift altgr h?id=054be6fb6700eae02ec1a47ecf3d4c2838ee06d2'>pywrappers.h
blob: d50af4c3b7237a640ad695e2718cd6f9f9b2fe35 (plain)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 *  nextpnr -- Next Generation Place and Route
 *
 *  Copyright (C) 2018  Clifford Wolf <clifford@symbioticeda.com>
 *  Copyright (C) 2018  David Shah <david@symbioticeda.com>
 *
 *  Permission to use, copy, modify, and/or distribute this software for any
 *  purpose with or without fee is hereby granted, provided that the above
 *  copyright notice and this permission notice appear in all copies.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 *  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 *  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 *  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 *  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef PYWRAPPERS_H
#define PYWRAPPERS_H

#include <boost/function_types/function_arity.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/map_indexing_suite.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <utility>
#include "nextpnr.h"

NEXTPNR_NAMESPACE_BEGIN

using namespace boost::python;

namespace PythonConversion {
template <typename T> struct ContextualWrapper
{
    Context *ctx;
    T base;

    inline ContextualWrapper(Context *c, T x) : ctx(c), base(x){};

    inline operator T() { return base; };
    typedef T base_type;
};

template <typename T> struct WrapIfNotContext
{
    typedef ContextualWrapper<T> maybe_wrapped_t;
};

template <> struct WrapIfNotContext<Context>
{
    typedef Context maybe_wrapped_t;
};

template <typename T> inline Context *get_ctx(typename WrapIfNotContext<T>::maybe_wrapped_t &wrp_ctx)
{
    return wrp_ctx.ctx;
}

template <> inline Context *get_ctx<Context>(WrapIfNotContext<Context>::maybe_wrapped_t &unwrp_ctx)
{
    return &unwrp_ctx;
}

template <typename T> inline T &get_base(typename WrapIfNotContext<T>::maybe_wrapped_t &wrp_ctx)
{
    return wrp_ctx.base;
}

template <> inline Context &get_base<Context>(WrapIfNotContext<Context>::maybe_wrapped_t &unwrp_ctx)
{
    return unwrp_ctx;
}

template <typename T> ContextualWrapper<T> wrap_ctx(Context *ctx, T x) { return ContextualWrapper<T>(ctx, x); }

// Dummy class, to be implemented by users
template <typename T> struct string_converter;

class bad_wrap
{
};

// Action options
template <typename T> struct pass_through
{
    inline T operator()(Context *ctx, T x) { return x; }

    using ret_type = T;
    using arg_type = T;
};

template <typename T> struct wrap_context
{
    inline ContextualWrapper<T> operator()(Context *ctx, T x) { return ContextualWrapper<T>(ctx, x); }

    using arg_type = T;
    using ret_type = ContextualWrapper<T>;
};

template <typename T> struct unwrap_context
{
    inline T operator()(Context *ctx, ContextualWrapper<T> x) { return x.base; }

    using ret_type = T;
    using arg_type = ContextualWrapper<T>;
};

template <typename T> struct conv_from_str
{
    inline T operator()(Context *ctx, std::string x) { return string_converter<T>().from_str(ctx, x); }

    using ret_type = T;
    using arg_type = std::string;
};

template <typename T> struct conv_to_str
{
    inline std::string operator()(Context *ctx, T x) { return string_converter<T>().to_str(ctx, x); }

    using ret_type = std::string;
    using arg_type = T;
};

template <typename T> struct deref_and_wrap
{
    inline ContextualWrapper<T &> operator()(Context *ctx, T *x)
    {
        if (x == nullptr)
            throw bad_wrap();
        return ContextualWrapper<T &>(ctx, *x);
    }

    using arg_type = T *;
    using ret_type = ContextualWrapper<T &>;
};

template <typename T> struct addr_and_unwrap
{
    inline T *operator()(Context *ctx, ContextualWrapper<T &> x) { return &(x.base); }

    using arg_type = ContextualWrapper<T &>;
    using ret_type = T *;
};

// Function wrapper
// Zero parameters, one return
template <typename Class, typename FuncT, FuncT fn, typename rv_conv> struct fn_wrapper_0a
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_result_type = typename rv_conv::ret_type;

    static object wrapped_fn(class_type &cls)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(rv_conv()(ctx, (base.*fn)()));
        } catch (bad_wrap &) {
            return object();
        }
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
};

// One parameter, one return
template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv> struct fn_wrapper_1a
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_result_type = typename rv_conv::ret_type;
    using conv_arg1_type = typename arg1_conv::arg_type;

    static object wrapped_fn(class_type &cls, conv_arg1_type arg1)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1))));
        } catch (bad_wrap &) {
            return object();
        }
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
};

// Two parameters, one return
template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv, typename arg2_conv>
struct fn_wrapper_2a
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_result_type = typename rv_conv::ret_type;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;

    static object wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2))));
        } catch (bad_wrap &) {
            return object();
        }
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
};

// Three parameters, one return
template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename arg1_conv, typename arg2_conv,
          typename arg3_conv>
struct fn_wrapper_3a
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_result_type = typename rv_conv::ret_type;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;
    using conv_arg3_type = typename arg3_conv::arg_type;

    static object wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(
                    rv_conv()(ctx, (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3))));
        } catch (bad_wrap &) {
            return object();
        }
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
};

// Zero parameters void
template <typename Class, typename FuncT, FuncT fn> struct fn_wrapper_0a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;

    static void wrapped_fn(class_type &cls)
    {
        Class &base = get_base<Class>(cls);
        return (base.*fn)();
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
};

// One parameter, void
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv> struct fn_wrapper_1a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, Ta a = arg("arg1"))
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Two parameters, no return
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv> struct fn_wrapper_2a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, const Ta &a)
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Three parameters, no return
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv, typename arg3_conv>
struct fn_wrapper_3a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;
    using conv_arg3_type = typename arg3_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, const Ta &a)
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Four parameters, no return
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv, typename arg3_conv,
          typename arg4_conv>
struct fn_wrapper_4a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;
    using conv_arg3_type = typename arg3_conv::arg_type;
    using conv_arg4_type = typename arg4_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3,
                           conv_arg4_type arg4)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3),
                          arg4_conv()(ctx, arg4));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, const Ta &a)
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Five parameters, no return
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv, typename arg3_conv,
          typename arg4_conv, typename arg5_conv>
struct fn_wrapper_5a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;
    using conv_arg3_type = typename arg3_conv::arg_type;
    using conv_arg4_type = typename arg4_conv::arg_type;
    using conv_arg5_type = typename arg5_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3,
                           conv_arg4_type arg4, conv_arg5_type arg5)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3),
                          arg4_conv()(ctx, arg4), arg5_conv()(ctx, arg5));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, const Ta &a)
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Six parameters, no return
template <typename Class, typename FuncT, FuncT fn, typename arg1_conv, typename arg2_conv, typename arg3_conv,
          typename arg4_conv, typename arg5_conv, typename arg6_conv>
struct fn_wrapper_6a_v
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_arg1_type = typename arg1_conv::arg_type;
    using conv_arg2_type = typename arg2_conv::arg_type;
    using conv_arg3_type = typename arg3_conv::arg_type;
    using conv_arg4_type = typename arg4_conv::arg_type;
    using conv_arg5_type = typename arg5_conv::arg_type;
    using conv_arg6_type = typename arg6_conv::arg_type;

    static void wrapped_fn(class_type &cls, conv_arg1_type arg1, conv_arg2_type arg2, conv_arg3_type arg3,
                           conv_arg4_type arg4, conv_arg5_type arg5, conv_arg6_type arg6)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        return (base.*fn)(arg1_conv()(ctx, arg1), arg2_conv()(ctx, arg2), arg3_conv()(ctx, arg3),
                          arg4_conv()(ctx, arg4), arg5_conv()(ctx, arg5), arg6_conv()(ctx, arg6));
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }

    template <typename WrapCls, typename Ta> static void def_wrap(WrapCls cls_, const char *name, const Ta &a)
    {
        cls_.def(name, wrapped_fn, a);
    }
};

// Wrapped getter
template <typename Class, typename MemT, MemT mem, typename v_conv> struct readonly_wrapper
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_val_type = typename v_conv::ret_type;

    static object wrapped_getter(class_type &cls)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(v_conv()(ctx, (base.*mem)));
        } catch (bad_wrap &) {
            return object();
        }
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name)
    {
        cls_.add_property(name, wrapped_getter);
    }
};

// Wrapped getter/setter
template <typename Class, typename MemT, MemT mem, typename get_conv, typename set_conv> struct readwrite_wrapper
{
    using class_type = typename WrapIfNotContext<Class>::maybe_wrapped_t;
    using conv_val_type = typename get_conv::ret_type;

    static object wrapped_getter(class_type &cls)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        try {
            return object(get_conv()(ctx, (base.*mem)));
        } catch (bad_wrap &) {
            return object();
        }
    }

    using conv_arg_type = typename set_conv::arg_type;

    static void wrapped_setter(class_type &cls, conv_arg_type val)
    {
        Context *ctx = get_ctx<Class>(cls);
        Class &base = get_base<Class>(cls);
        (base.*mem) = set_conv()(ctx, val);
    }

    template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name)
    {
        cls_.add_property(name, wrapped_getter, wrapped_setter);
    }
};

} // namespace PythonConversion

NEXTPNR_NAMESPACE_END

#endif