Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py lcm] Adjust CreateDefaultValue to avoid unique_ptr #22463

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions bindings/pydrake/systems/lcm_py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ class PySerializerInterface : public py::wrapper<SerializerInterface> {
// `PySerializerInterface`). C++ implementations will use the bindings on the
// interface below.

std::unique_ptr<AbstractValue> CreateDefaultValue() const override {
PYBIND11_OVERLOAD_PURE(std::unique_ptr<AbstractValue>, SerializerInterface,
CreateDefaultValue);
std::unique_ptr<AbstractValue> CreateDefaultValue() const final {
// Our required unique_ptr return type cannot be directly fulfilled by a
// Python override, so we only ask the Python override for a py::object and
// then just Clone it to obtain the necessary C++ signature. Because the
// PYBIND11_OVERLOAD_PURE macro embeds a `return ...;` statement, we must
// wrap it in lambda so that we can post-process the return value.
py::object default_value = [this]() -> py::object {
PYBIND11_OVERLOAD_PURE(
py::object, SerializerInterface, CreateDefaultValue);
}();
DRAKE_THROW_UNLESS(!default_value.is_none());
return default_value.template cast<const AbstractValue*>()->Clone();
}

void Deserialize(const void* message_bytes, int message_length,
Expand Down