{"id":12085,"url":"https://patchwork.libcamera.org/api/1.1/covers/12085/?format=json","web_url":"https://patchwork.libcamera.org/cover/12085/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210423020932.2760-1-laurent.pinchart@ideasonboard.com>","date":"2021-04-23T02:09:29","name":"[libcamera-devel,PATCH/RFC,0/3] libcamera: Simplify range-based for loop counters","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/cover/12085/mbox/","series":[{"id":1966,"url":"https://patchwork.libcamera.org/api/1.1/series/1966/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1966","date":"2021-04-23T02:09:29","name":"libcamera: Simplify range-based for loop counters","version":1,"mbox":"https://patchwork.libcamera.org/series/1966/mbox/"}],"comments":"https://patchwork.libcamera.org/api/covers/12085/comments/","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4916DBDB1A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 23 Apr 2021 02:09:44 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7741C68879;\n\tFri, 23 Apr 2021 04:09:43 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 459106885B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 23 Apr 2021 04:09:41 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B1CAA45F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 23 Apr 2021 04:09:40 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ukxXi9pD\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1619143780;\n\tbh=Hf6kboZOGCYNQ8hLHZqXz/VKngjvZfm5cUG4hm434c4=;\n\th=From:To:Subject:Date:From;\n\tb=ukxXi9pDjnAkzH2uBUeTBVvFE6c/9421FYzr1WIgqdaJL74+OF1mhmwLzTc4NtTwc\n\t9RqYmeSbPeM3BP+gA3IHxea15CtckLkvb46WSx3vx2ljr5/14rA2jvW6wloyVAwuAA\n\tQnnZrU/oacqonhmHUJ7LxA7ePeMPrB4AKMmhdbtQ=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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\n\tfor loop counters","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Hello,\n\nThis RFC patch series is an attempt to simplify for loops that iterate\nover a range, and require a loop counter.\n\nThe limitation of the range-based for loop is explained in patch 1/3,\nwhich provides a new utils::enumerate() function to solve the problem.\nThe API is inspired by the C++23 views::enumerate proposal\n(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2164r1.pdf),\nbut implemented without the range library as that's a C++20 feature.\n\nPatch 2/3 provides an alternative API that uses an aggregate with named\nfields instead of an std::pair with structured binding. I currently have\na slight preference for the named fields, and if we agree that this API\nis better, I'll squash 1/3 and 2/3.\n\nPatch 3/3 is an example of how code can be simplified. It also outlines\na limitation of the API. Due to the C++ lifetime rules, we can't safely\npass a temporary to utils::enumerate() as the temporary would be\ndestroyed once utils::enumerate() returns but before the for loop\ncompletes (actually before it even starts), resulting in use-after-free\nerrors. The issue is explained in more details in\nhttp://josuttis.com/download/std/D2012R0_fix_rangebasedfor_201029.pdf.\nThe ensure that such bugs can't be introduced by mistake, the\nutils::enumerate() function takes an lvalue reference only. The drawback\nis that an loop that iterates over a temporary must now stores that\ntemporary in a named variable before the loop. On a side note, C++20\nwould bring us a workaround to declare that variable within the loop\nstatement. If anyone can think of a clever idea to fix this problem, I'm\nall ears. One option may be to store a copy of the iterable in the\nadapter when utils::enumerate() is given an rvalue, possibly with move\nsemantics, but it may still be costly in some cases, I'm not sure how\ncompilers would optimize that.\n\nLaurent Pinchart (3):\n  libcamera: utils: Add enumerate view for range-based for loops\n  libcamera: utils: enumerate: Use named fields for result\n  v4l2: Replace manual loop counters with utils::enumerate()\n\n include/libcamera/internal/utils.h | 92 ++++++++++++++++++++++++++++++\n src/libcamera/utils.cpp            | 30 ++++++++++\n src/v4l2/v4l2_compat_manager.cpp   | 18 +++---\n test/utils.cpp                     | 59 +++++++++++++++++++\n 4 files changed, 190 insertions(+), 9 deletions(-)"}