python: Update wrapper for non-unique_ptr maps
Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
parent
4376ae43af
commit
79e91368f9
@ -21,6 +21,7 @@
|
|||||||
#ifndef NO_PYTHON
|
#ifndef NO_PYTHON
|
||||||
|
|
||||||
#include "pybindings.h"
|
#include "pybindings.h"
|
||||||
|
#include "arch_pybindings.h"
|
||||||
#include "jsonparse.h"
|
#include "jsonparse.h"
|
||||||
#include "nextpnr.h"
|
#include "nextpnr.h"
|
||||||
|
|
||||||
@ -105,32 +106,20 @@ BOOST_PYTHON_MODULE(MODULE_NAME)
|
|||||||
.def_readwrite("net", &PortInfo::net)
|
.def_readwrite("net", &PortInfo::net)
|
||||||
.def_readwrite("type", &PortInfo::type);
|
.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);
|
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");
|
typedef std::unordered_map<IdString, std::string> AttrMap;
|
||||||
// WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap");
|
|
||||||
|
|
||||||
auto ci_cls = class_<ContextualWrapper<CellInfo &>>("CellInfo", no_init);
|
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,
|
readonly_wrapper<CellInfo &, typeof(&CellInfo::type), &CellInfo::type, conv_to_str<IdString>>::def_wrap(ci_cls,
|
||||||
"type");
|
"type");
|
||||||
|
readonly_wrapper<CellInfo &, typeof(&CellInfo::attrs), &CellInfo::attrs, wrap_context<AttrMap &>>::def_wrap(
|
||||||
|
ci_cls, "attrs");
|
||||||
def("parse_json", parse_json_shim);
|
def("parse_json", parse_json_shim);
|
||||||
def("load_design", load_design_shim, return_value_policy<manage_new_object>());
|
def("load_design", load_design_shim, return_value_policy<manage_new_object>());
|
||||||
|
|
||||||
|
WRAP_MAP(AttrMap, pass_through<std::string>, "AttrMap");
|
||||||
|
|
||||||
arch_wrap_python();
|
arch_wrap_python();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,20 +188,23 @@ template <typename T1, typename T2> struct pair_wrapper
|
|||||||
/*
|
/*
|
||||||
Special case of above for map key/values
|
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 std::pair<T1, T2> T;
|
||||||
|
typedef PythonConversion::ContextualWrapper<T &> wrapped_pair;
|
||||||
|
typedef typename T::second_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);
|
return object(value_conv()(iter.first.ctx, iter.first.base.second));
|
||||||
} 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();
|
||||||
@ -213,30 +216,37 @@ template <typename T1, typename T2> struct map_pair_wrapper
|
|||||||
|
|
||||||
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) : 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)
|
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>(pair_name, no_init)
|
class_<wrapped_pair>(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)
|
||||||
.def_readwrite("second", &T::second);
|
.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
|
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 std::remove_cv<typename std::remove_reference<typename T::key_type>::type>::type K;
|
||||||
typedef typename T::mapped_type V;
|
typedef typename T::mapped_type V;
|
||||||
|
typedef typename value_conv::ret_type wrapped_V;
|
||||||
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 wrapped_V 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);
|
if (x.base.find(k) != x.base.end())
|
||||||
|
return value_conv()(x.ctx, x.base.at(k));
|
||||||
KeyError();
|
KeyError();
|
||||||
std::terminate();
|
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)
|
||||||
|
|
||||||
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)] = 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();
|
||||||
@ -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)
|
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);
|
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<copy_non_const_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>(map_name, no_init)
|
class_<wrapped_map>(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>());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -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) \
|
#define WRAP_MAP_UPTR(t, name) \
|
||||||
map_wrapper_uptr<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator")
|
map_wrapper_uptr<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator")
|
||||||
|
|
||||||
|
70
ice40/arch_pybindings.h
Normal file
70
ice40/arch_pybindings.h
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* 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 ARCH_PYBINDINGS_H
|
||||||
|
#define ARCH_PYBINDINGS_H
|
||||||
|
#ifndef NO_PYTHON
|
||||||
|
|
||||||
|
#include "pybindings.h"
|
||||||
|
#include "nextpnr.h"
|
||||||
|
|
||||||
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
|
|
||||||
|
namespace PythonConversion {
|
||||||
|
|
||||||
|
template <> struct string_converter<BelId>
|
||||||
|
{
|
||||||
|
BelId from_str(Context *ctx, std::string name) { return ctx->getBelByName(ctx->id(name)); }
|
||||||
|
|
||||||
|
std::string to_str(Context *ctx, BelId id) { return ctx->getBelName(id).str(ctx); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <> struct string_converter<BelType>
|
||||||
|
{
|
||||||
|
BelType from_str(Context *ctx, std::string name) { return ctx->belTypeFromId(ctx->id(name)); }
|
||||||
|
|
||||||
|
std::string to_str(Context *ctx, BelType typ) { return ctx->belTypeToId(typ).str(ctx); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <> struct string_converter<WireId>
|
||||||
|
{
|
||||||
|
WireId from_str(Context *ctx, std::string name) { return ctx->getWireByName(ctx->id(name)); }
|
||||||
|
|
||||||
|
std::string to_str(Context *ctx, WireId id) { return ctx->getWireName(id).str(ctx); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <> struct string_converter<PipId>
|
||||||
|
{
|
||||||
|
PipId from_str(Context *ctx, std::string name) { return ctx->getPipByName(ctx->id(name)); }
|
||||||
|
|
||||||
|
std::string to_str(Context *ctx, PipId id) { return ctx->getPipName(id).str(ctx); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <> struct string_converter<PortPin>
|
||||||
|
{
|
||||||
|
PortPin from_str(Context *ctx, std::string name) { return ctx->portPinFromId(ctx->id(name)); }
|
||||||
|
|
||||||
|
std::string to_str(Context *ctx, PortPin id) { return ctx->portPinToId(id).str(ctx); }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace PythonConversion
|
||||||
|
|
||||||
|
NEXTPNR_NAMESPACE_END
|
||||||
|
#endif
|
||||||
|
#endif
|
@ -21,49 +21,11 @@
|
|||||||
#ifndef NO_PYTHON
|
#ifndef NO_PYTHON
|
||||||
|
|
||||||
#include "pybindings.h"
|
#include "pybindings.h"
|
||||||
|
#include "arch_pybindings.h"
|
||||||
#include "nextpnr.h"
|
#include "nextpnr.h"
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_BEGIN
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
|
|
||||||
namespace PythonConversion {
|
|
||||||
|
|
||||||
template <> struct string_converter<BelId>
|
|
||||||
{
|
|
||||||
BelId from_str(Context *ctx, std::string name) { return ctx->getBelByName(ctx->id(name)); }
|
|
||||||
|
|
||||||
std::string to_str(Context *ctx, BelId id) { return ctx->getBelName(id).str(ctx); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <> struct string_converter<BelType>
|
|
||||||
{
|
|
||||||
BelType from_str(Context *ctx, std::string name) { return ctx->belTypeFromId(ctx->id(name)); }
|
|
||||||
|
|
||||||
std::string to_str(Context *ctx, BelType typ) { return ctx->belTypeToId(typ).str(ctx); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <> struct string_converter<WireId>
|
|
||||||
{
|
|
||||||
WireId from_str(Context *ctx, std::string name) { return ctx->getWireByName(ctx->id(name)); }
|
|
||||||
|
|
||||||
std::string to_str(Context *ctx, WireId id) { return ctx->getWireName(id).str(ctx); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <> struct string_converter<PipId>
|
|
||||||
{
|
|
||||||
PipId from_str(Context *ctx, std::string name) { return ctx->getPipByName(ctx->id(name)); }
|
|
||||||
|
|
||||||
std::string to_str(Context *ctx, PipId id) { return ctx->getPipName(id).str(ctx); }
|
|
||||||
};
|
|
||||||
|
|
||||||
template <> struct string_converter<PortPin>
|
|
||||||
{
|
|
||||||
PortPin from_str(Context *ctx, std::string name) { return ctx->portPinFromId(ctx->id(name)); }
|
|
||||||
|
|
||||||
std::string to_str(Context *ctx, PortPin id) { return ctx->portPinToId(id).str(ctx); }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace PythonConversion
|
|
||||||
|
|
||||||
void arch_wrap_python()
|
void arch_wrap_python()
|
||||||
{
|
{
|
||||||
using namespace PythonConversion;
|
using namespace PythonConversion;
|
||||||
@ -134,8 +96,8 @@ void arch_wrap_python()
|
|||||||
conv_from_str<WireId>>::def_wrap(ctx_cls, "checkWireAvail");
|
conv_from_str<WireId>>::def_wrap(ctx_cls, "checkWireAvail");
|
||||||
fn_wrapper_1a<Context, typeof(&Context::getBoundWireNet), &Context::getBoundWireNet, conv_to_str<IdString>,
|
fn_wrapper_1a<Context, typeof(&Context::getBoundWireNet), &Context::getBoundWireNet, conv_to_str<IdString>,
|
||||||
conv_from_str<WireId>>::def_wrap(ctx_cls, "getBoundWireNet");
|
conv_from_str<WireId>>::def_wrap(ctx_cls, "getBoundWireNet");
|
||||||
fn_wrapper_1a<Context, typeof(&Context::getConflictingWireNet), &Context::getConflictingWireNet, conv_to_str<IdString>,
|
fn_wrapper_1a<Context, typeof(&Context::getConflictingWireNet), &Context::getConflictingWireNet,
|
||||||
conv_from_str<WireId>>::def_wrap(ctx_cls, "getConflictingWireNet");
|
conv_to_str<IdString>, conv_from_str<WireId>>::def_wrap(ctx_cls, "getConflictingWireNet");
|
||||||
|
|
||||||
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");
|
||||||
@ -152,8 +114,8 @@ void arch_wrap_python()
|
|||||||
conv_from_str<PipId>>::def_wrap(ctx_cls, "checkPipAvail");
|
conv_from_str<PipId>>::def_wrap(ctx_cls, "checkPipAvail");
|
||||||
fn_wrapper_1a<Context, typeof(&Context::getBoundPipNet), &Context::getBoundPipNet, conv_to_str<IdString>,
|
fn_wrapper_1a<Context, typeof(&Context::getBoundPipNet), &Context::getBoundPipNet, conv_to_str<IdString>,
|
||||||
conv_from_str<PipId>>::def_wrap(ctx_cls, "getBoundPipNet");
|
conv_from_str<PipId>>::def_wrap(ctx_cls, "getBoundPipNet");
|
||||||
fn_wrapper_1a<Context, typeof(&Context::getConflictingPipNet), &Context::getConflictingPipNet, conv_to_str<IdString>,
|
fn_wrapper_1a<Context, typeof(&Context::getConflictingPipNet), &Context::getConflictingPipNet,
|
||||||
conv_from_str<PipId>>::def_wrap(ctx_cls, "getConflictingPipNet");
|
conv_to_str<IdString>, conv_from_str<PipId>>::def_wrap(ctx_cls, "getConflictingPipNet");
|
||||||
|
|
||||||
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");
|
||||||
@ -193,7 +155,6 @@ void arch_wrap_python()
|
|||||||
|
|
||||||
WRAP_MAP_UPTR(CellMap, "IdCellMap");
|
WRAP_MAP_UPTR(CellMap, "IdCellMap");
|
||||||
WRAP_MAP_UPTR(NetMap, "IdNetMap");
|
WRAP_MAP_UPTR(NetMap, "IdNetMap");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_END
|
NEXTPNR_NAMESPACE_END
|
||||||
|
Loading…
Reference in New Issue
Block a user