Fixing Python bindings after adding unique_ptr
Signed-off-by: David Shah <davey1576@gmail.com>
This commit is contained in:
parent
ded9df61dc
commit
103dde79de
@ -19,12 +19,12 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <memory>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#ifndef NEXTPNR_H
|
#ifndef NEXTPNR_H
|
||||||
#define NEXTPNR_H
|
#define NEXTPNR_H
|
||||||
|
@ -26,8 +26,8 @@
|
|||||||
#include "nextpnr.h"
|
#include "nextpnr.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <memory>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_BEGIN
|
NEXTPNR_NAMESPACE_BEGIN
|
||||||
|
|
||||||
// Required to determine concatenated module name (which differs for different
|
// Required to determine concatenated module name (which differs for different
|
||||||
@ -55,10 +55,10 @@ void parse_json_shim(std::string filename, Context &d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Chip and load design from json file
|
// Create a new Chip and load design from json file
|
||||||
Context load_design_shim(std::string filename, ArchArgs args)
|
Context *load_design_shim(std::string filename, ArchArgs args)
|
||||||
{
|
{
|
||||||
Context d(args);
|
Context *d = new Context(args);
|
||||||
parse_json_shim(filename, d);
|
parse_json_shim(filename, *d);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ BOOST_PYTHON_MODULE(MODULE_NAME)
|
|||||||
|
|
||||||
class_<PortRef>("PortRef").def_readwrite("cell", &PortRef::cell).def_readwrite("port", &PortRef::port);
|
class_<PortRef>("PortRef").def_readwrite("cell", &PortRef::cell).def_readwrite("port", &PortRef::port);
|
||||||
|
|
||||||
class_<NetInfo, NetInfo *>("NetInfo")
|
class_<NetInfo, NetInfo *, boost::noncopyable>("NetInfo")
|
||||||
.def_readwrite("name", &NetInfo::name)
|
.def_readwrite("name", &NetInfo::name)
|
||||||
.def_readwrite("driver", &NetInfo::driver)
|
.def_readwrite("driver", &NetInfo::driver)
|
||||||
.def_readwrite("users", &NetInfo::users)
|
.def_readwrite("users", &NetInfo::users)
|
||||||
@ -96,7 +96,7 @@ 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")
|
class_<CellInfo, CellInfo *, boost::noncopyable>("CellInfo")
|
||||||
.def_readwrite("name", &CellInfo::name)
|
.def_readwrite("name", &CellInfo::name)
|
||||||
.def_readwrite("type", &CellInfo::type)
|
.def_readwrite("type", &CellInfo::type)
|
||||||
.def_readwrite("ports", &CellInfo::ports)
|
.def_readwrite("ports", &CellInfo::ports)
|
||||||
@ -108,15 +108,15 @@ 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 *>("BaseCtx", no_init)
|
class_<BaseCtx, BaseCtx *, boost::noncopyable>("BaseCtx", no_init)
|
||||||
.def_readwrite("nets", &Context::nets)
|
.add_property("nets", make_getter(&Context::nets, return_internal_reference<>()))
|
||||||
.def_readwrite("cells", &Context::cells);
|
.add_property("cells", make_getter(&Context::nets, return_internal_reference<>()));
|
||||||
|
|
||||||
WRAP_MAP(decltype(Context::nets), "IdNetMap");
|
WRAP_MAP_UPTR(decltype(Context::nets), "IdNetMap");
|
||||||
WRAP_MAP(decltype(Context::cells), "IdCellMap");
|
WRAP_MAP_UPTR(decltype(Context::cells), "IdCellMap");
|
||||||
|
|
||||||
def("parse_json", parse_json_shim);
|
def("parse_json", parse_json_shim);
|
||||||
def("load_design", load_design_shim);
|
def("load_design", load_design_shim, return_value_policy<manage_new_object>());
|
||||||
|
|
||||||
class_<IdString>("IdString")
|
class_<IdString>("IdString")
|
||||||
.def("__str__", &IdString::global_str, return_value_policy<copy_const_reference>())
|
.def("__str__", &IdString::global_str, return_value_policy<copy_const_reference>())
|
||||||
@ -124,7 +124,7 @@ BOOST_PYTHON_MODULE(MODULE_NAME)
|
|||||||
.def(self == self);
|
.def(self == self);
|
||||||
arch_wrap_python();
|
arch_wrap_python();
|
||||||
|
|
||||||
class_<Context, Context *, bases<Arch>>("Context", no_init).def("checksum", &Context::checksum);
|
class_<Context, Context *, bases<Arch>, boost::noncopyable>("Context", no_init).def("checksum", &Context::checksum);
|
||||||
}
|
}
|
||||||
|
|
||||||
void arch_appendinittab() { PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); }
|
void arch_appendinittab() { PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); }
|
||||||
|
@ -228,7 +228,7 @@ template <typename T> struct map_wrapper
|
|||||||
std::terminate();
|
std::terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set(T &x, K const &i, V const &v) { x[i] = v; }
|
static void set(T &x, K const &i, V &v) { x[i] = v; }
|
||||||
|
|
||||||
static void del(T const &x, K const &i)
|
static void del(T const &x, K const &i)
|
||||||
{
|
{
|
||||||
@ -252,7 +252,108 @@ template <typename T> struct map_wrapper
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Special case of above for map key/values where value is a unique_ptr
|
||||||
|
*/
|
||||||
|
template <typename T1, typename T2> struct map_pair_wrapper_uptr
|
||||||
|
{
|
||||||
|
typedef std::pair<T1, T2> T;
|
||||||
|
typedef typename T::second_type::element_type V;
|
||||||
|
struct pair_iterator_wrapper
|
||||||
|
{
|
||||||
|
static object next(std::pair<T &, int> &iter)
|
||||||
|
{
|
||||||
|
if (iter.second == 0) {
|
||||||
|
iter.second++;
|
||||||
|
return object(iter.first.first);
|
||||||
|
} else if (iter.second == 1) {
|
||||||
|
iter.second++;
|
||||||
|
return object(iter.first.second.get());
|
||||||
|
} else {
|
||||||
|
PyErr_SetString(PyExc_StopIteration, "End of range reached");
|
||||||
|
boost::python::throw_error_already_set();
|
||||||
|
// Should be unreachable, but prevent control may reach end of
|
||||||
|
// non-void
|
||||||
|
throw std::runtime_error("unreachable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wrap(const char *python_name)
|
||||||
|
{
|
||||||
|
class_<std::pair<T &, int>>(python_name, no_init).def("__next__", next);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static object get(T &x, int i)
|
||||||
|
{
|
||||||
|
if ((i >= 2) || (i < 0))
|
||||||
|
KeyError();
|
||||||
|
return (i == 1) ? object(x.second.get()) : object(x.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int len(T &x) { return 2; }
|
||||||
|
|
||||||
|
static std::pair<T &, int> iter(T &x) { return std::make_pair(boost::ref(x), 0); };
|
||||||
|
|
||||||
|
static V &second_getter(T &t) { return *t.second.get(); }
|
||||||
|
|
||||||
|
static void wrap(const char *pair_name, const char *iter_name)
|
||||||
|
{
|
||||||
|
pair_iterator_wrapper::wrap(iter_name);
|
||||||
|
class_<T, boost::noncopyable>(pair_name, no_init)
|
||||||
|
.def("__iter__", iter)
|
||||||
|
.def("__len__", len)
|
||||||
|
.def("__getitem__", get)
|
||||||
|
.def_readonly("first", &T::first)
|
||||||
|
.add_property("second", make_function(second_getter, return_internal_reference<>()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Wrapper for a map, either an unordered_map, regular map or dict
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 T::mapped_type::pointer V;
|
||||||
|
typedef typename T::value_type KV;
|
||||||
|
|
||||||
|
static V get(T &x, K const &i)
|
||||||
|
{
|
||||||
|
if (x.find(i) != x.end())
|
||||||
|
return x.at(i).get();
|
||||||
|
KeyError();
|
||||||
|
std::terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void set(T &x, K const &i, V const &v) { x[i] = typename T::mapped_type(v); }
|
||||||
|
|
||||||
|
static void del(T const &x, K const &i)
|
||||||
|
{
|
||||||
|
if (x.find(i) != x.end())
|
||||||
|
x.erase(i);
|
||||||
|
else
|
||||||
|
KeyError();
|
||||||
|
std::terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
typedef range_wrapper<T, return_value_policy<copy_non_const_reference>> rw;
|
||||||
|
typename rw::iter_wrap().wrap(iter_name);
|
||||||
|
class_<T, boost::noncopyable>(map_name, no_init)
|
||||||
|
.def("__iter__", rw::iter)
|
||||||
|
.def("__len__", &T::size)
|
||||||
|
.def("__getitem__", get, return_internal_reference<>())
|
||||||
|
.def("__setitem__", set, with_custodian_and_ward<1, 2>());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#define WRAP_MAP(t, name) map_wrapper<t>().wrap(#name, #name "KeyValue", #name "KeyValueIter", #name "Iterator")
|
#define WRAP_MAP(t, name) map_wrapper<t>().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")
|
||||||
|
|
||||||
NEXTPNR_NAMESPACE_END
|
NEXTPNR_NAMESPACE_END
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ void arch_wrap_python()
|
|||||||
;
|
;
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
class_<Arch, Arch *, bases<BaseCtx>>("Arch", init<ArchArgs>())
|
class_<Arch, Arch *, bases<BaseCtx>, boost::noncopyable>("Arch", init<ArchArgs>())
|
||||||
.def("getBelByName", &Arch::getBelByName)
|
.def("getBelByName", &Arch::getBelByName)
|
||||||
.def("getWireByName", &Arch::getWireByName)
|
.def("getWireByName", &Arch::getWireByName)
|
||||||
.def("getBelName", &Arch::getBelName)
|
.def("getBelName", &Arch::getBelName)
|
||||||
|
Loading…
Reference in New Issue
Block a user