From patchwork Fri Apr 23 02:09:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12085 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 4916DBDB1A for ; Fri, 23 Apr 2021 02:09:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7741C68879; Fri, 23 Apr 2021 04:09:43 +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="ukxXi9pD"; 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 459106885B 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 B1CAA45F for ; Fri, 23 Apr 2021 04:09:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1619143780; bh=Hf6kboZOGCYNQ8hLHZqXz/VKngjvZfm5cUG4hm434c4=; h=From:To:Subject:Date:From; b=ukxXi9pDjnAkzH2uBUeTBVvFE6c/9421FYzr1WIgqdaJL74+OF1mhmwLzTc4NtTwc 9RqYmeSbPeM3BP+gA3IHxea15CtckLkvb46WSx3vx2ljr5/14rA2jvW6wloyVAwuAA QnnZrU/oacqonhmHUJ7LxA7ePeMPrB4AKMmhdbtQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 23 Apr 2021 05:09:29 +0300 Message-Id: <20210423020932.2760-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.28.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH/RFC 0/3] libcamera: Simplify range-based for loop counters 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" Hello, This RFC patch series is an attempt to simplify for loops that iterate over a range, and require a loop counter. The limitation of the range-based for loop is explained in patch 1/3, which provides a new utils::enumerate() function to solve the problem. The API is inspired by the C++23 views::enumerate proposal (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2164r1.pdf), but implemented without the range library as that's a C++20 feature. Patch 2/3 provides an alternative API that uses an aggregate with named fields instead of an std::pair with structured binding. I currently have a slight preference for the named fields, and if we agree that this API is better, I'll squash 1/3 and 2/3. Patch 3/3 is an example of how code can be simplified. It also outlines a limitation of the API. Due to the C++ lifetime rules, we can't safely pass a temporary to utils::enumerate() as the temporary would be destroyed once utils::enumerate() returns but before the for loop completes (actually before it even starts), resulting in use-after-free errors. The issue is explained in more details in http://josuttis.com/download/std/D2012R0_fix_rangebasedfor_201029.pdf. The ensure that such bugs can't be introduced by mistake, the utils::enumerate() function takes an lvalue reference only. The drawback is that an loop that iterates over a temporary must now stores that temporary in a named variable before the loop. On a side note, C++20 would bring us a workaround to declare that variable within the loop statement. If anyone can think of a clever idea to fix this problem, I'm all ears. One option may be to store a copy of the iterable in the adapter when utils::enumerate() is given an rvalue, possibly with move semantics, but it may still be costly in some cases, I'm not sure how compilers would optimize that. Laurent Pinchart (3): libcamera: utils: Add enumerate view for range-based for loops libcamera: utils: enumerate: Use named fields for result v4l2: Replace manual loop counters with utils::enumerate() include/libcamera/internal/utils.h | 92 ++++++++++++++++++++++++++++++ src/libcamera/utils.cpp | 30 ++++++++++ src/v4l2/v4l2_compat_manager.cpp | 18 +++--- test/utils.cpp | 59 +++++++++++++++++++ 4 files changed, 190 insertions(+), 9 deletions(-)