|
| 1 | +/********************************************************************* |
| 2 | + * Software License Agreement (BSD License) |
| 3 | + * |
| 4 | + * Copyright (c) 2021, Robert Haschke |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * * Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * * Redistributions in binary form must reproduce the above |
| 14 | + * copyright notice, this list of conditions and the following |
| 15 | + * disclaimer in the documentation and/or other materials provided |
| 16 | + * with the distribution. |
| 17 | + * * The name of Robert Haschke may not be used to endorse or promote |
| 18 | + * products derived from this software without specific prior |
| 19 | + * written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 24 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 25 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 27 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 31 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + * POSSIBILITY OF SUCH DAMAGE. |
| 33 | + *********************************************************************/ |
| 34 | + |
| 35 | +/* Author: Robert Haschke */ |
| 36 | + |
| 37 | +#pragma once |
| 38 | + |
| 39 | +#include <pybind11/pybind11.h> |
| 40 | +#include <ros/duration.h> |
| 41 | +#include <ros/serialization.h> |
| 42 | + |
| 43 | +/** Provide pybind11 type converters for ROS types */ |
| 44 | + |
| 45 | +namespace py_binding_tools |
| 46 | +{ |
| 47 | +PYBIND11_EXPORT pybind11::object createMessage(const std::string& ros_msg_name); |
| 48 | +PYBIND11_EXPORT bool convertible(const pybind11::handle& h, const char* ros_msg_name); |
| 49 | + |
| 50 | +/** Throws genpy.DeserializationError exception. |
| 51 | + * |
| 52 | + * This is a convenience method to support the old behaviour of returning |
| 53 | + * an empty ByteString. |
| 54 | + */ |
| 55 | +PYBIND11_EXPORT void throwDeserializationError [[noreturn]] (); |
| 56 | + |
| 57 | +} // namespace py_binding_tools |
| 58 | + |
| 59 | +namespace pybind11 |
| 60 | +{ |
| 61 | +namespace detail |
| 62 | +{ |
| 63 | +/// Convert ros::Duration / ros::WallDuration into a float |
| 64 | +template <typename T> |
| 65 | +struct DurationCaster |
| 66 | +{ |
| 67 | + // C++ -> Python |
| 68 | + static handle cast(T&& src, return_value_policy /* policy */, handle /* parent */) |
| 69 | + { |
| 70 | + return PyFloat_FromDouble(src.toSec()); |
| 71 | + } |
| 72 | + |
| 73 | + // Python -> C++ |
| 74 | + bool load(handle src, bool convert) |
| 75 | + { |
| 76 | + if (hasattr(src, "to_sec")) |
| 77 | + { |
| 78 | + value = T(src.attr("to_sec")().cast<double>()); |
| 79 | + } |
| 80 | + else if (convert) |
| 81 | + { |
| 82 | + value = T(src.cast<double>()); |
| 83 | + } |
| 84 | + else |
| 85 | + return false; |
| 86 | + return true; |
| 87 | + } |
| 88 | + |
| 89 | + PYBIND11_TYPE_CASTER(T, _("Duration")); |
| 90 | +}; |
| 91 | + |
| 92 | +template <> |
| 93 | +struct type_caster<ros::Duration> : DurationCaster<ros::Duration> |
| 94 | +{ |
| 95 | +}; |
| 96 | + |
| 97 | +template <> |
| 98 | +struct type_caster<ros::WallDuration> : DurationCaster<ros::WallDuration> |
| 99 | +{ |
| 100 | +}; |
| 101 | + |
| 102 | +/// Base class for type conversion (C++ <-> python) of ROS message types |
| 103 | +template <typename T> |
| 104 | +struct RosMsgTypeCaster |
| 105 | +{ |
| 106 | + // C++ -> Python |
| 107 | + static handle cast(const T& src, return_value_policy /* policy */, handle /* parent */) |
| 108 | + { |
| 109 | + // serialize src into (python) buffer |
| 110 | + std::size_t size = ros::serialization::serializationLength(src); |
| 111 | + object pbuffer = reinterpret_steal<object>(PyBytes_FromStringAndSize(nullptr, size)); |
| 112 | + ros::serialization::OStream stream(reinterpret_cast<uint8_t*>(PyBytes_AsString(pbuffer.ptr())), size); |
| 113 | + ros::serialization::serialize(stream, src); |
| 114 | + // deserialize python type from buffer |
| 115 | + object msg = py_binding_tools::createMessage(ros::message_traits::DataType<T>::value()); |
| 116 | + msg.attr("deserialize")(pbuffer); |
| 117 | + return msg.release(); |
| 118 | + } |
| 119 | + |
| 120 | + // Python -> C++ |
| 121 | + bool load(handle src, bool /*convert*/) |
| 122 | + { |
| 123 | + if (!py_binding_tools::convertible(src, ros::message_traits::DataType<T>::value())) |
| 124 | + return false; |
| 125 | + // serialize src into (python) buffer |
| 126 | + object pstream = module::import("io").attr("BytesIO")(); |
| 127 | + src.attr("serialize")(pstream); |
| 128 | + object pbuffer = pstream.attr("getvalue")(); |
| 129 | + // deserialize C++ type from buffer |
| 130 | + char* cbuffer = nullptr; |
| 131 | + Py_ssize_t size; |
| 132 | + PyBytes_AsStringAndSize(pbuffer.ptr(), &cbuffer, &size); |
| 133 | + ros::serialization::IStream cstream(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(cbuffer)), size); |
| 134 | + ros::serialization::deserialize(cstream, value); |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + PYBIND11_TYPE_CASTER(T, _<T>()); |
| 139 | +}; |
| 140 | + |
| 141 | +template <typename T> |
| 142 | +struct type_caster<T, enable_if_t<ros::message_traits::IsMessage<T>::value>> : RosMsgTypeCaster<T> |
| 143 | +{ |
| 144 | +}; |
| 145 | + |
| 146 | +} // namespace detail |
| 147 | +} // namespace pybind11 |
0 commit comments