From patchwork Sat May 15 04:05:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12299 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id F1284C31F6 for ; Sat, 15 May 2021 04:05:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B1E0468922; Sat, 15 May 2021 06:05:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="I3V98h+B"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7C1566890E for ; Sat, 15 May 2021 06:05:25 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F2BCE6EE for ; Sat, 15 May 2021 06:05:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1621051525; bh=9a7WHIyNzHJhF1vP7jYh9uigTP9HJd/ga9ckyToKuN0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=I3V98h+Bz+DY0j/5jWP7KSUJY44hmA/pT1zuK5xukREIIYPSw2Ji4ZyzQld/tXJOt eZ2Xi04s1W4eGQzkMPgiA87INfHNUJ1Xffz8xv9vxv4mKMEVVUh6X0OiJ3g4IG1ABC wJP9ezNfmrK4uUSvOO9MDwczyJtqC0w+KoCxjG3M= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 15 May 2021 07:05:08 +0300 Message-Id: <20210515040511.23294-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> References: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/4] libcamera: utils: Add enumerate view for range-based for loops X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Range-based for loops are handy and widely preferred in C++, but are limited in their ability to replace for loops that require access to a loop counter. The enumerate() function solves this problem by wrapping the \a iterable in an adapter that, when used as a range-expression, will provide iterators whose value_type is a pair of index and value reference. The iterable must support std::begin() and std::end(). This includes all containers provided by the standard C++ library, as well as C-style arrays. A typical usage pattern would use structured binding to store the index and value in two separate variables: std::vector values = ...; for (auto [index, value] : utils::enumerate(values)) { ... } Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- Changes since v1: - Hardcoded enumerate_iterator::iterator_category to std::input_iterator_tag - Made the enumerate_adapter begin(), end(), begin_ and end_ const --- include/libcamera/internal/utils.h | 86 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 29 ++++++++++ test/utils.cpp | 59 ++++++++++++++++++++ 3 files changed, 174 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index d0146b71727d..83dada7cc16c 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -9,12 +9,14 @@ #include #include +#include #include #include #include #include #include #include +#include #include #ifndef __DOXYGEN__ @@ -230,6 +232,90 @@ details::reverse_adapter reverse(T &&iterable) return { iterable }; } +namespace details { + +template +class enumerate_iterator +{ +private: + using base_reference = typename std::iterator_traits::reference; + +public: + using difference_type = typename std::iterator_traits::difference_type; + using value_type = std::pair; + using pointer = value_type *; + using reference = value_type &; + using iterator_category = std::input_iterator_tag; + + explicit enumerate_iterator(Base iter) + : current_(iter), pos_(0) + { + } + + enumerate_iterator &operator++() + { + ++current_; + ++pos_; + return *this; + } + + bool operator!=(const enumerate_iterator &other) const + { + return current_ != other.current_; + } + + value_type operator*() const + { + return { pos_, *current_ }; + } + +private: + Base current_; + difference_type pos_; +}; + +template +class enumerate_adapter +{ +public: + using iterator = enumerate_iterator; + + enumerate_adapter(Base begin, Base end) + : begin_(begin), end_(end) + { + } + + iterator begin() const + { + return iterator{ begin_ }; + } + + iterator end() const + { + return iterator{ end_ }; + } + +private: + const Base begin_; + const Base end_; +}; + +} /* namespace details */ + +template +auto enumerate(T &iterable) -> details::enumerate_adapter +{ + return { std::begin(iterable), std::end(iterable) }; +} + +#ifndef __DOXYGEN__ +template +auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter +{ + return { std::begin(iterable), std::end(iterable) }; +} +#endif + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index c4098a74e0ab..ff9a5832b10e 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -472,6 +472,35 @@ std::string libcameraSourcePath() * loop, will cause the loop to iterate over the \a iterable in reverse order */ +/** + * \fn enumerate(T &iterable) + * \brief Wrap an iterable to enumerate index and value in a range-based loop + * \param[in] iterable The iterable + * + * Range-based for loops are handy and widely preferred in C++, but are limited + * in their ability to replace for loops that require access to a loop counter. + * The enumerate() function solves this problem by wrapping the \a iterable in + * an adapter that, when used as a range-expression, will provide iterators + * whose value_type is a pair of index and value reference. + * + * The iterable must support std::begin() and std::end(). This includes all + * containers provided by the standard C++ library, as well as C-style arrays. + * + * A typical usage pattern would use structured binding to store the index and + * value in two separate variables: + * + * \code{.cpp} + * std::vector values = ...; + * + * for (auto [index, value] : utils::enumerate(values)) { + * ... + * } + * \endcode + * + * \return A value of unspecified type that, when used in a range-based for + * loop, iterates over an indexed view of the \a iterable + */ + } /* namespace utils */ } /* namespace libcamera */ diff --git a/test/utils.cpp b/test/utils.cpp index 08f293898fd9..7e24c71e4775 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "libcamera/internal/utils.h" @@ -73,6 +74,60 @@ protected: return TestPass; } + int testEnumerate() + { + std::vector integers{ 1, 2, 3, 4, 5 }; + int i = 0; + + for (auto [index, value] : utils::enumerate(integers)) { + if (index != i || value != i + 1) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + /* Verify that we can modify the value. */ + --value; + ++i; + } + + if (integers != std::vector{ 0, 1, 2, 3, 4 }) { + cerr << "Failed to modify container in enumerated range loop" << endl; + return TestFail; + } + + Span span{ integers }; + i = 0; + + for (auto [index, value] : utils::enumerate(span)) { + if (index != i || value != i) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + ++i; + } + + const int array[] = { 0, 2, 4, 6, 8 }; + i = 0; + + for (auto [index, value] : utils::enumerate(array)) { + if (index != i || value != i * 2) { + cerr << "utils::enumerate() test failed: i=" << i + << ", index=" << index << ", value=" << value + << std::endl; + return TestFail; + } + + ++i; + } + + return TestPass; + } + int run() { /* utils::hex() test. */ @@ -177,6 +232,10 @@ protected: return TestFail; } + /* utils::enumerate() test. */ + if (testEnumerate() != TestPass) + return TestFail; + return TestPass; } }; From patchwork Sat May 15 04:05:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12300 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 4BF42C31F6 for ; Sat, 15 May 2021 04:05:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 086C16891B; Sat, 15 May 2021 06:05:31 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="geQXy/77"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 528EC6890E for ; Sat, 15 May 2021 06:05:26 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C56326EE for ; Sat, 15 May 2021 06:05:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1621051525; bh=2QFRSWUCMAfsZ+kMhZHJISQfTSzuMpk9atCohxy2mvY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=geQXy/77hkrYnlb4jkbTUaaq2XM9miRWbgB842HmAGO+4xKvLcVmeQgpDnXX/RJTp jN5HyIi5cuK8OyaMdqksPQWh0CgfUAFLMoOLNCterZI8IXH68n+dNhHjQwuIbIZz8U ZyzRhS76xpfnkKEYvRjCteWXoEe5Q7OWKRP5cs/U= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 15 May 2021 07:05:09 +0300 Message-Id: <20210515040511.23294-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> References: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 2/4] v4l2: Replace manual loop counters with utils::enumerate() X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Use the newly introduced utils::enumerate() to replace manual loop counters. A local variable is needed, as utils::enumerate() requires an lvalue reference. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Kieran Bingham --- Changes since v1: - Use structured bindings --- src/v4l2/v4l2_compat_manager.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp index 90c0f0121a32..96dbcdf28f04 100644 --- a/src/v4l2/v4l2_compat_manager.cpp +++ b/src/v4l2/v4l2_compat_manager.cpp @@ -23,6 +23,7 @@ #include #include "libcamera/internal/log.h" +#include "libcamera/internal/utils.h" #include "v4l2_camera_file.h" @@ -81,11 +82,10 @@ int V4L2CompatManager::start() * For each Camera registered in the system, a V4L2CameraProxy gets * created here to wrap a camera device. */ - unsigned int index = 0; - for (auto &camera : cm_->cameras()) { + auto cameras = cm_->cameras(); + for (auto [index, camera] : utils::enumerate(cameras)) { V4L2CameraProxy *proxy = new V4L2CameraProxy(index, camera); proxies_.emplace_back(proxy); - ++index; } return 0; @@ -117,11 +117,10 @@ int V4L2CompatManager::getCameraIndex(int fd) if (!target) return -1; - unsigned int index = 0; - for (auto &camera : cm_->cameras()) { + auto cameras = cm_->cameras(); + for (auto [index, camera] : utils::enumerate(cameras)) { if (camera == target) return index; - ++index; } return -1; From patchwork Sat May 15 04:05:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12301 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id E36E6C31F6 for ; Sat, 15 May 2021 04:05:31 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A190F6891C; Sat, 15 May 2021 06:05:31 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YRJl16Bm"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B277A68923 for ; Sat, 15 May 2021 06:05:26 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 679E08BA for ; Sat, 15 May 2021 06:05:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1621051526; bh=xdyhj7jYUZd6VeeyYZ0TE83/w4PfET+8+wYL0cDkmIc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=YRJl16BmNGD9dSw1nrbG7OuUL8Jz2xMudHXuk8pH9+XsYl6U2UodWBHBgR0ph3Ze2 nFrIpGLeBxfA0/V7jSK3aGG+Ucz4Dp5gtm2tozevhS5UH1wblDTlM83zeG1iFs849B ZNAA6VVJ21+8NhMwTUDn/PLgJtwneXJIH64vJtSc= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 15 May 2021 07:05:10 +0300 Message-Id: <20210515040511.23294-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> References: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 3/4] libcamera: utils: enumerate: Use named fields for result X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Returning a pair of { index, value } from container enumeration is error-prone, as the index and value can easily be swapped. Use a structure with named index and value fields instead. This will be squashed with "libcamera: utils: Add enumerate view for range-based for loops" if accepted. Signed-off-by: Laurent Pinchart --- include/libcamera/internal/utils.h | 16 +++++++++++----- src/libcamera/utils.cpp | 13 +++++++------ test/utils.cpp | 24 ++++++++++++------------ 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 83dada7cc16c..5ea82e68a7a2 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #ifndef __DOXYGEN__ @@ -237,12 +236,19 @@ namespace details { template class enumerate_iterator { -private: - using base_reference = typename std::iterator_traits::reference; - public: using difference_type = typename std::iterator_traits::difference_type; - using value_type = std::pair; + +private: + using base_reference = typename std::iterator_traits::reference; + + struct result { + const difference_type index; + base_reference value; + }; + +public: + using value_type = result; using pointer = value_type *; using reference = value_type &; using iterator_category = std::input_iterator_tag; diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index ff9a5832b10e..b4b4180c1337 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -481,20 +481,21 @@ std::string libcameraSourcePath() * in their ability to replace for loops that require access to a loop counter. * The enumerate() function solves this problem by wrapping the \a iterable in * an adapter that, when used as a range-expression, will provide iterators - * whose value_type is a pair of index and value reference. + * whose value_type exposes both the element's value and its index. * * The iterable must support std::begin() and std::end(). This includes all * containers provided by the standard C++ library, as well as C-style arrays. * - * A typical usage pattern would use structured binding to store the index and - * value in two separate variables: + * The iterator's value_type is a structure that aggregates the index and value + * reference in two named members. The index is always const, and the value + * reference is conditionally const depending on the iterable. A typical usage + * pattern would be: * * \code{.cpp} * std::vector values = ...; * - * for (auto [index, value] : utils::enumerate(values)) { - * ... - * } + * for (const auto v : utils::enumerate(values)) + * std::cout << "- index " << v.index << ", value " << v.value << std::endl; * \endcode * * \return A value of unspecified type that, when used in a range-based for diff --git a/test/utils.cpp b/test/utils.cpp index 7e24c71e4775..06ce5301a74e 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -79,16 +79,16 @@ protected: std::vector integers{ 1, 2, 3, 4, 5 }; int i = 0; - for (auto [index, value] : utils::enumerate(integers)) { - if (index != i || value != i + 1) { + for (const auto v : utils::enumerate(integers)) { + if (v.index != i || v.value != i + 1) { cerr << "utils::enumerate() test failed: i=" << i - << ", index=" << index << ", value=" << value - << std::endl; + << ", index=" << v.index << ", value=" << v.value + << endl; return TestFail; } /* Verify that we can modify the value. */ - --value; + --v.value; ++i; } @@ -100,10 +100,10 @@ protected: Span span{ integers }; i = 0; - for (auto [index, value] : utils::enumerate(span)) { - if (index != i || value != i) { + for (const auto v : utils::enumerate(span)) { + if (v.index != i || v.value != i) { cerr << "utils::enumerate() test failed: i=" << i - << ", index=" << index << ", value=" << value + << ", index=" << v.index << ", value=" << v.value << std::endl; return TestFail; } @@ -114,11 +114,11 @@ protected: const int array[] = { 0, 2, 4, 6, 8 }; i = 0; - for (auto [index, value] : utils::enumerate(array)) { - if (index != i || value != i * 2) { + for (const auto v : utils::enumerate(array)) { + if (v.index != i || v.value != i * 2) { cerr << "utils::enumerate() test failed: i=" << i - << ", index=" << index << ", value=" << value - << std::endl; + << ", index=" << v.index << ", value=" << v.value + << endl; return TestFail; } From patchwork Sat May 15 04:05:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12302 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 74768C31F6 for ; Sat, 15 May 2021 04:05:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0918968919; Sat, 15 May 2021 06:05:32 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UVO3X358"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 28FD168920 for ; Sat, 15 May 2021 06:05:27 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B43C36EE for ; Sat, 15 May 2021 06:05:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1621051526; bh=5kg12NqlBw/SExg63szd59FYREyrjhOUD19hARUxaoM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=UVO3X3581J0O82BlcpoIPnghuF02WIuC7OEGarOclqbZwZ3fv+8o2B6seSpykeZCn aORNtn8qfS5rPx4XoIlA6kHc/erNpf7ikMbjygKdJbhjds3J2Hodaz3LOa1glma159 ZaHvDBkBWvOOhTVI5tM/m3UDymcEFG5Eq9k3lF8U= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sat, 15 May 2021 07:05:11 +0300 Message-Id: <20210515040511.23294-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> References: <20210515040511.23294-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 4/4] v4l2: Adapt to utils::enumerate() usage of named fields X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This will be squashed with "v4l2: Replace manual loop counters with utils::enumerate()" if "libcamera: utils: enumerate: Use named fields for result" is accepted. Signed-off-by: Laurent Pinchart --- src/v4l2/v4l2_compat_manager.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp index 96dbcdf28f04..bf5bf8e3223d 100644 --- a/src/v4l2/v4l2_compat_manager.cpp +++ b/src/v4l2/v4l2_compat_manager.cpp @@ -83,8 +83,9 @@ int V4L2CompatManager::start() * created here to wrap a camera device. */ auto cameras = cm_->cameras(); - for (auto [index, camera] : utils::enumerate(cameras)) { - V4L2CameraProxy *proxy = new V4L2CameraProxy(index, camera); + for (const auto camera : utils::enumerate(cameras)) { + V4L2CameraProxy *proxy = new V4L2CameraProxy(camera.index, + camera.value); proxies_.emplace_back(proxy); } @@ -118,9 +119,9 @@ int V4L2CompatManager::getCameraIndex(int fd) return -1; auto cameras = cm_->cameras(); - for (auto [index, camera] : utils::enumerate(cameras)) { - if (camera == target) - return index; + for (const auto camera : utils::enumerate(cameras)) { + if (camera.value == target) + return camera.index; } return -1;