Skip to content

Commit

Permalink
unique_ptr: Confirm RobotLocomotion/drake#8160
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Feb 23, 2018
1 parent 060f8eb commit ac2205a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/test_smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ TEST_SUBMODULE(smart_ptr, m) {
return py::cast(std::move(obj));
});

class FirstT {};
py::class_<FirstT>(m, "FirstT")
.def(py::init());
class SecondT {};
py::class_<SecondT>(m, "SecondT")
.def(py::init());

m.def("unique_ptr_overload",
[](std::unique_ptr<UniquePtrHeld> obj, FirstT) {
py::dict out;
out["obj"] = py::cast(std::move(obj));
out["overload"] = 1;
return out;
});
m.def("unique_ptr_overload",
[](std::unique_ptr<UniquePtrHeld> obj, SecondT) {
py::dict out;
out["obj"] = py::cast(std::move(obj));
out["overload"] = 2;
return out;
});

// Ensure class is non-empty, so it's easier to detect double-free
// corruption. (If empty, this may be harder to see easily.)
struct SharedPtrHeld { int value = 10; };
Expand Down
11 changes: 11 additions & 0 deletions tests/test_smart_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,14 @@ def test_unique_ptr_arg():
def test_unique_ptr_to_shared_ptr():
obj = m.shared_ptr_held_in_unique_ptr()
assert m.shared_ptr_held_func(obj)


def test_unique_ptr_overload_fail():
obj = m.UniquePtrHeld(1)
# These overloads pass ownership back to Python.
out = m.unique_ptr_overload(obj, m.FirstT())
assert out["obj"] is obj
assert out["overload"] == 1
out = m.unique_ptr_overload(obj, m.SecondT())
assert out["obj"] is obj
assert out["overload"] == 2

0 comments on commit ac2205a

Please sign in to comment.