Skip to content

Commit

Permalink
Add is_connected() method to python_binding example
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Feb 6, 2024
1 parent 620a166 commit f5d94a8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to the Softcam library will be documented in this file.

### [Unreleased]
- Added `scIsConnected()` method to API to make application connection waiting more flexible by @jekyll2014. (thanks!) [#51](https://github.com/tshino/softcam/pull/51)
- Added corresponding `is_connected()` method to the python_binding example.
- Updated solution file version from Visual Studio 2019 to Visual Studio 2022. [#45](https://github.com/tshino/softcam/pull/45)
- Added `pushd`/`popd` to example installer BAT files to work correctly even if launched from another directory. [#48](https://github.com/tshino/softcam/pull/48)

Expand Down
14 changes: 14 additions & 0 deletions examples/python_binding/python_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class Camera
return scWaitForConnection(m_camera, timeout);
}

bool IsConnected()
{
if (!m_camera)
{
throw std::runtime_error("the camera instance has been deleted");
}

return scIsConnected(m_camera);
}

private:
scCamera m_camera{};
int m_width = 0;
Expand Down Expand Up @@ -110,5 +120,9 @@ PYBIND11_MODULE(softcam, m) {
&Camera::WaitForConnection,
py::arg("timeout") = 0.0f
)
.def(
"is_connected",
&Camera::IsConnected
)
;
}
14 changes: 14 additions & 0 deletions examples/python_binding/tests/test_softcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,17 @@ def test_wait_for_connection_use_after_free():
with pytest.raises(RuntimeError) as e:
assert cam.wait_for_connection()
assert e.value.args == ('the camera instance has been deleted',)


def test_is_connected():
cam = softcam.camera(320, 240, 60)
assert cam.is_connected() == False
cam = None


def test_is_connected_use_after_free():
cam = softcam.camera(320, 240, 60)
cam.delete()
with pytest.raises(RuntimeError) as e:
assert cam.is_connected()
assert e.value.args == ('the camera instance has been deleted',)

0 comments on commit f5d94a8

Please sign in to comment.