Skip to content
Snippets Groups Projects
Commit 51de65c9 authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

[std-map] add more general visitor for map types

+ test boost::unordered_map
+ pull StdMapPythonVisitor out of eigenpy::python namespace (shouldn't exist, add using-decl for backwards compatibility)
parent 9537d8f6
No related branches found
No related tags found
No related merge requests found
......@@ -70,8 +70,6 @@ struct overload_base_get_item_for_std_map
// Rohan Budhiraja.
///////////////////////////////////////////////////////////////////////////////
namespace python {
namespace bp = boost::python;
/**
......@@ -162,23 +160,15 @@ struct dict_to_map {
};
/**
* @brief Expose an std::map from a type given as template argument.
* @brief Expose the map-like container, e.g. (std::map).
*
* @param[in] T Type to expose as std::map<T>.
* @param[in] Compare Type for the Compare in std::map<T,Compare,Allocator>.
* @param[in] Allocator Type for the Allocator in
* std::map<T,Compare,Allocator>.
* @param[in] Container Container to expose.
* @param[in] NoProxy When set to false, the elements will be copied when
* returned to Python.
*/
template <class Key, class T, class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >,
bool NoProxy = false>
struct StdMapPythonVisitor
: public bp::map_indexing_suite<
typename std::map<Key, T, Compare, Allocator>, NoProxy>,
public dict_to_map<std::map<Key, T, Compare, Allocator> > {
typedef std::map<Key, T, Compare, Allocator> Container;
template <class Container, bool NoProxy = false>
struct GenericMapVisitor : public bp::map_indexing_suite<Container, NoProxy>,
public dict_to_map<Container> {
typedef dict_to_map<Container> FromPythonDictConverter;
static void expose(const std::string& class_name,
......@@ -186,15 +176,34 @@ struct StdMapPythonVisitor
namespace bp = bp;
bp::class_<Container>(class_name.c_str(), doc_string.c_str())
.def(StdMapPythonVisitor())
.def(GenericMapVisitor())
.def("todict", &FromPythonDictConverter::todict, bp::arg("self"),
"Returns the std::map as a Python dictionary.")
"Returns the map type as a Python dictionary.")
.def_pickle(PickleMap<Container>());
// Register conversion
FromPythonDictConverter::register_converter();
}
};
/**
* @brief Expose an std::map from a type given as template argument.
*
* @param[in] T Type to expose as std::map<T>.
* @param[in] Compare Type for the Compare in std::map<T,Compare,Allocator>.
* @param[in] Allocator Type for the Allocator in
* std::map<T,Compare,Allocator>.
* @param[in] NoProxy When set to false, the elements will be copied when
* returned to Python.
*/
template <class Key, class T, class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >,
bool NoProxy = false>
struct StdMapPythonVisitor
: GenericMapVisitor<std::map<Key, T, Compare, Allocator>, NoProxy> {};
namespace python {
// fix previous mistake
using ::eigenpy::StdMapPythonVisitor;
} // namespace python
} // namespace eigenpy
......
......
from std_map import copy, std_map_to_dict
from std_map import copy, copy_boost, std_map_to_dict
t = {"one": 1.0, "two": 2.0}
t2 = {"one": 1, "two": 2, "three": 3}
assert std_map_to_dict(t) == t
assert std_map_to_dict(copy(t)) == t
m = copy_boost(t2)
assert m.todict() == t2
......@@ -3,7 +3,7 @@
#include <eigenpy/eigenpy.hpp>
#include <eigenpy/std-map.hpp>
#include <iostream>
#include <boost/unordered_map.hpp>
namespace bp = boost::python;
......@@ -22,6 +22,12 @@ std::map<std::string, T1> copy(const std::map<std::string, T1>& map) {
return out;
}
template <typename T1>
boost::unordered_map<std::string, T1> copy_boost(
const boost::unordered_map<std::string, T1>& obj) {
return obj;
}
BOOST_PYTHON_MODULE(std_map) {
eigenpy::enableEigenPy();
......@@ -30,6 +36,10 @@ BOOST_PYTHON_MODULE(std_map) {
std::allocator<std::pair<const std::string, double> >,
true>::expose("StdMap_Double");
eigenpy::GenericMapVisitor<boost::unordered_map<std::string, int> >::expose(
"boost_map_int");
bp::def("std_map_to_dict", std_map_to_dict<double>);
bp::def("copy", copy<double>);
bp::def("copy_boost", copy_boost<int>);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment