knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::py::dict_map_converter< KeyT, ValT, Map > Struct Template Reference

#include <dict.h>

Static Public Member Functions

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)
 
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)
 
static PyObject * convert (const Map &m)
 
static const PyTypeObject * get_pytype ()
 

Detailed Description

template<typename KeyT, typename ValT, typename Map = std::map<KeyT, ValT>>
struct knowrob::py::dict_map_converter< KeyT, ValT, Map >

Definition at line 67 of file dict.h.

Member Function Documentation

◆ construct() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::construct ( PyObject *  obj,
boost::python::converter::rvalue_from_python_stage1_data *  data 
)
inlinestatic

Definition at line 100 of file dict.h.

101  {
102  void *storage = ((boost::python::converter::rvalue_from_python_storage <Map> *) data)->storage.bytes;
103  auto pmap = new(storage) Map();
104 
105  PyObject * key, *val;
106  Py_ssize_t pos = 0;
107  while (PyDict_Next(obj, &pos, &key, &val)) {
108  (*pmap)[boost::python::extract<KeyT>(key)] = boost::python::extract<ValT>(val);
109  }
110  data->convertible = storage;
111  }

◆ construct() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::construct ( PyObject *  obj,
boost::python::converter::rvalue_from_python_stage1_data *  data 
)
inlinestatic

Definition at line 100 of file dict.h.

101  {
102  void *storage = ((boost::python::converter::rvalue_from_python_storage <Map> *) data)->storage.bytes;
103  auto pmap = new(storage) Map();
104 
105  PyObject * key, *val;
106  Py_ssize_t pos = 0;
107  while (PyDict_Next(obj, &pos, &key, &val)) {
108  (*pmap)[boost::python::extract<KeyT>(key)] = boost::python::extract<ValT>(val);
109  }
110  data->convertible = storage;
111  }

◆ convert() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static PyObject* knowrob::py::dict_map_converter< KeyT, ValT, Map >::convert ( const Map &  m)
inlinestatic

Definition at line 113 of file dict.h.

113  {
114  PyObject * obj = PyDict_New();
115  // memory should not leak
116  // but seeking for better approach.
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;
120 
121  for (auto p: m) {
122  // for C++ 11 support
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());
126  }
127  free(buffer);
128  return obj;
129  }

◆ convert() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static PyObject* knowrob::py::dict_map_converter< KeyT, ValT, Map >::convert ( const Map &  m)
inlinestatic

Definition at line 113 of file dict.h.

113  {
114  PyObject * obj = PyDict_New();
115  // memory should not leak
116  // but seeking for better approach.
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;
120 
121  for (auto p: m) {
122  // for C++ 11 support
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());
126  }
127  free(buffer);
128  return obj;
129  }

◆ convertible() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void* knowrob::py::dict_map_converter< KeyT, ValT, Map >::convertible ( PyObject *  obj)
inlinestatic

Definition at line 84 of file dict.h.

84  {
85  if (!PyDict_Check(obj))
86  return nullptr;
87 
88  PyObject * key, *val;
89  Py_ssize_t pos = 0;
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()) {
94  return nullptr;
95  }
96  }
97  return obj;
98  }

◆ convertible() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void* knowrob::py::dict_map_converter< KeyT, ValT, Map >::convertible ( PyObject *  obj)
inlinestatic

Definition at line 84 of file dict.h.

84  {
85  if (!PyDict_Check(obj))
86  return nullptr;
87 
88  PyObject * key, *val;
89  Py_ssize_t pos = 0;
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()) {
94  return nullptr;
95  }
96  }
97  return obj;
98  }

◆ get_pytype() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static const PyTypeObject* knowrob::py::dict_map_converter< KeyT, ValT, Map >::get_pytype ( )
inlinestatic

Definition at line 131 of file dict.h.

131  {
132  return &PyDict_Type;
133  }

◆ get_pytype() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static const PyTypeObject* knowrob::py::dict_map_converter< KeyT, ValT, Map >::get_pytype ( )
inlinestatic

Definition at line 131 of file dict.h.

131  {
132  return &PyDict_Type;
133  }

◆ register_bidirectional_converter() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_bidirectional_converter ( )
inlinestatic

Definition at line 79 of file dict.h.

79  {
82  }
static void register_from_python_converter()
Definition: dict.h:68
static void register_to_python_converter()
Definition: dict.h:75

◆ register_bidirectional_converter() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_bidirectional_converter ( )
inlinestatic

Definition at line 79 of file dict.h.

◆ register_from_python_converter() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_from_python_converter ( )
inlinestatic

Definition at line 68 of file dict.h.

68  {
69  boost::python::converter::registry::push_back(
72  boost::python::type_id<Map>());
73  }
static void * convertible(PyObject *obj)
Definition: dict.h:84
static void construct(PyObject *obj, boost::python::converter::rvalue_from_python_stage1_data *data)
Definition: dict.h:100

◆ register_from_python_converter() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_from_python_converter ( )
inlinestatic

Definition at line 68 of file dict.h.

68  {
69  boost::python::converter::registry::push_back(
72  boost::python::type_id<Map>());
73  }

◆ register_to_python_converter() [1/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_to_python_converter ( )
inlinestatic

Definition at line 75 of file dict.h.

75  {
76  boost::python::to_python_converter<Map, dict_map_converter<KeyT, ValT, Map>, true>();
77  }

◆ register_to_python_converter() [2/2]

template<typename KeyT , typename ValT , typename Map = std::map<KeyT, ValT>>
static void knowrob::py::dict_map_converter< KeyT, ValT, Map >::register_to_python_converter ( )
inlinestatic

Definition at line 75 of file dict.h.

75  {
76  boost::python::to_python_converter<Map, dict_map_converter<KeyT, ValT, Map>, true>();
77  }

The documentation for this struct was generated from the following file: