From patchwork Fri Mar 13 11:33:43 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 26285 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 96ABBBE086 for ; Fri, 13 Mar 2026 11:34:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3B3CC62657; Fri, 13 Mar 2026 12:34:25 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="RCMyNpcb"; 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 37D7A6263D for ; Fri, 13 Mar 2026 12:34:23 +0100 (CET) Received: from [192.168.224.131] (unknown [37.159.92.229]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 844FA103D; Fri, 13 Mar 2026 12:33:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1773401594; bh=zprjRPHJf6qrBYQuTrxKQDqXGcDi5fNentWk7weQpOI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=RCMyNpcbj45DanT51KNshYzuEN7YFT/U6CXcEBoKw8Ai9paG3OcLx5bn+CSA/7vRd rSg4PRNKzGonz1FZoNAGCTMl+bzZCwhOwZrXzWwYmT3vIVaTNzG//zjLjPb3Gej4AF ++2Zhgu+XWHCihFXe7VL9/ftOqNGCjXCXXZDCrTM= From: Jacopo Mondi Date: Fri, 13 Mar 2026 12:33:43 +0100 Subject: [PATCH v4 2/7] libcamera: utils: Add overloaded visitor helpers MIME-Version: 1.0 Message-Id: <20260313-mali-cru-v4-2-c0d9bc8cd8fa@ideasonboard.com> References: <20260313-mali-cru-v4-0-c0d9bc8cd8fa@ideasonboard.com> In-Reply-To: <20260313-mali-cru-v4-0-c0d9bc8cd8fa@ideasonboard.com> To: Daniel Scally , libcamera-devel@lists.libcamera.org Cc: Jacopo Mondi X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4117; i=jacopo.mondi@ideasonboard.com; h=from:subject:message-id; bh=zprjRPHJf6qrBYQuTrxKQDqXGcDi5fNentWk7weQpOI=; b=owEBbQKS/ZANAwAKAXI0Bo8WoVY8AcsmYgBps/Y06wo47K0QwTTk2FCH3z+v5bSgXx/ukxo3P RC64Bd9iCyJAjMEAAEKAB0WIQS1xD1IgJogio9YOMByNAaPFqFWPAUCabP2NAAKCRByNAaPFqFW PLvqEADHmdaMo+P2tCCqeM1fK7GDIcQhWzLFbxD1wUPl3bhGofXGgXqwJB1gTv+wd6SZup3Ovf3 8quBiQiE3PefeOHPAlD2+fy9xtDIUFhmav4UU6vuLSI7slMwlK5Pyj3f0kvWv7b1e5irQ3IGsyB +lkefw47H7HPESviBBcTvMyiuWCYhRgZYgLoi+SDxvS9oKCjvFV53WIAc5fc90yRnU4/NFurC+W UrJkUWmAmRc25qF8hEnoGHUrZLE00+vsJwNWoVEfA+/ZEZsztt6VatD1uThD6ers3nfrhlAvF8H cMYeJTftO5yHdUKfytBmW9W9MRk9XMa2FARj6J3vB9icxiWloZg36Iy7obe6wiNHcEMX6JFt5DA hoTnBP9sqy/6c7QfUWg8b0LW6R32hSp8ovFHd5k+RzzCgz+RVShLTGzOo5KmJ2yg5eV+a57ev5q Na17jz2EIIs1zjN91JKvQ6p8MoqN0ZfKbiMlqUNUSJ6LGGyhacBDpLv/hms9ZeqresVyBa6zdfD D3R999C1G8OICXd/mABL1SpOQtpB54PbqFBi5KbcrGL+T4m07TECINtuXw6CWjckNj2bhlPh9xY XzROWCH6xzDeLQPcBXDGuthLJuB/EF9ARYQmyKgnZ/6zLRn6yzkyifbFXJ86SUMIJoJE/8ielYN jDKSPGmp0/s9hqw== 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 overaloded 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 types to libcamera::utils for easier re-use. Signed-off-by: Jacopo Mondi Reviewed-by: Barnabás Pőcze --- 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..a18ca82e8d07 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 + * implementations of \a operator(). + */ + +/** + * \var overloaded(Ts...) -> overloaded + * \brief Overloaded visitor class for type-matching std::visit implementations + * \tparam Ts... Template arguments pack of visitor functions + * + * std::visit allows quite elegant type-matching implementation of the visitor + * pattern. 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(). + */ + /** * \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)