From patchwork Fri Jul 22 15:16:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Sylvestre X-Patchwork-Id: 16734 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 AE590BD1F1 for ; Fri, 22 Jul 2022 15:16:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 675BE63318; Fri, 22 Jul 2022 17:16:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658503007; bh=pMzNI7g/tFbiOPklsa+5EIbxGf0di9GSiColgaj2RLM=; 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=2SRKZKqeSL7aDQTk94mb8Fy/3Pfu048wC9+ehaGDVA3W3wuFoarczA8pG5mcvK6sx JA9hKOj8EhkDO0D7hmpsRu2Qv9AoIrjSuXRNR3CcuqD964ItlgQuHPcJW6HiOKGss8 q4kR7j3qoGsXZg4bbRj5zT5Lxrp5ceZOlfB6u1joS+fX8yNPK6ylJACKvZFFY3A6OS PaGR7sdemHxHuPOKVTZHmsaZHqNruiQBse7CDnLyjxBYytqiEdP1EQvrdozWnIQzoC VVK8fXiJvDkQ09RnfRE9Se9FpdcIz+YGXJZjI8OTKeZqxHk2gWEgyTePW7qgsuJwOX v4fWfF7OgR1iA== Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CD7C863313 for ; Fri, 22 Jul 2022 17:16:44 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="gX0b9CIz"; dkim-atps=neutral Received: by mail-wr1-x42a.google.com with SMTP id h9so6979995wrm.0 for ; Fri, 22 Jul 2022 08:16:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=m7hUmiaKzanMb2eUzI916OaV1LitMalvbU40wiWSryc=; b=gX0b9CIzCIJV45UAC8oEJpx39U6HMCh1mTKKpfvs+fJjsULfE8JKLLSW9yzfz1FleI oauBy7kbxLMxlHoAqBo+r5KJyqNKN2D/6yVxAW1zZ82GTyip4F2yK2P2Jp4SGawknCsP SiYNuMnb0SQqWFIm3YTh6+7Q1rcqGZXuYYrIDI5VR/nhci6z1a4BmJjL5IVC7Qs7e4IG IHtLQEPdlLzW/hE2rnaVRYNMotSKvst9HFYw2ix1aAtYytlFaUFMoU+a3Vk4nC91z6Lp ThJK8eVORRGii4ykUp5UyIv981AZGMYuscG7g8IY4crhuLdhlJIQGNVen+MH6qUfkT1M 3jYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=m7hUmiaKzanMb2eUzI916OaV1LitMalvbU40wiWSryc=; b=OrFdEdRUysDLiI9iMLBl7IlHnkKZahGUndHv1sZYbU7U8YW9pn37IJGQEQQd2tN2di M4F6UaRgLtSybnJeN1jrqOfUB7P71+r4mHEnlIUkn6s4fDPPWDbDls58q0pvd16y0URs Sp0dVG3CaDEOSEPeYAkmIMXuT2WM6KxcIVBvgwwXQXMRyrXheesb1FOhlGKEKudM3/mf yk170jnwHmQoEADpkULc8AQiE7YrrU79SX0+Z1qktPvXo1FjZFaxDhSpKWWuwhYutzvE XhmFyY7dPBG2RVuTIc8xjQsyctbqqBl3KmVH4JAE8pcSKjHWc0iHrBl143Rj5HhnSeZa chSA== X-Gm-Message-State: AJIora/HlT6mYxgRCGHyz92xCIOa5IpKsuG75tGEnIDXKQsMVuOimzIk pvaR6TS33r2c/M1Zt3l8CVzns50Wf2iYoQH+ X-Google-Smtp-Source: AGRyM1uMTXsACDZrKMEsZHAxrFkRR610oGE/gyz72LdNVr0x0QLvhkERpQCsPH8TIgoE0I/HonxjPg== X-Received: by 2002:a5d:5847:0:b0:21d:b75e:12d0 with SMTP id i7-20020a5d5847000000b0021db75e12d0mr294379wrf.208.1658503004069; Fri, 22 Jul 2022 08:16:44 -0700 (PDT) Received: from BL087.. ([2a01:e34:eea9:e630:e9da:4076:6aa:6cd2]) by smtp.gmail.com with ESMTPSA id b1-20020a5d4d81000000b0021e4fd8e10bsm4871557wru.11.2022.07.22.08.16.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 08:16:43 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 22 Jul 2022 17:16:31 +0200 Message-Id: <20220722151635.239221-2-fsylvestre@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220722151635.239221-1-fsylvestre@baylibre.com> References: <20220722151635.239221-1-fsylvestre@baylibre.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/5] 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: Florian Sylvestre via libcamera-devel From: Florian Sylvestre Reply-To: Florian Sylvestre Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Allow to retrieve a YAML list of any already supported types in a std::vector. Signed-off-by: Florian Sylvestre Reviewed-by: Laurent Pinchart Reviewed-by: Paul Elder --- include/libcamera/internal/yaml_parser.h | 16 ++++++ src/libcamera/yaml_parser.cpp | 66 ++++++++++++++++++++++++ test/yaml-parser.cpp | 6 +++ 3 files changed, 88 insertions(+) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 064cf443..39161bbb 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -167,6 +167,22 @@ public: #endif T get(const T &defaultValue, bool *ok = nullptr) const; +#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::vector getList(bool *ok = nullptr) 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 5c45e44e..125a348e 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -325,6 +325,72 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const #endif /* __DOXYGEN__ */ +/** + * \fn template YamlObject::getList(bool *ok) const + * \brief Parse the YamlObject as a list of \a T + * \param[out] ok The result of whether the parse succeeded + * + * 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 \a ok is set + * to false and a default empty vector is returned. Otherwise, the + * YamlObject list of values is returned, and \a ok is set to true. + * + * The \a ok pointer is optional and can be a nullptr if the caller doesn't + * need to know if parsing succeeded. + * + * \return Value as a std::vector type + */ + +#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::vector YamlObject::getList(bool *ok) const +{ + setOk(ok, false); + + if (type_ != Type::List) + return {}; + + std::vector value; + value.reserve(list_.size()); + + /* + * Provide somme dummy defaultValue to get() function and rely on the + * ok variable to know if the parsing was correct. + */ + T defaultValue{}; + + for (const YamlObject &entry : asList()) { + bool result; + value.emplace_back(entry.get(defaultValue, &result)); + if (!result) + return {}; + } + + setOk(ok, true); + return value; +} + +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) const; +template std::vector YamlObject::getList(bool *ok) 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 38f84823..bb96a11b 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -524,6 +524,12 @@ protected: return TestFail; } + std::vector values = firstElement.getList(); + if (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") ||