knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
string_view.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_PY_CONVERTER_STRING_VIEW_H
7 #define KNOWROB_PY_CONVERTER_STRING_VIEW_H
8 
9 namespace knowrob::py {
10  // This is needed for python to work with std::string_view.
11  struct string_view_to_python_str {
12  static PyObject *convert(std::string_view s) {
13  return PyUnicode_FromStringAndSize(s.data(), s.size());
14  }
15  };
16 
17  // This is needed to map Python strings into std::string_view.
18  struct python_str_to_string_view {
19  static void *convertible(PyObject *obj_ptr) {
20  if (!PyUnicode_Check(obj_ptr)) return 0;
21  return obj_ptr;
22  }
23 
24  static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data) {
25  const char *value = PyUnicode_AsUTF8(obj_ptr);
26  if (value == nullptr) boost::python::throw_error_already_set();
27  void *storage = ((boost::python::converter::rvalue_from_python_storage<std::string_view> *) data)->storage.bytes;
28  new(storage) std::string_view(value);
29  data->convertible = storage;
30  }
31  };
32 
34  boost::python::to_python_converter<std::string_view, string_view_to_python_str>();
35  boost::python::converter::registry::push_back(
38  boost::python::type_id<std::string_view>()
39  );
40  }
41 }
42 
43 #endif //KNOWROB_PY_CONVERTER_STRING_VIEW_H
void register_string_view_converter()
Definition: string_view.h:33
static void * convertible(PyObject *obj_ptr)
Definition: string_view.h:19
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Definition: string_view.h:24
static PyObject * convert(std::string_view s)
Definition: string_view.h:12