From patchwork Wed Jul 27 22:21:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16851 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 F35DBBE173 for ; Wed, 27 Jul 2022 22:21:59 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B9D7E6331E; Thu, 28 Jul 2022 00:21:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658960518; bh=Ey/FSnyl3Sgk/a2SI/x0+AuB8Vd2N7heHyg8oOf6nP4=; 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=ZTQLjcW+An9kVFkAF7nHvIotN0q7D15kEFLUw4I9B6Fviet1bIGvn7bm+L3jsUDcY 6sne1sPs/b3020kl20Twxf2E2F2yzd0yFyx74vnynkk2lgQOo++0jlpaRkQmhVhbB8 S1AD1opFnR+hllOvkXT6CkULOM0GVbYDd1XnSSAw0TXg0O4s7s/qvvvN/hPmgLW5n/ EdQa9ehHLqTXGJrrQ1W0GtoR3LGULZ6rzgX9gFQbV4VdAYCQvT41AwBT1vMltuxQoT VddbbLw5ob1AFDpQqp6P3TrGa2EgjpqVp8q13qCuNiaxnm1vqttA2bRKT8RgwqS65x SUi1gxmLQcpJQ== 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 D920963310 for ; Thu, 28 Jul 2022 00:21:55 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ATjAD03b"; 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 6DB23835; Thu, 28 Jul 2022 00:21:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1658960515; bh=Ey/FSnyl3Sgk/a2SI/x0+AuB8Vd2N7heHyg8oOf6nP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ATjAD03bmCu2HQ3XN8v6lvYZ9Xp/E2iR89mhOTnkOlA8PMaqpwmVy4yuarmiPB68C BJ6Jg4OXv67iKxG/34c2oIEV9fiKuLb19mFHPKFv2DZR4FjbOea9Lt9j3ViMGSeQOt gYxDO6pc15qd0AH4vhN85eMFbaJ0MH5aQ9T1A5fU= To: libcamera-devel@lists.libcamera.org Date: Thu, 28 Jul 2022 01:21:43 +0300 Message-Id: <20220727222149.30627-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220727222149.30627-1-laurent.pinchart@ideasonboard.com> References: <20220727222149.30627-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v5 3/9] 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 Tested-by: Naushir Patuck Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi --- 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 61f2223223a7..4b3da0f03512 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -174,6 +174,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{ dictionary_ }; } ListAdapter asList() const { return ListAdapter{ list_ }; } diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 4299f5abd38a..cb4f1eb29a77 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -293,6 +293,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 ebb654f2ef9c..bb54e0d7f76a 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -523,6 +523,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") ||