aligator  0.16.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
string-view-converter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/python.hpp>
4
5namespace bp = boost::python;
6
7namespace aligator::python {
8namespace detail {
9
10struct string_view_to_python {
11 static PyObject *convert(std::string_view sv) noexcept {
12 return PyUnicode_FromStringAndSize(sv.data(),
13 static_cast<Py_ssize_t>(sv.size()));
14 }
15};
16
17struct string_view_from_python {
18 static void *convertible(PyObject *obj_ptr) noexcept {
19 if (!PyUnicode_Check(obj_ptr) && !PyBytes_Check(obj_ptr)) {
20 return nullptr;
21 }
22 return obj_ptr;
23 }
24
25 static void construct(PyObject *obj_ptr,
26 bp::converter::rvalue_from_python_stage1_data *data) {
27 const char *value = nullptr;
28 Py_ssize_t len = 0;
29
30 if (PyUnicode_Check(obj_ptr)) {
31 value = PyUnicode_AsUTF8AndSize(obj_ptr, &len);
32 if (!value) {
33 bp::throw_error_already_set();
34 }
35
36 void *storage =
37 ((bp::converter::rvalue_from_python_storage<std::string_view> *)data)
38 ->storage.bytes;
39 new (storage) std::string_view(value, static_cast<size_t>(len));
40 data->convertible = storage;
41 }
42 }
43};
44} // namespace detail
45
47 bp::to_python_converter<std::string_view, detail::string_view_to_python>();
48
49 bp::converter::registry::push_back(
50 &detail::string_view_from_python::convertible,
51 &detail::string_view_from_python::construct,
52 bp::type_id<std::string_view>());
53}
54
55} // namespace aligator::python
The Python bindings.
Definition blk-matrix.hpp:5