2 #ifndef KNOWROB_PY_CONVERTER_PYTHON_TO_MAP_H
3 #define KNOWROB_PY_CONVERTER_PYTHON_TO_MAP_H
5 #include <boost/python.hpp>
6 #include <unordered_map>
7 #include <boost/any.hpp>
16 if (extract<int>(obj).check()) {
17 return boost::any(extract<int>(obj)());
18 }
else if (extract<double>(obj).check()) {
19 return boost::any(extract<double>(obj)());
20 }
else if (extract<std::string>(obj).check()) {
21 return boost::any(extract<std::string>(obj)());
24 PyErr_SetString(PyExc_TypeError,
"Unsupported type in Python object");
25 throw_error_already_set();
32 std::unordered_map<std::string, boost::any>
dict_to_map(
const boost::python::dict &py_dict) {
33 std::unordered_map<std::string, boost::any> map;
34 boost::python::list keys = py_dict.keys();
35 for (
int i = 0; i < len(keys); ++i) {
36 std::string key = boost::python::extract<std::string>(keys[i]);
37 boost::python::object value = py_dict[keys[i]];
44 struct DictToMapConverter {
46 if (!PyDict_Check(obj_ptr))
return 0;
50 static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data) {
51 void *storage = ((boost::python::converter::rvalue_from_python_storage<std::unordered_map<std::string, boost::any>> *) data)->storage.bytes;
52 new(storage) std::unordered_map<std::string, boost::any>(
53 dict_to_map(boost::python::dict(boost::python::borrowed(obj_ptr))));
54 data->convertible = storage;
60 boost::python::converter::registry::push_back(
63 boost::python::type_id<std::unordered_map<std::string, boost::any>>());
66 template<
typename KeyT,
typename ValT,
typename Map = std::map<KeyT, ValT>>
67 struct dict_map_converter {
69 boost::python::converter::registry::push_back(
72 boost::python::type_id<Map>());
76 boost::python::to_python_converter<Map, dict_map_converter<KeyT, ValT, Map>,
true>();
85 if (!PyDict_Check(obj))
90 while (PyDict_Next(obj, &pos, &key, &val)) {
91 boost::python::extract <KeyT> key_e(key);
92 boost::python::extract <ValT> val_e(val);
93 if (!key_e.check() || !val_e.check()) {
101 boost::python::converter::rvalue_from_python_stage1_data *data) {
102 void *storage = ((boost::python::converter::rvalue_from_python_storage <Map> *) data)->storage.bytes;
103 auto pmap =
new(storage) Map();
105 PyObject * key, *val;
107 while (PyDict_Next(obj, &pos, &key, &val)) {
108 (*pmap)[boost::python::extract<KeyT>(key)] = boost::python::extract<ValT>(val);
110 data->convertible = storage;
114 PyObject * obj = PyDict_New();
117 boost::python::object *buffer = (boost::python::object *) malloc(2 *
sizeof(boost::python::object));
118 boost::python::object *buffer_k = buffer + 0;
119 boost::python::object *buffer_v = buffer + 1;
123 boost::python::object *kobj =
new(buffer_k) boost::python::object(p.first);
124 boost::python::object *vobj =
new(buffer_v) boost::python::object(p.second);
125 PyDict_SetItem(obj, kobj->ptr(), vobj->ptr());
void register_dict_to_map_converter()
std::unordered_map< std::string, boost::any > dict_to_map(const boost::python::dict &py_dict)
boost::any python_to_boost_any(const boost::python::object &obj)
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
static void * convertible(PyObject *obj_ptr)
static PyObject * convert(const Map &m)
static const PyTypeObject * get_pytype()
static void register_from_python_converter()
static void register_to_python_converter()
static void register_bidirectional_converter()
static void * convertible(PyObject *obj)
static void construct(PyObject *obj, boost::python::converter::rvalue_from_python_stage1_data *data)