From patchwork Fri Mar 13 16:14:36 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 26293 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 1EA2BC32EF for ; Fri, 13 Mar 2026 16:14:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D9A7262703; Fri, 13 Mar 2026 17:14:47 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="hJJH6skD"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 40C6E62695 for ; Fri, 13 Mar 2026 17:14:46 +0100 (CET) Received: from [192.168.224.131] (unknown [37.159.122.93]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 84A32E70; Fri, 13 Mar 2026 17:13:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1773418417; bh=FegylNAS/ZE8S1oZUyAdmGxPOxc4ElLR/QAsnKcxhNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hJJH6skDqHReuyo0FSM+y6SKefdwe3jelp0UB5pcjtkJXPjg30R8Uyh85ECEYyUxV gmv1q5+93gZhjeJAkhPrWCKT6aYFR8V4ElGV35PZY9Kd1EiUaG7wtV5iX02SvxRbwn BhIDjTXKz2y8KC0V5xgFUSB36yyXApr3KOpGqbiY= From: Jacopo Mondi Date: Fri, 13 Mar 2026 17:14:36 +0100 Subject: [PATCH v5 2/7] libcamera: utils: Add overloaded visitor helpers MIME-Version: 1.0 Message-Id: <20260313-mali-cru-v5-2-48f93e431294@ideasonboard.com> References: <20260313-mali-cru-v5-0-48f93e431294@ideasonboard.com> In-Reply-To: <20260313-mali-cru-v5-0-48f93e431294@ideasonboard.com> To: Daniel Scally , libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi , =?utf-8?b?QmFybmFiw6Fz?= =?utf-8?q?_P=C5=91cze?= X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4147; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=FegylNAS/ZE8S1oZUyAdmGxPOxc4ElLR/QAsnKcxhNE=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBptDfyxMGFcVvnzbZWAtJsA0zxYr5YnAHMwzpAc XRcjLPr4jKJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCabQ38gAKCRByNAaPFqFW PBwnEACXOfwUsFkCcGkNTTosPRvJsmU6YsOFAR68NOgoAKprBKNsRXVavn8D/XdSTXtwUHWpxBX BdyQnI4Re0ATaLfV/vqqA+IDLVXootjCvi3o+q6Sr+PuCVbVelNJUkCXpfMDgA7TIpBO3VK5rUE NobCIWdH/83Z2niRwZv3+dN6otjMiOm+AGgeNTutVfxjwpnDA1xndYYPskWG1qOKvpW5Y+ebJl8 d7PEnF1tQ8wLGT7fg1u8RK0sShSdYoQ09Xyfng8EvRFyO4EWlYrcxrfB8YW2uWY+5gdWhGT6AOs u38jLV1wnkWHwFLNJSwc7OTWTSwTRfvabmlI2eJxMJ3jlsNGPuyvzlDHKNERrzZ81MtkvanuClH 1QjW4rjgc0dQfpcF29hMGfq8Z9TP+LIFADzj1kgwzB6iyTYbMRVwvdJjdDNDGbt7jJB4zUjdHMu /6Y2h25SRsS4WcaZxxyU/B713+z9zwDKgakuXYb23pGvRkkhv6G2bwY3OOsIA/9FOSaDOtYhCuw wfuCyP5L8UARNb+8NTblEPRnDbxkNJ5z8ndrlbxd6u+Vod0AciqZn/6c3UMhbD7ua1ksBHapkgr 5fq1PuN/eOCs38RRKD3iTf+tf3pTnkfpo8KMvBPKGb582gww2mD1BeFCHtt3EBUXZNnJqoPfP1d HQEHbKyb5moQy4g== X-Developer-Key: i=jacopo.mondi@ideasonboard.com; a=openpgp; fpr=72392EDC88144A65C701EA9BA5826A2587AD026B 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" std::visit() allows quite elegant type-matching implementation of the visitor pattern. The 'overloaded' type helpers allow to define a hierarchy of overloaded operator() implementations which can be used by std::visit(). Currently only the Virtual pipeline handler uses this type-matching implementation of std::visit. To prepare to add another user in the Mali C55 pipeline handler move the 'overloaded' helper type to libcamera::utils for easier re-use. Reviewed-by: Barnabás Pőcze Signed-off-by: Jacopo Mondi --- include/libcamera/base/utils.h | 7 ++++++ src/libcamera/base/utils.cpp | 35 ++++++++++++++++++++++++++++++ src/libcamera/pipeline/virtual/virtual.cpp | 10 ++------- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 7083b7ce9ce9..62c7f89c25a1 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -37,6 +37,13 @@ namespace libcamera { namespace utils { +template +struct overloaded : Ts... { + using Ts::operator()...; +}; +template +overloaded(Ts...) -> overloaded; + const char *basename(const char *path); char *secure_getenv(const char *name); diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 42a516097be2..836aec87c539 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -23,6 +23,41 @@ namespace libcamera { namespace utils { +/** + * \struct overloaded + * \brief Helper type for type-matching std::visit implementations + * \tparam Ts... Template arguments pack of visitors + * + * Expand the template argument pack \a Ts... to provide overloaded \a + * operator() to support type-matching implementations of the visitor design + * pattern using std::visit. + * + * An example is provided by the STL documentation in the form of: + * + * \code{.cpp} + * template struct overloaded : Ts... { using Ts::operator()...; }; + * template overloaded(Ts...) -> overloaded; + * + * using var_t = std::variant; + * std::vector vec = {10, 15l, 1.5, "hello"}; + * + * for (auto& v: vec) { + * std::visit(overloaded { + * [](auto arg) { std::cout << arg << ' '; }, + * [](double arg) { std::cout << std::fixed << arg << ' '; }, + * [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, + * }, v); + * \endcode + * + * Use this helper to implement type-matching visitors using std::visit(). + */ + +/** + * \var overloaded(Ts...) -> overloaded + * \brief Deduction guide necessary for C++17 compatibility + * \tparam Ts... Template arguments pack of visitor functions + */ + /** * \brief Strip the directory prefix from the path * \param[in] path The path to process diff --git a/src/libcamera/pipeline/virtual/virtual.cpp b/src/libcamera/pipeline/virtual/virtual.cpp index efd800ebe3d6..e8ef7e524ccf 100644 --- a/src/libcamera/pipeline/virtual/virtual.cpp +++ b/src/libcamera/pipeline/virtual/virtual.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -57,13 +58,6 @@ uint64_t currentTimestamp() } /* namespace */ -template -struct overloaded : Ts... { - using Ts::operator()...; -}; -template -overloaded(Ts...) -> overloaded; - class VirtualCameraConfiguration : public CameraConfiguration { public: @@ -428,7 +422,7 @@ bool PipelineHandlerVirtual::initFrameGenerator(Camera *camera) { auto data = cameraData(camera); auto &frame = data->config_.frame; - std::visit(overloaded{ + std::visit(utils::overloaded{ [&](TestPattern &testPattern) { for (auto &streamConfig : data->streamConfigs_) { if (testPattern == TestPattern::DiagonalLines)