forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example no intermediate type downcast. show downcast hurts identity
- Loading branch information
1 parent
82057e6
commit 09b7338
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# empty. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |