From 1e96d65ded029f68bb294cfb25c56f138dd51180 Mon Sep 17 00:00:00 2001 From: David Shah Date: Mon, 2 Jul 2018 15:59:18 +0200 Subject: python: Add context wrapper support for ranges Signed-off-by: David Shah --- common/pybindings.h | 56 ++++++++++++----------------------------------------- 1 file changed, 12 insertions(+), 44 deletions(-) (limited to 'common/pybindings.h') diff --git a/common/pybindings.h b/common/pybindings.h index 1565a138..852fb21f 100644 --- a/common/pybindings.h +++ b/common/pybindings.h @@ -36,50 +36,6 @@ NEXTPNR_NAMESPACE_BEGIN using namespace boost::python; -/* -A wrapper to enable custom type/ID to/from string conversions - */ -template struct string_wrapper -{ - template struct from_pystring_converter - { - from_pystring_converter() - { - converter::registry::push_back(&convertible, &construct, boost::python::type_id()); - }; - - static void *convertible(PyObject *object) { return PyUnicode_Check(object) ? object : 0; } - - static void construct(PyObject *object, converter::rvalue_from_python_stage1_data *data) - { - const wchar_t *value = PyUnicode_AsUnicode(object); - const std::wstring value_ws(value); - if (value == 0) - throw_error_already_set(); - void *storage = ((boost::python::converter::rvalue_from_python_storage *)data)->storage.bytes; - new (storage) T(fn(std::string(value_ws.begin(), value_ws.end()))); - data->convertible = storage; - } - - static F fn; - }; - - template struct to_str_wrapper - { - static F fn; - - std::string str(T &x) { return fn(x); } - }; - - template static void wrap(const char *type_name, F1 to_str_fn, F2 from_str_fn) - { - from_pystring_converter::fn = from_str_fn; - from_pystring_converter(); - to_str_wrapper::fn = to_str_fn; - class_(type_name, no_init).def("__str__", to_str_wrapper::str); - }; -}; - std::string parse_python_exception(); template void python_export_global(const char *name, Tn &x) @@ -106,6 +62,18 @@ void deinit_python(); void execute_python_file(const char *python_file); +// Defauld IdString conversions +namespace PythonConversion { + +template <> struct string_converter +{ + inline IdString from_str(Context *ctx, std::string name) { return ctx->id(name); } + + inline std::string to_str(Context *ctx, IdString id) { return id.str(ctx); } +}; + +} // namespace PythonConversion + NEXTPNR_NAMESPACE_END #endif /* end of include guard: COMMON_PYBINDINGS_HH */ -- cgit v1.2.3