python: Developing context wrappers for maps
Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
parent
45ec502ded
commit
a382d906ef
@ -107,9 +107,9 @@ BOOST_PYTHON_MODULE(MODULE_NAME)
|
|||||||
// WRAP_MAP(decltype(CellInfo::ports), "IdPortMap");
|
// WRAP_MAP(decltype(CellInfo::ports), "IdPortMap");
|
||||||
// WRAP_MAP(decltype(CellInfo::pins), "IdIdMap");
|
// WRAP_MAP(decltype(CellInfo::pins), "IdIdMap");
|
||||||
|
|
||||||
class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init)
|
class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init);
|
||||||
.add_property("nets", make_getter(&Context::nets, return_internal_reference<>()))
|
//.add_property("nets", make_getter(&Context::nets, return_internal_reference<>()))
|
||||||
.add_property("cells", make_getter(&Context::cells, 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::nets), "IdNetMap");
|
||||||
// WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap");
|
// WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap");
|
||||||
|
@ -72,6 +72,13 @@ template <> struct string_converter<IdString>
|
|||||||
inline std::string to_str(Context *ctx, IdString id) { return id.str(ctx); }
|
inline std::string to_str(Context *ctx, IdString id) { return id.str(ctx); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <> struct string_converter<const IdString>
|
||||||
|
{
|
||||||
|
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
|
} // namespace PythonConversion
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_END
|
NEXTPNR_NAMESPACE_END
|
||||||
|
@ -112,7 +112,7 @@ struct range_wrapper
|
|||||||
iterator_wrapper<iterator_t, P, value_conv>().wrap(iter_name);
|
iterator_wrapper<iterator_t, P, value_conv>().wrap(iter_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef iterator_wrapper<iterator_t, P> iter_wrap;
|
typedef iterator_wrapper<iterator_t, P, value_conv> iter_wrap;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define WRAP_RANGE(t, conv) \
|
#define WRAP_RANGE(t, conv) \
|
||||||
@ -288,18 +288,20 @@ Special case of above for map key/values where value is a unique_ptr
|
|||||||
template <typename T1, typename T2> struct map_pair_wrapper_uptr
|
template <typename T1, typename T2> struct map_pair_wrapper_uptr
|
||||||
{
|
{
|
||||||
typedef std::pair<T1, T2> T;
|
typedef std::pair<T1, T2> T;
|
||||||
|
typedef PythonConversion::ContextualWrapper<T &> wrapped_pair;
|
||||||
typedef typename T::second_type::element_type V;
|
typedef typename T::second_type::element_type V;
|
||||||
|
|
||||||
struct pair_iterator_wrapper
|
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) {
|
if (iter.second == 0) {
|
||||||
iter.second++;
|
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) {
|
} else if (iter.second == 1) {
|
||||||
iter.second++;
|
iter.second++;
|
||||||
return object(iter.first.second.get());
|
return object(PythonConversion::ContextualWrapper<V &>(iter.first.ctx, *iter.first.base.second.get()));
|
||||||
} else {
|
} else {
|
||||||
PyErr_SetString(PyExc_StopIteration, "End of range reached");
|
PyErr_SetString(PyExc_StopIteration, "End of range reached");
|
||||||
boost::python::throw_error_already_set();
|
boost::python::throw_error_already_set();
|
||||||
@ -311,32 +313,41 @@ template <typename T1, typename T2> struct map_pair_wrapper_uptr
|
|||||||
|
|
||||||
static void wrap(const char *python_name)
|
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))
|
if ((i >= 2) || (i < 0))
|
||||||
KeyError();
|
KeyError();
|
||||||
return (i == 1) ? object(x.second.get()) : object(x.first);
|
return (i == 1) ? object(PythonConversion::ContextualWrapper<V &>(x.ctx, *x.base.second.get()))
|
||||||
|
: 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 V &second_getter(T &t) { return *t.second.get(); }
|
static std::string first_getter(wrapped_pair &t)
|
||||||
|
{
|
||||||
|
return PythonConversion::string_converter<typeof(t.base.first)>().to_str(t.ctx, t.base.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
static PythonConversion::ContextualWrapper<V &> second_getter(wrapped_pair &t)
|
||||||
|
{
|
||||||
|
return PythonConversion::ContextualWrapper<V &>(t.ctx, *t.base.second.get());
|
||||||
|
}
|
||||||
|
|
||||||
static void wrap(const char *pair_name, const char *iter_name)
|
static void wrap(const char *pair_name, const char *iter_name)
|
||||||
{
|
{
|
||||||
pair_iterator_wrapper::wrap(iter_name);
|
pair_iterator_wrapper::wrap(iter_name);
|
||||||
class_<T, boost::noncopyable>(pair_name, no_init)
|
class_<wrapped_pair, boost::noncopyable>(pair_name, no_init)
|
||||||
.def("__iter__", iter)
|
.def("__iter__", iter)
|
||||||
.def("__len__", len)
|
.def("__len__", len)
|
||||||
.def("__getitem__", get)
|
.def("__getitem__", get)
|
||||||
.def_readonly("first", &T::first)
|
.add_property("first", first_getter)
|
||||||
.add_property("second", make_function(second_getter, return_internal_reference<>()));
|
.add_property("second", second_getter);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -348,22 +359,29 @@ template <typename T> struct map_wrapper_uptr
|
|||||||
{
|
{
|
||||||
typedef typename std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K;
|
typedef typename std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K;
|
||||||
typedef typename T::mapped_type::pointer V;
|
typedef typename T::mapped_type::pointer V;
|
||||||
|
typedef typename T::mapped_type::element_type &Vr;
|
||||||
typedef typename T::value_type KV;
|
typedef typename T::value_type KV;
|
||||||
|
typedef typename PythonConversion::ContextualWrapper<T &> wrapped_map;
|
||||||
|
|
||||||
static V get(T &x, K const &i)
|
static PythonConversion::ContextualWrapper<Vr> get(wrapped_map &x, std::string const &i)
|
||||||
{
|
{
|
||||||
if (x.find(i) != x.end())
|
K k = PythonConversion::string_converter<K>().from_str(x.ctx, i);
|
||||||
return x.at(i).get();
|
if (x.base.find(k) != x.base.end())
|
||||||
|
return PythonConversion::ContextualWrapper<Vr>(x.ctx, *x.base.at(k).get());
|
||||||
KeyError();
|
KeyError();
|
||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(T &x, K const &i, V const &v) { x[i] = typename T::mapped_type(v); }
|
static void set(wrapped_map &x, std::string const &i, V const &v)
|
||||||
|
|
||||||
static void del(T const &x, K const &i)
|
|
||||||
{
|
{
|
||||||
if (x.find(i) != x.end())
|
x.base[PythonConversion::string_converter<K>().from_str(x.ctx, i)] = typename T::mapped_type(v);
|
||||||
x.erase(i);
|
}
|
||||||
|
|
||||||
|
static void del(T const &x, std::string const &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
|
else
|
||||||
KeyError();
|
KeyError();
|
||||||
std::terminate();
|
std::terminate();
|
||||||
@ -372,12 +390,12 @@ template <typename T> struct map_wrapper_uptr
|
|||||||
static void wrap(const char *map_name, const char *kv_name, const char *kv_iter_name, const char *iter_name)
|
static void wrap(const char *map_name, const char *kv_name, const char *kv_iter_name, const char *iter_name)
|
||||||
{
|
{
|
||||||
map_pair_wrapper_uptr<typename KV::first_type, typename KV::second_type>::wrap(kv_name, kv_iter_name);
|
map_pair_wrapper_uptr<typename KV::first_type, typename KV::second_type>::wrap(kv_name, kv_iter_name);
|
||||||
typedef range_wrapper<T, return_internal_reference<>> rw;
|
typedef range_wrapper<T, return_value_policy<return_by_value>, PythonConversion::wrap_context<KV &>> rw;
|
||||||
typename rw::iter_wrap().wrap(iter_name);
|
typename rw::iter_wrap().wrap(iter_name);
|
||||||
class_<T, boost::noncopyable>(map_name, no_init)
|
class_<wrapped_map, boost::noncopyable>(map_name, no_init)
|
||||||
.def("__iter__", rw::iter)
|
.def("__iter__", rw::iter)
|
||||||
.def("__len__", &T::size)
|
.def("__len__", &T::size)
|
||||||
.def("__getitem__", get, return_internal_reference<>())
|
.def("__getitem__", get)
|
||||||
.def("__setitem__", set, with_custodian_and_ward<1, 2>());
|
.def("__setitem__", set, with_custodian_and_ward<1, 2>());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -150,6 +150,25 @@ template <typename Class, typename FuncT, FuncT fn, typename rv_conv, typename a
|
|||||||
template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
|
template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name) { cls_.def(name, wrapped_fn); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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 conv_val_type wrapped_getter(class_type &cls)
|
||||||
|
{
|
||||||
|
Context *ctx = get_ctx<Class>(cls);
|
||||||
|
Class &base = get_base<Class>(cls);
|
||||||
|
return v_conv()(ctx, (base.*mem));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename WrapCls> static void def_wrap(WrapCls cls_, const char *name)
|
||||||
|
{
|
||||||
|
cls_.add_property(name, wrapped_getter);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace PythonConversion
|
} // namespace PythonConversion
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_END
|
NEXTPNR_NAMESPACE_END
|
||||||
|
@ -95,13 +95,28 @@ void arch_wrap_python()
|
|||||||
conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelType");
|
conv_from_str<BelId>>::def_wrap(ctx_cls, "getBelType");
|
||||||
fn_wrapper_1a<Context, typeof(&Context::checkBelAvail), &Context::checkBelAvail, pass_through<bool>,
|
fn_wrapper_1a<Context, typeof(&Context::checkBelAvail), &Context::checkBelAvail, pass_through<bool>,
|
||||||
conv_from_str<BelId>>::def_wrap(ctx_cls, "checkBelAvail");
|
conv_from_str<BelId>>::def_wrap(ctx_cls, "checkBelAvail");
|
||||||
|
|
||||||
fn_wrapper_0a<Context, typeof(&Context::getBels), &Context::getBels, wrap_context<BelRange>>::def_wrap(ctx_cls,
|
fn_wrapper_0a<Context, typeof(&Context::getBels), &Context::getBels, wrap_context<BelRange>>::def_wrap(ctx_cls,
|
||||||
"getBels");
|
"getBels");
|
||||||
fn_wrapper_0a<Context, typeof(&Context::getWires), &Context::getWires, wrap_context<WireRange>>::def_wrap(
|
fn_wrapper_0a<Context, typeof(&Context::getWires), &Context::getWires, wrap_context<WireRange>>::def_wrap(
|
||||||
ctx_cls, "getWires");
|
ctx_cls, "getWires");
|
||||||
|
fn_wrapper_0a<Context, typeof(&Context::getPips), &Context::getPips, wrap_context<AllPipRange>>::def_wrap(
|
||||||
|
ctx_cls, "getPips");
|
||||||
|
|
||||||
fn_wrapper_1a<Context, typeof(&Context::getPipsDownhill), &Context::getPipsDownhill, wrap_context<PipRange>,
|
fn_wrapper_1a<Context, typeof(&Context::getPipsDownhill), &Context::getPipsDownhill, wrap_context<PipRange>,
|
||||||
conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsDownhill");
|
conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsDownhill");
|
||||||
|
fn_wrapper_1a<Context, typeof(&Context::getPipsUphill), &Context::getPipsUphill, wrap_context<PipRange>,
|
||||||
|
conv_from_str<WireId>>::def_wrap(ctx_cls, "getPipsUphill");
|
||||||
|
|
||||||
|
fn_wrapper_1a<Context, typeof(&Context::getPipSrcWire), &Context::getPipSrcWire, conv_to_str<WireId>,
|
||||||
|
conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipSrcWire");
|
||||||
|
fn_wrapper_1a<Context, typeof(&Context::getPipDstWire), &Context::getPipDstWire, conv_to_str<WireId>,
|
||||||
|
conv_from_str<PipId>>::def_wrap(ctx_cls, "getPipDstWire");
|
||||||
|
|
||||||
|
typedef std::unordered_map<IdString, std::unique_ptr<CellInfo>> CellMap;
|
||||||
|
|
||||||
|
readonly_wrapper<Context, typeof(&Context::cells), &Context::cells, wrap_context<CellMap &>>::def_wrap(ctx_cls,
|
||||||
|
"cells");
|
||||||
/*
|
/*
|
||||||
.def("getBelByName", &Arch::getBelByName)
|
.def("getBelByName", &Arch::getBelByName)
|
||||||
.def("getWireByName", &Arch::getWireByName)
|
.def("getWireByName", &Arch::getWireByName)
|
||||||
@ -130,6 +145,7 @@ void arch_wrap_python()
|
|||||||
WRAP_RANGE(AllPip, conv_to_str<PipId>);
|
WRAP_RANGE(AllPip, conv_to_str<PipId>);
|
||||||
WRAP_RANGE(Pip, conv_to_str<PipId>);
|
WRAP_RANGE(Pip, conv_to_str<PipId>);
|
||||||
|
|
||||||
|
WRAP_MAP_UPTR(CellMap, "IdCellMap");
|
||||||
// WRAP_RANGE(BelPin);
|
// WRAP_RANGE(BelPin);
|
||||||
// WRAP_RANGE(Wire);
|
// WRAP_RANGE(Wire);
|
||||||
// WRAP_RANGE(AllPip);
|
// WRAP_RANGE(AllPip);
|
||||||
|
Loading…
Reference in New Issue
Block a user