diff options
author | David Shah <davey1576@gmail.com> | 2018-07-04 14:13:55 +0200 |
---|---|---|
committer | David Shah <davey1576@gmail.com> | 2018-07-04 14:55:24 +0200 |
commit | 79e91368f901343c7f0c88d5841ae231432a4c0b (patch) | |
tree | a98d774de1b76bece93fdbc66ce04f251eba8357 /common | |
parent | 4376ae43af4f44e6f13fc5fd2355cbe59adcb425 (diff) | |
download | nextpnr-79e91368f901343c7f0c88d5841ae231432a4c0b.tar.gz nextpnr-79e91368f901343c7f0c88d5841ae231432a4c0b.tar.bz2 nextpnr-79e91368f901343c7f0c88d5841ae231432a4c0b.zip |
python: Update wrapper for non-unique_ptr maps
Signed-off-by: David Shah <davey1576@gmail.com>
Diffstat (limited to 'common')
-rw-r--r-- | common/pybindings.cc | 23 | ||||
-rw-r--r-- | common/pycontainers.h | 68 |
2 files changed, 49 insertions, 42 deletions
diff --git a/common/pybindings.cc b/common/pybindings.cc index 5ad8a983..a85c5fe2 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -21,6 +21,7 @@ #ifndef NO_PYTHON #include "pybindings.h" +#include "arch_pybindings.h" #include "jsonparse.h" #include "nextpnr.h" @@ -105,32 +106,20 @@ BOOST_PYTHON_MODULE(MODULE_NAME) .def_readwrite("net", &PortInfo::net) .def_readwrite("type", &PortInfo::type); - /*class_<CellInfo, CellInfo *>("CellInfo") - .def_readwrite("name", &CellInfo::name) - .def_readwrite("type", &CellInfo::type) - .def_readwrite("ports", &CellInfo::ports) - .def_readwrite("attrs", &CellInfo::attrs) - .def_readwrite("params", &CellInfo::params) - .def_readwrite("bel", &CellInfo::bel) - .def_readwrite("pins", &CellInfo::pins);*/ - - // WRAP_MAP(decltype(CellInfo::ports), "IdPortMap"); - // WRAP_MAP(decltype(CellInfo::pins), "IdIdMap"); - class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init); - //.add_property("nets", make_getter(&Context::nets, return_internal_reference<>())) - //.add_property("cells", make_getter(&Context::cells, return_internal_reference<>())); - // WRAP_MAP_UPTR(decltype(Context::nets), "IdNetMap"); - // WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap"); + typedef std::unordered_map<IdString, std::string> AttrMap; auto ci_cls = class_<ContextualWrapper<CellInfo &>>("CellInfo", no_init); readonly_wrapper<CellInfo &, typeof(&CellInfo::type), &CellInfo::type, conv_to_str<IdString>>::def_wrap(ci_cls, "type"); - + readonly_wrapper<CellInfo &, typeof(&CellInfo::attrs), &CellInfo::attrs, wrap_context<AttrMap &>>::def_wrap( + ci_cls, "attrs"); def("parse_json", parse_json_shim); def("load_design", load_design_shim, return_value_policy<manage_new_object>()); + WRAP_MAP(AttrMap, pass_through<std::string>, "AttrMap"); + arch_wrap_python(); } diff --git a/common/pycontainers.h b/common/pycontainers.h index cc3c2c84..c8d2257b 100644 --- a/common/pycontainers.h +++ b/common/pycontainers.h @@ -188,20 +188,23 @@ template <typename T1, typename T2> struct pair_wrapper /* Special case of above for map key/values */ -template <typename T1, typename T2> struct map_pair_wrapper +template <typename T1, typename T2, typename value_conv> struct map_pair_wrapper { typedef std::pair<T1, T2> T; + typedef PythonConversion::ContextualWrapper<T &> wrapped_pair; + typedef typename T::second_type V; struct pair_iterator_wrapper { - static object next(std::pair<T &, int> &iter) + static object next(std::pair<wrapped_pair &, int> &iter) { if (iter.second == 0) { iter.second++; - return object(iter.first.first); + return object(PythonConversion::string_converter<typeof(iter.first.base.first)>().to_str( + iter.first.ctx, iter.first.base.first)); } else if (iter.second == 1) { iter.second++; - return object(iter.first.second); + return object(value_conv()(iter.first.ctx, iter.first.base.second)); } else { PyErr_SetString(PyExc_StopIteration, "End of range reached"); boost::python::throw_error_already_set(); @@ -213,30 +216,37 @@ template <typename T1, typename T2> struct map_pair_wrapper static void wrap(const char *python_name) { - class_<std::pair<T &, int>>(python_name, no_init).def("__next__", next); + class_<std::pair<wrapped_pair &, int>>(python_name, no_init).def("__next__", next); } }; - static object get(T &x, int i) + static object get(wrapped_pair &x, int i) { if ((i >= 2) || (i < 0)) KeyError(); - return (i == 1) ? object(x.second) : object(x.first); + return (i == 1) ? object(value_conv()(x.ctx, x.base.second)) : object(x.base.first); } - static int len(T &x) { return 2; } + static int len(wrapped_pair &x) { return 2; } - static std::pair<T &, int> iter(T &x) { return std::make_pair(boost::ref(x), 0); }; + static std::pair<wrapped_pair &, int> iter(wrapped_pair &x) { return std::make_pair(boost::ref(x), 0); }; + + static std::string first_getter(wrapped_pair &t) + { + return PythonConversion::string_converter<typeof(t.base.first)>().to_str(t.ctx, t.base.first); + } + + static typename value_conv::ret_type second_getter(wrapped_pair &t) { return value_conv()(t.ctx, t.base.second); } static void wrap(const char *pair_name, const char *iter_name) { pair_iterator_wrapper::wrap(iter_name); - class_<T>(pair_name, no_init) + class_<wrapped_pair>(pair_name, no_init) .def("__iter__", iter) .def("__len__", len) .def("__getitem__", get) - .def_readonly("first", &T::first) - .def_readwrite("second", &T::second); + .add_property("first", first_getter) + .add_property("second", second_getter); } }; @@ -244,26 +254,33 @@ template <typename T1, typename T2> struct map_pair_wrapper Wrapper for a map, either an unordered_map, regular map or dict */ -template <typename T> struct map_wrapper +template <typename T, typename value_conv> struct map_wrapper { typedef typename std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K; typedef typename T::mapped_type V; + typedef typename value_conv::ret_type wrapped_V; typedef typename T::value_type KV; + typedef typename PythonConversion::ContextualWrapper<T &> wrapped_map; - static V &get(T &x, K const &i) + static wrapped_V get(wrapped_map &x, std::string const &i) { - if (x.find(i) != x.end()) - return x.at(i); + K k = PythonConversion::string_converter<K>().from_str(x.ctx, i); + if (x.base.find(k) != x.base.end()) + return value_conv()(x.ctx, x.base.at(k)); KeyError(); std::terminate(); } - static void set(T &x, K const &i, V &v) { x[i] = v; } + static void set(wrapped_map &x, std::string const &i, V const &v) + { + x.base[PythonConversion::string_converter<K>().from_str(x.ctx, i)] = v; + } - static void del(T const &x, K const &i) + static void del(T const &x, std::string const &i) { - if (x.find(i) != x.end()) - x.erase(i); + K k = PythonConversion::string_converter<K>().from_str(x.ctx, i); + if (x.base.find(k) != x.base.end()) + x.base.erase(k); else KeyError(); std::terminate(); @@ -271,13 +288,13 @@ template <typename T> struct map_wrapper static void wrap(const char *map_name, const char *kv_name, const char *kv_iter_name, const char *iter_name) { - map_pair_wrapper<typename KV::first_type, typename KV::second_type>::wrap(kv_name, kv_iter_name); - typedef range_wrapper<T, return_value_policy<copy_non_const_reference>> rw; + map_pair_wrapper<typename KV::first_type, typename KV::second_type, value_conv>::wrap(kv_name, kv_iter_name); + typedef range_wrapper<T &, return_value_policy<return_by_value>, PythonConversion::wrap_context<KV &>> rw; typename rw::iter_wrap().wrap(iter_name); - class_<T>(map_name, no_init) + class_<wrapped_map>(map_name, no_init) .def("__iter__", rw::iter) .def("__len__", &T::size) - .def("__getitem__", get, return_internal_reference<>()) + .def("__getitem__", get) .def("__setitem__", set, with_custodian_and_ward<1, 2>()); } }; @@ -400,7 +417,8 @@ template <typename T> struct map_wrapper_uptr } }; -#define WRAP_MAP(t, name) map_wrapper<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") +#define WRAP_MAP(t, conv, name) \ + map_wrapper<t, conv>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") #define WRAP_MAP_UPTR(t, name) \ map_wrapper_uptr<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator") |