From patchwork Fri Apr 23 02:09:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12087 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 2D8E0BDB70 for ; Fri, 23 Apr 2021 02:09:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5A39468876; Fri, 23 Apr 2021 04:09:45 +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="WdVg2FZh"; 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 D1D7968861 for ; Fri, 23 Apr 2021 04:09:41 +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 69090BB4 for ; Fri, 23 Apr 2021 04:09:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1619143781; bh=fOCXAOCwyja2xqpQr1Uu+VhZq21pbLFFRj4MerDiJSc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=WdVg2FZho6e6R/SsJKZ6759slxGynfDY//piOn6ZKmWs13mL5seh7OUIqusA8ztuC p3FTKS/2C+9P1ogsc7dxQ8ErZsV8AW1JCEXHM4nTGvEkmZzXFih026L8NLZt3eqpd4 aincSHwhwm6Omv1uKx6Mafzuo3zXBxu0lE0nYT+E= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 23 Apr 2021 05:09:31 +0300 Message-Id: <20210423020932.2760-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 In-Reply-To: <20210423020932.2760-1-laurent.pinchart@ideasonboard.com> References: <20210423020932.2760-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH/RFC 2/3] 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. 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 9372a75889ac..54a6b6d283fb 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 = typename std::iterator_traits::iterator_category; 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; }