From 6cb63c923c49a8763ec99eb7a9285ef79558b8f2 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 27 Sep 2021 00:05:54 +0300 Subject: simple-cam: Move camera detection check to where it belongs Commit 14529b6d1c4a ("simple-cam: Early return if no cameras are found on the system") added a check to ensure that at least one camera is present in the system to avoid a crash otherwise. The check was however placed towards the end of the main() function, way after the camera is accessed, possibly due to a rebase conflict. Move it before accessing the first camera. While at it, replace usage of std::vector::size() with std::vector::empty() which expresses the intent better. Fixes: 14529b6d1c4a ("simple-cam: Early return if no cameras are found on the system") Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham Signed-off-by: Kieran Bingham --- simple-cam.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/simple-cam.cpp b/simple-cam.cpp index 95e472b..1a60602 100644 --- a/simple-cam.cpp +++ b/simple-cam.cpp @@ -163,13 +163,21 @@ int main() * Application lock usage of Camera by 'acquiring' them. * Once done with it, application shall similarly 'release' the Camera. * - * As an example, use the first available camera in the system. + * As an example, use the first available camera in the system after + * making sure that at least one camera is available. * * Cameras can be obtained by their ID or their index, to demonstrate * this, the following code gets the ID of the first camera; then gets * the camera associated with that ID (which is of course the same as * cm->cameras()[0]). */ + if (cm->cameras().empty()) { + std::cout << "No cameras were identified on the system." + << std::endl; + cm->stop(); + return EXIT_FAILURE; + } + std::string cameraId = cm->cameras()[0]->id(); camera = cm->get(cameraId); camera->acquire(); @@ -386,13 +394,6 @@ int main() for (std::unique_ptr &request : requests) camera->queueRequest(request.get()); - if (!cm->cameras().size()) { - std::cout << "No cameras were identified on the system." - << std::endl; - cm->stop(); - return EXIT_FAILURE; - } - /* * -------------------------------------------------------------------- * Run an EventLoop -- cgit v1.2.1