From patchwork Wed Jul 27 02:38:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16824 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 6EECCC3275 for ; Wed, 27 Jul 2022 02:38:38 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1B70963335; Wed, 27 Jul 2022 04:38:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658889518; bh=4ogMDBF/JMuX5fNaBX1VqNSXwS84vBW27QFVWyEaRAo=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=swTyZTAk/g85AfmVOEkBBl6xJFTvWdTBj3N7Qf2mD2aONRdG5fIepNbMPTWhEQ4BF zhmsboIevWEOFArgKTWeHHcAoykAsCLnUg+09TfmyHGoVQ6gp3FyK9tI2kt68Ey9jd nqK39s4/54L1RZg0fI2qJWIke2OPjdBom44EY++9LYB0Rw7JMbGNFw1/FnrTW7eu2S Oi2h0bnuSpNd6heAAj+57xf2HshXwgv8yrRKSar7kbwLrhNVPE1Yvv3ihdnkRZqacM 5B+rKFBP1OLn2HEsboN9pxFitKWV82mm1QqRdZEd1bYYxit0enJIMYnkkqGudUzJpV 5DKImD6OFhyMw== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 85F516332C for ; Wed, 27 Jul 2022 04:38:34 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="c96tIMyG"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1E0F5835; Wed, 27 Jul 2022 04:38:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1658889514; bh=4ogMDBF/JMuX5fNaBX1VqNSXwS84vBW27QFVWyEaRAo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c96tIMyGCLCbNI9n0/P5NFg5ZU3K5e/uDPK3RZG92l7FS9qPTntvgoAv0Kc6zO4yQ IsB+rGF+61m3CoUtWoU04ytzmLtDBKJEZkKXAhPgB//l3ZBcKrMqfVrkJYdgZcVdvK MF/53Lv/lEmdthGzTAujqqerkCOVw5isZ/JmcEt0= To: libcamera-devel@lists.libcamera.org Date: Wed, 27 Jul 2022 05:38:15 +0300 Message-Id: <20220727023816.30008-14-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220727023816.30008-1-laurent.pinchart@ideasonboard.com> References: <20220727023816.30008-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v7 13/14] libcamera: yaml_parser: Add getList() function 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Florian Sylvestre Allow to retrieve a YAML list of any already supported types in a std::vector. Signed-off-by: Florian Sylvestre Reviewed-by: Laurent Pinchart --- include/libcamera/internal/yaml_parser.h | 16 +++++++ src/libcamera/yaml_parser.cpp | 53 ++++++++++++++++++++++++ test/yaml-parser.cpp | 6 +++ 3 files changed, 75 insertions(+) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 9c85d26a2a88..78c359f749bf 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -183,6 +183,22 @@ public: return get().value_or(defaultValue); } +#ifndef __DOXYGEN__ + template || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v> * = nullptr> +#else + template +#endif + std::optional> getList() const; + DictAdapter asDict() const { return DictAdapter{ list_ }; } ListAdapter asList() const { return ListAdapter{ list_ }; } diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 89c234fbbce5..440e35c47cab 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -292,6 +292,59 @@ std::optional YamlObject::get() const #endif /* __DOXYGEN__ */ +/** + * \fn template YamlObject::getList() const + * \brief Parse the YamlObject as a list of \a T + * + * This function parses the value of the YamlObject as a list of \a T objects, + * and returns the value as a \a std::vector. If parsing fails, std::nullopt + * is returned. + * + * \return The YamlObject value as a std::vector, or std::nullopt if parsing + * failed + */ + +#ifndef __DOXYGEN__ + +template || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v || + std::is_same_v> *> +std::optional> YamlObject::getList() const +{ + if (type_ != Type::List) + return {}; + + std::vector values; + values.reserve(list_.size()); + + for (const YamlObject &entry : asList()) { + const auto value = entry.get(); + if (!value) + return {}; + values.emplace_back(*value); + } + + return values; +} + +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; +template std::optional> YamlObject::getList() const; + +#endif /* __DOXYGEN__ */ + /** * \fn YamlObject::asDict() const * \brief Wrap a dictionary YamlObject in an adapter that exposes iterators diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 782331764346..9fd278664b3b 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -518,6 +518,12 @@ protected: return TestFail; } + const auto &values = firstElement.getList(); + if (!values || values->size() != 2 || (*values)[0] != 1 || (*values)[1] != 2) { + cerr << "getList() failed to return correct vector" << std::endl; + return TestFail; + } + auto &secondElement = level2Obj[1]; if (!secondElement.isDictionary() || !secondElement.contains("one") ||