From patchwork Wed Jul 27 08:40:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Sylvestre X-Patchwork-Id: 16832 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 F2589BE173 for ; Wed, 27 Jul 2022 08:41:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A34C563316; Wed, 27 Jul 2022 10:41:01 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658911261; bh=/pk7XbtkUYawi0j+LgzQKVNqxB0MC9D4ZRM3xUTk++c=; 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=Vo+7mdSfuzbcxiXuDmsTQcVVX4OzV1Lg6XNfC5hc92RMu04dnqsVHZ9lBUUdIgUI3 VP3+7rmKZaKUIitRIhrnhX9de3F3zqhJas6YDHCn5lz5n1GiloCWPIGTpdrBbJcZOS 7FHthtSlWaWPbFZnK+L71JAVYVjPHJtOV1wWadELjJHF/YsMViArEeXavWyFCzKWmN uI1OZNfqQKSmDGWiW67MtDIwUv843pMGsqeMGKCUQ/HxI6cxkvlg299f47gsEByQl8 IESw17BwZUzw8bIkmxjvOgMDBB+jqC1rOyUVJ1O4C/gajVA/FdW63RRaZcaBV1GVZk diQkS8+HnDODQ== Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 782CA6330E for ; Wed, 27 Jul 2022 10:40:59 +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="UIbIWAej"; dkim-atps=neutral Received: by mail-wr1-x432.google.com with SMTP id v17so4256780wrr.10 for ; Wed, 27 Jul 2022 01:40:59 -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=K8KcnQjbTB/EoE6YznZHrFHtKHxZ0/IUQtG7YIMQdnY=; b=UIbIWAejOPVxG8hVfMMKNF5pOgskuWsjXEqlarm9J3DvMJNWxERJpz9Z1K9zHyEcy9 vCMbqdh959vDANbFdSO2o25Jyilvt38hZRcvmtou00IYyN8BNVK7uu23Zl8ICfSAizwF HzhHpKwT5XoEEH74XiUbTWv91R13rwR662pzha+pIxeOt94OWue9o+X4zgF1+RQ/ij72 BwGMOaMNsrhBrMyj4XjNzvKu7Ke2uWCxDVBb1E4JjYVnM+vSK8g7RUxqmYm/hYyHp0VX xaMrTpwcNIEM7t9ZYNlc4PsKRCUcPt48LCdj3EZPGq6ppxx60zCgqLE8X0Iuwv7owStn PsqQ== 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=K8KcnQjbTB/EoE6YznZHrFHtKHxZ0/IUQtG7YIMQdnY=; b=B1DnWDYcUGYOSFO0OqAd1zVlT1RO7KGWniQHIdEN5TIRw3DP3CfLfkBoCMMJqdYr4a u1JZEM4DNSvbC4qZVkF6tcLS+Kw5K0I8MovD7XI7osY09a+sIxnZuC+0PFm/Wf28RG2d pj9G2swMWVzF1hI3rqNZ23BM1g2mBD1ip1iWJuvJ6tvxxl0yUnZvs+gv4rQx9GMY7cgi hHWdHMwCgHbthvV5T1LeAnjVR9MORL0n/PTBqlb1gB/bWV739xKw6/Xzks8YRy8mnLr5 e1GwAWNqbDXVO3Erde+o+dUpt1Cey4djceUx8ooo/k0w/fjSaqokIA+9dn83Lid8+1eg aqLw== X-Gm-Message-State: AJIora8rexcssjTZ1qA6ZJiKmzx7o/8dq+q5RDi3qlAPZtgu5QNYxb9u kKopsF9tXQ54QBSMOPKwNHCpqRS+JGSIeBmF X-Google-Smtp-Source: AGRyM1terR/08uqRIyMhMTTpkXqT7zppAfaYqEEaCjS2HwdPh9Bw8oH0blQINFbn20ATQbNsrrGyQw== X-Received: by 2002:a05:6000:15c6:b0:21d:a96b:8ffd with SMTP id y6-20020a05600015c600b0021da96b8ffdmr14154970wry.610.1658911258568; Wed, 27 Jul 2022 01:40:58 -0700 (PDT) Received: from BL087.. ([2a01:e34:eea9:e630:5810:316e:87c:fc17]) by smtp.gmail.com with ESMTPSA id 16-20020a05600c241000b003a2f6367049sm1464275wmp.48.2022.07.27.01.40.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Jul 2022 01:40:58 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 27 Jul 2022 10:40:48 +0200 Message-Id: <20220727084052.590421-2-fsylvestre@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220727084052.590421-1-fsylvestre@baylibre.com> References: <20220727084052.590421-1-fsylvestre@baylibre.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 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") ||