From patchwork Tue Mar 31 16:36:30 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 26392 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 15961BDCBD for ; Tue, 31 Mar 2026 16:36:57 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C684262D18; Tue, 31 Mar 2026 18:36:53 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="A6uvPMF2"; 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 7E2B062D13 for ; Tue, 31 Mar 2026 18:36:50 +0200 (CEST) Received: from [100.93.44.16] (net-93-65-100-155.cust.vodafonedsl.it [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id F11E625B5; Tue, 31 Mar 2026 18:35:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1774974928; bh=FegylNAS/ZE8S1oZUyAdmGxPOxc4ElLR/QAsnKcxhNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=A6uvPMF2ndYHCNaiMbSV3nN8kVYk2tILt+nU5/5Z7FVyPL2stpiCfaQKcoF3jl3La KGqKNZmGzKiBsFaEeOAElXTQ/C5bh8LHmoBHkQ/9kkxvbRclNFtBL8ZkWhcViSFJ9Z KiiVHRaDi2pt21XP9iAg/aDTvBVBk9Zntao59sXE= From: Jacopo Mondi Date: Tue, 31 Mar 2026 18:36:30 +0200 Subject: [PATCH v7 2/8] libcamera: utils: Add overloaded visitor helpers MIME-Version: 1.0 Message-Id: <20260331-mali-cru-v7-2-4caedc898a0e@ideasonboard.com> References: <20260331-mali-cru-v7-0-4caedc898a0e@ideasonboard.com> In-Reply-To: <20260331-mali-cru-v7-0-4caedc898a0e@ideasonboard.com> To: Daniel Scally , libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= , Jacopo Mondi 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/ZANAwAKAXI0Bo8WoVY8AcsmYgBpy/gggN1+I9Fr8/70j8mu3wnLf+8qfdNBQgP4a woRfATOSmyJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCacv4IAAKCRByNAaPFqFW PGIJD/4rhei/fdDAGoOqorP4Xw9R9cT2r/bcXQ4Akere6M4GaX3hYZ8g7QB4F4sMgKAD9OhZTPr DuSQ7Jo7vbyyiT6XQQX2JqcmvYfS8uxUwsIr0VexU0u1QNP/yTjoz8sahNURRIsRwrwQfhCJzTd bfiaGhy/sLpZEdOiu20GrPTEMf+lcx1gZvJRWEIUwlRKfEJO8SkiK51xiWI9bp5YRleW/rs/z/Z KzcKWqWX1+Nto5WU03mH5ItDwRvdu8xBtNLdd10tzO0xlebMI4s4TO0raJm+vZZJmEniogKOfS8 KDSKOD5BeMPMEsjThCm50Ubc//CrjCAdJtZnAnJ6NBDynT1LTyCVdkVJZkMsFYviE5dSGPfD8p+ 97FJQtw79s3xjedKECkoUsAUnv3RUK3b2/MjGQcpaDG10qxbQnKSjZiwmUsp3641l/y4aOqDe6g fx90CkAR/az8QdbmG3+mfk4hwjQriNd4y+MSizAE8KutOvC+s14IaYzT2UV3fAH/GtbtlZ0cGwn X76L4JfQ9uhrhhzvb1+qZwK3ynfzn/uTsMu0gHAMvz8ys1aBe2okREqvvUAILUpbkuC+sfQgJv4 090oLkbo0rAtLg4XCvJVQXngubhA9vnDUGt1eeyTLLnFRFjso5vxQJ3E/cxTCo6yeszdGm+6cJq suj3CG/JAJzEDng== 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)