From patchwork Wed Mar 25 14:44:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 26336 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 D9DE7C32DE for ; Wed, 25 Mar 2026 14:44:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EDA6A6274D; Wed, 25 Mar 2026 15:44:22 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="c1lJI5tQ"; 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 5DD046274D for ; Wed, 25 Mar 2026 15:44:19 +0100 (CET) Received: from [192.168.1.104] (net-93-65-100-155.cust.vodafonedsl.it [93.65.100.155]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5DF011943; Wed, 25 Mar 2026 15:43:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1774449781; bh=FegylNAS/ZE8S1oZUyAdmGxPOxc4ElLR/QAsnKcxhNE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=c1lJI5tQfvmoapB3LC2CgETRqulLpb2O2rig4M7SI/9/nH5WeZUSwq69kVMo9yXEt DcPvR0LIlMykC4JCkEQqYEyzNANLSJ1qY4UfiK6ChNEkrFhM2wlKldQ7Io917SLfAW jlwaNAg+LvP2jtEl8JahihC4XEU8RX8RVuJ2Wiao= From: Jacopo Mondi Date: Wed, 25 Mar 2026 15:44:09 +0100 Subject: [PATCH v6 2/7] libcamera: utils: Add overloaded visitor helpers MIME-Version: 1.0 Message-Id: <20260325-mali-cru-v6-2-b16b0c49819a@ideasonboard.com> References: <20260325-mali-cru-v6-0-b16b0c49819a@ideasonboard.com> In-Reply-To: <20260325-mali-cru-v6-0-b16b0c49819a@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=kA0DAAoBcjQGjxahVjwByyZiAGnD9MGjgZHy5H2qMyHMYnglPixqilbRwVyt8+W3tWKVmsXDF okCMwQAAQoAHRYhBLXEPUiAmiCKj1g4wHI0Bo8WoVY8BQJpw/TBAAoJEHI0Bo8WoVY84B0P/j7j ZFXkLrMkFZxfvIL9DzuHjfmSN+ACjnl68A+tr8PYBjYnUNPbwNKs4K+YNzTHL4csNQlRculBG5B Cyk0Zo37cBUMz7VUb1DwLqe2n2TM+TmSksSS0hreyGioz4Elma8zPGnXiRxW+muVh+kdeOf70SU ihnfQ0P6YPeV/fIpw8RvDr6l+fO49aCS+b0Cqu1+37K+ks8+Br4Q1RwLjcp/4ht2Uq+xPg2XRaI RmIWORs3kc6NSwqNeNTkQpS84wYAdp5zPL72Umm5N5s5GxheDfFE/xs1T9BNKPLwB0EAXSN50Ob p22i12A5JnPH75CcaXV9W6QAAQNjY11TCqwnE5QMzTcttu5Tciqh1ha8brBuHhviQR8byOBlkLH sqXBf2uOsTUx2DukF+p1gctkTxy2NhAOlXMKFtCLs7KnmQX22rnGOrAhvgtENW26FXU3SDaQ/fV nxUxa1UBM7U+dFCMImgzRO3Vy9CX446+pqGgN6L7kZpDttBG3gJ346iZtYOrGcTb7AHT6mnUIpb rFbrwCaQ/my8rXsRupwKlQDZMvlGTUa147GwPizeETO4MCTj1mSxkcLwX+3D6hwO8I682GRN5oZ J7K8/uiXi4o3wxYELRlMsnxHNoFlrB+oLC/5H1XcGrngg6yrTccTbWb2qmjJ3BdWGbIda5JltNq ZziZ6 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)