6 #ifndef __eigenpy_map_hpp__
7 #define __eigenpy_map_hpp__
13 #include <boost/python/suite/indexing/map_indexing_suite.hpp>
14 #include <boost/python/stl_iterator.hpp>
15 #include <boost/python/to_python_converter.hpp>
23 template <
typename Container>
25 :
public boost::python::def_visitor<
26 overload_base_get_item_for_map<Container>> {
28 typedef typename Container::value_type::second_type
data_type;
32 template <
class Class>
39 boost::python::back_reference<Container&> container, PyObject* i_) {
41 typename Container::iterator i = container.get().find(idx);
42 if (i == container.get().end()) {
43 PyErr_SetString(PyExc_KeyError,
"Invalid key");
44 boost::python::throw_error_already_set();
47 typename boost::python::to_python_indirect<
48 data_type&, boost::python::detail::make_reference_holder>
50 return boost::python::object(boost::python::handle<>(convert(i->second)));
54 boost::python::extract<key_type const&> i(i_);
58 boost::python::extract<key_type> i(i_);
59 if (i.check())
return i();
62 PyErr_SetString(PyExc_TypeError,
"Invalid index type");
63 boost::python::throw_error_already_set();
83 template <
typename Container>
85 static void setstate(bp::object op, bp::tuple tup) {
86 Container& o = bp::extract<Container&>(op)();
87 bp::stl_input_iterator<typename Container::value_type> begin(tup[0]), end;
95 template <
typename Container>
100 bp::type_id<Container>());
106 if (!PyObject_GetIter(
object))
return 0;
112 bp::converter::rvalue_from_python_stage1_data*
data) {
114 bp::handle<> handle(bp::borrowed(
object));
115 bp::dict dict(handle);
119 typedef bp::converter::rvalue_from_python_storage<Container> storage_type;
120 void* storage =
reinterpret_cast<storage_type*
>(
data)->storage.bytes;
123 new (storage) Container();
126 Container& map(*(
static_cast<Container*
>(storage)));
127 bp::list keys(dict.keys());
128 int keycount(
static_cast<int>(bp::len(keys)));
129 for (
int i = 0; i < keycount; ++i) {
131 bp::object keyobj(keys[i]);
132 bp::extract<typename Container::key_type> keyproxy(keyobj);
133 if (!keyproxy.check()) {
134 PyErr_SetString(PyExc_KeyError,
"Bad key type");
135 bp::throw_error_already_set();
137 typename Container::key_type key = keyproxy();
140 bp::object valobj(dict[keyobj]);
141 bp::extract<typename Container::mapped_type> valproxy(valobj);
142 if (!valproxy.check()) {
143 PyErr_SetString(PyExc_ValueError,
"Bad value type");
144 bp::throw_error_already_set();
146 typename Container::mapped_type
val = valproxy();
147 map.emplace(key,
val);
151 data->convertible = storage;
154 static bp::dict
todict(Container&
self) {
156 typename Container::const_iterator it;
157 for (it =
self.begin(); it !=
self.end(); ++it) {
158 dict.setdefault(it->first, it->second);
166 template <
class Container,
bool NoProxy>
168 : bp::map_indexing_suite<Container, NoProxy,
169 emplace_set_derived_policies<Container, NoProxy>> {
171 typedef typename Container::value_type::second_type
data_type;
174 bp::detail::final_map_derived_policies<Container, NoProxy>;
176 template <
class Class>
179 std::string elem_name =
"map_indexing_suite_";
180 bp::object class_name(cl.attr(
"__name__"));
181 bp::extract<std::string> class_name_extractor(class_name);
182 elem_name += class_name_extractor();
183 elem_name +=
"_entry";
184 namespace mpl = boost::mpl;
186 typedef typename mpl::if_<
187 mpl::and_<boost::is_class<data_type>, mpl::bool_<!NoProxy>>,
188 bp::return_internal_reference<>, bp::default_call_policies>::type
189 get_data_return_policy;
191 bp::class_<value_type>(elem_name.c_str(), bp::no_init)
192 .def(
"__repr__", &DerivedPolicies::print_elem)
193 .def(
"data", &DerivedPolicies::get_data, get_data_return_policy())
194 .def(
"key", &DerivedPolicies::get_key);
198 container.emplace(i,
v);
209 template <
class Container,
bool NoProxy = false>
215 template <
typename DerivedVisitor>
216 static void expose(
const std::string& class_name,
217 const std::string& doc_string,
218 const bp::def_visitor<DerivedVisitor>& visitor) {
221 if (!register_symbolic_link_to_registered_type<Container>()) {
222 bp::class_<Container>(class_name.c_str(), doc_string.c_str())
225 "Returns the map type as a Python dictionary.")
233 static void expose(
const std::string& class_name,
234 const std::string& doc_string =
"") {
238 template <
typename DerivedVisitor>
239 static void expose(
const std::string& class_name,
240 const bp::def_visitor<DerivedVisitor>& visitor) {
241 expose(class_name,
"", visitor);
247 #endif // ifndef __eigenpy_map_hpp__