From patchwork Tue Jul 26 14:36: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: 16796 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 CC9F3C3275 for ; Tue, 26 Jul 2022 14:36:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 83D5563317; Tue, 26 Jul 2022 16:36:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1658846204; 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=dL/TwfMSRkChwZsEDipfDx7sU3VzsMPCT2E9QTotPZrL39W3+ZJUdshPBDye87ZZY 2nPMhi56zM8v/FGDkA96rh1XPHIg7AdRY0pDSCJJc5zYTwPPQsnssw7F+iDTNYFCkc VjBufK/GyKt6cWHF99gGfLQkKiHAaR7v6gtpcovMyqahbgv0sJ2lf700qpeLUsoyER /e5wjC102dsQD+Uy4pRTeSKwU596xfy9VIEn/n29YgQYptO4z37rooj/Qnf3AIvm8D ACsNQpN+SOGh+TTqb2BKbsJFvNxEsfO5bXi3+MGhofu8Zr9AYlicuYo+4QP6QPhWKe jb5hf/xMUNuVA== Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5592760487 for ; Tue, 26 Jul 2022 16:36:41 +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="q8sKAL3V"; dkim-atps=neutral Received: by mail-wm1-x333.google.com with SMTP id f24-20020a1cc918000000b003a30178c022so11466186wmb.3 for ; Tue, 26 Jul 2022 07:36:41 -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=q8sKAL3VHLZ+NStg4Ilpqy3UCYTLd/9viY1WBZwHiNKPODj722mJ5avFu0A97r+Su6 agCSQkDa6wHB/qtBHnoVYyhM+jikmhrF2DcadFn5ea/b8L+DdOYiBaQlhYZ5xzVi4xH1 eXD4Zoenrd9ORMHw0h/HXPv5MH1DooTK+3PurlevKy8Nr0qG46Y16Oq/MaIa9uHVQgN8 uvQ5eCjjGUMXMB0U7W01R7fmYAqqasg0Ouk8puHdkgm2v1eLX9aFh/cB5dRuYxc/zAdc nGY53ZyXMUr4WPOVi1KJhrBy+dl3KA4Sg1itj7/Nw4xjjAwWIxqDWWLyoQ6izpKoSWUr +6gw== 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=k2A9U130roJbohcGQ0B7THn51ERMTiymvRm1STfhCcwUSt17heL2KJThrQr3vsUAys 5foO0SAXJ4nCwH3JpxZFmZOK7+OkauNYLkisGKltVaUIa831/0ss1aIASqD6FeGYxzwQ VwVL2qcG1RSJh8zpAfmvrCMwjvAxpfIB8SEHir2/74zPE0E/JJfb4y1lGDkTSuHCeXlR j+g6BEC8Z09Kj6zsp5j/WW1Ql7Wue7vAFczJ02qQ9fW+Pf3QuUyidyXLXHBMTQihgvlO NO3oDsS5utLCVtz8EkOQLiYCFqT9HDs0gpwv3Y3Q+BzwoyAi7pB4TU8YnICSt6c3FfzZ YiRw== X-Gm-Message-State: AJIora8nXmn3dGT2XcVysno4/WlhxbNFR8kk5e+vzzVLuRmrLPSrVH0s LX03aRQemaVGpwCEkV7KCmdCM2/1oOKyX3jj X-Google-Smtp-Source: AGRyM1t548SvY6kwjYjp/tf5lzhT/HnYf4FXqI/8UMzchJ1Ygmrwu2UjTJgnB37nzhD68S9+rNA3Kw== X-Received: by 2002:a05:600c:3d93:b0:3a3:3a93:fb16 with SMTP id bi19-20020a05600c3d9300b003a33a93fb16mr16946782wmb.190.1658846200504; Tue, 26 Jul 2022 07:36:40 -0700 (PDT) Received: from BL087.. ([2a01:e34:eea9:e630:c7b2:2183:8a06:b4dc]) by smtp.gmail.com with ESMTPSA id m184-20020a1c26c1000000b003a04722d745sm22447386wmm.23.2022.07.26.07.36.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 07:36:39 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Tue, 26 Jul 2022 16:36:31 +0200 Message-Id: <20220726143635.518227-2-fsylvestre@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220726143635.518227-1-fsylvestre@baylibre.com> References: <20220726143635.518227-1-fsylvestre@baylibre.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 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") ||