From patchwork Sun Dec 27 10:03:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 10749 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 D9131C0F1A for ; Sun, 27 Dec 2020 10:03:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A32AF60525; Sun, 27 Dec 2020 11:03:46 +0100 (CET) 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="rWm9l1/W"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 69A4B6031F for ; Sun, 27 Dec 2020 11:03:45 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C28BD80B for ; Sun, 27 Dec 2020 11:03:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1609063425; bh=7Ur+R1j9PG8PYbMeeoLgITLBWJ9uwLX93OnqSh+hbQM=; h=From:To:Subject:Date:From; b=rWm9l1/WyhDpEsYF7O1zE5QrBt4a1yoWdGiFX6I2OSwlDlKJNMiebQ23rbFJN8ymP 6pO+b21sRGAW71nJ6RUIfdJ1CsEm4Cdxh7XKUmJcKfPx6Bys+SPbjJDx6b9PKu7TI+ Wp+XTrhsi3SE+UDodIsO2SK/qSxaMpWInrnk8jfU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sun, 27 Dec 2020 12:03:31 +0200 Message-Id: <20201227100331.1347-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: utils: Add reverse adapter for range-based loop 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" Add a utils::reverse() function that creates an adapter wrapping in iterable, such that range-based iteration over the adapter iterates over the iterable in reverse order. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- include/libcamera/internal/utils.h | 27 +++++++++++++++++++++++++++ src/libcamera/utils.cpp | 8 ++++++++ 2 files changed, 35 insertions(+) This will be used in an upcoming patch series, I'm already sending it out to get the idea reviewed. diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index f08134afb5ba..d0146b71727d 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -203,6 +203,33 @@ constexpr unsigned int alignUp(unsigned int value, unsigned int alignment) return (value + alignment - 1) / alignment * alignment; } +namespace details { + +template +struct reverse_adapter { + T &iterable; +}; + +template +auto begin(reverse_adapter r) +{ + return std::rbegin(r.iterable); +} + +template +auto end(reverse_adapter r) +{ + return std::rend(r.iterable); +} + +} /* namespace details */ + +template +details::reverse_adapter reverse(T &&iterable) +{ + return { iterable }; +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index e90375ae115c..c4098a74e0ab 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -464,6 +464,14 @@ std::string libcameraSourcePath() * \return The value rounded up to the nearest multiple of \a alignment */ +/** + * \fn reverse(T &&iterable) + * \brief Wrap an iterable to reverse iteration in a range-based loop + * \param[in] iterable The iterable + * \return A value of unspecified type that, when used in a range-based for + * loop, will cause the loop to iterate over the \a iterable in reverse order + */ + } /* namespace utils */ } /* namespace libcamera */