Skip to content

Commit

Permalink
example no intermediate type downcast. show downcast hurts identity
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed May 13, 2020
1 parent 82057e6 commit 09b7338
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tmp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load(
"//tools/skylark:drake_py.bzl",
"drake_py_binary",
"drake_py_library",
)
load(
"//tools/skylark:pybind.bzl",
"pybind_py_library",
)

drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
)

pybind_py_library(
name = "cc_py",
cc_deps = ["//:drake_shared_library"],
cc_so_name = "cc",
cc_srcs = ["cc_py.cc"],
py_deps = [
":module_py",
"@drake//bindings/pydrake",
],
)

drake_py_binary(
name = "repro",
srcs = ["repro.py"],
deps = [":cc_py"],
)
1 change: 1 addition & 0 deletions tmp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# empty.
39 changes: 39 additions & 0 deletions tmp/cc_py.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <pybind11/pybind11.h>

#include "drake/common/drake_assert.h"
#include "drake/systems/framework/diagram_builder.h"
#include "drake/systems/primitives/adder.h"

namespace py = pybind11;

namespace drake {
namespace pydrake {

class MyDiagram : public systems::Diagram<double> {
public:
MyDiagram() {
systems::DiagramBuilder<double> builder;
builder.AddSystem<systems::Adder<double>>(0, 1);
builder.BuildInto(this);
}
};

PYBIND11_MODULE(cc, m) {
py::module::import("pydrake.systems.framework");

m.def("make_my_diagram_as_system", []()
-> std::unique_ptr<systems::System<double>> {
return std::make_unique<MyDiagram>();
});
m.def("make_my_diagram_as_diagram", []()
-> std::unique_ptr<systems::Diagram<double>> {
return std::make_unique<MyDiagram>();
});

m.def("as_diagram", [](systems::System<double>* system) {
return dynamic_cast<systems::Diagram<double>*>(system);
}, py::return_value_policy::reference, py::keep_alive<0, 1>());
}

} // namespace
} // namespace pydrake
9 changes: 9 additions & 0 deletions tmp/repro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydrake.systems.framework import LeafSystem, Diagram
from drake.tmp.cc import make_my_diagram_as_system, make_my_diagram_as_diagram, as_diagram

system = make_my_diagram_as_system()
print(system)
print(as_diagram(system))
assert as_diagram(system) is not system # :(
diagram = make_my_diagram_as_diagram()
print(diagram)

0 comments on commit 09b7338

Please sign in to comment.