[{"id":24199,"web_url":"https://patchwork.libcamera.org/comment/24199/","msgid":"<YuGrbW86UnnpbWTc@pendragon.ideasonboard.com>","date":"2022-07-27T21:17:33","subject":"Re: [libcamera-devel] [PATCH v4 1/5] libcamera: yaml_parser: Add\n\tgetList() function","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Florian,\n\nOn Wed, Jul 27, 2022 at 10:40:48AM +0200, Florian Sylvestre wrote:\n> Allow to retrieve a YAML list of any already supported types in a std::vector.\n> \n> Signed-off-by: Florian Sylvestre <fsylvestre@baylibre.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\nI've rebased this patch on top of \"[PATCH v7 01/14] libcamera:\nyaml_parser: Replace ok flag to get() with std::optional\". I'll send a\nv5 of your pending series rebased on top of that change.\n\n> ---\n>  include/libcamera/internal/yaml_parser.h | 16 ++++++\n>  src/libcamera/yaml_parser.cpp            | 66 ++++++++++++++++++++++++\n>  test/yaml-parser.cpp                     |  6 +++\n>  3 files changed, 88 insertions(+)\n> \n> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\n> index 064cf443..39161bbb 100644\n> --- a/include/libcamera/internal/yaml_parser.h\n> +++ b/include/libcamera/internal/yaml_parser.h\n> @@ -167,6 +167,22 @@ public:\n>  #endif\n>  \tT get(const T &defaultValue, bool *ok = nullptr) const;\n>  \n> +#ifndef __DOXYGEN__\n> +\ttemplate<typename T,\n> +\t\t typename std::enable_if_t<\n> +\t\t\t std::is_same_v<bool, T> ||\n> +\t\t\t std::is_same_v<double, T> ||\n> +\t\t\t std::is_same_v<int16_t, T> ||\n> +\t\t\t std::is_same_v<uint16_t, T> ||\n> +\t\t\t std::is_same_v<int32_t, T> ||\n> +\t\t\t std::is_same_v<uint32_t, T> ||\n> +\t\t\t std::is_same_v<std::string, T> ||\n> +\t\t\t std::is_same_v<Size, T>> * = nullptr>\n> +#else\n> +\ttemplate<typename T>\n> +#endif\n> +\tstd::vector<T> getList(bool *ok = nullptr) const;\n> +\n>  \tDictAdapter asDict() const { return DictAdapter{ dictionary_ }; }\n>  \tListAdapter asList() const { return ListAdapter{ list_ }; }\n>  \n> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\n> index 5c45e44e..125a348e 100644\n> --- a/src/libcamera/yaml_parser.cpp\n> +++ b/src/libcamera/yaml_parser.cpp\n> @@ -325,6 +325,72 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const\n>  \n>  #endif /* __DOXYGEN__ */\n>  \n> +/**\n> + * \\fn template<typename T> YamlObject::getList<T>(bool *ok) const\n> + * \\brief Parse the YamlObject as a list of \\a T\n> + * \\param[out] ok The result of whether the parse succeeded\n> + *\n> + * This function parses the value of the YamlObject as a list of \\a T objects,\n> + * and returns the value as a \\a std::vector<T>. If parsing fails \\a ok is set\n> + * to false and a default empty vector is returned. Otherwise, the\n> + * YamlObject list of values is returned, and \\a ok is set to true.\n> + *\n> + * The \\a ok pointer is optional and can be a nullptr if the caller doesn't\n> + * need to know if parsing succeeded.\n> + *\n> + * \\return Value as a std::vector<T> type\n> + */\n> +\n> +#ifndef __DOXYGEN__\n> +\n> +template<typename T,\n> +\t typename std::enable_if_t<\n> +\t\t std::is_same_v<bool, T> ||\n> +\t\t std::is_same_v<double, T> ||\n> +\t\t std::is_same_v<int16_t, T> ||\n> +\t\t std::is_same_v<uint16_t, T> ||\n> +\t\t std::is_same_v<int32_t, T> ||\n> +\t\t std::is_same_v<uint32_t, T> ||\n> +\t\t std::is_same_v<std::string, T> ||\n> +\t\t std::is_same_v<Size, T>> *>\n> +std::vector<T> YamlObject::getList(bool *ok) const\n> +{\n> +\tsetOk(ok, false);\n> +\n> +\tif (type_ != Type::List)\n> +\t\treturn {};\n> +\n> +\tstd::vector<T> value;\n> +\tvalue.reserve(list_.size());\n> +\n> +\t/*\n> +\t * Provide somme dummy defaultValue to get() function and rely on the\n> +\t * ok variable to know if the parsing was correct.\n> +\t */\n> +\tT defaultValue{};\n> +\n> +\tfor (const YamlObject &entry : asList()) {\n> +\t\tbool result;\n> +\t\tvalue.emplace_back(entry.get<T>(defaultValue, &result));\n> +\t\tif (!result)\n> +\t\t\treturn {};\n> +\t}\n> +\n> +\tsetOk(ok, true);\n> +\treturn value;\n> +}\n> +\n> +template std::vector<bool> YamlObject::getList<bool>(bool *ok) const;\n> +template std::vector<double> YamlObject::getList<double>(bool *ok) const;\n> +template std::vector<int16_t> YamlObject::getList<int16_t>(bool *ok) const;\n> +template std::vector<uint16_t> YamlObject::getList<uint16_t>(bool *ok) const;\n> +template std::vector<int32_t> YamlObject::getList<int32_t>(bool *ok) const;\n> +template std::vector<uint32_t> YamlObject::getList<uint32_t>(bool *ok) const;\n> +template std::vector<std::string> YamlObject::getList<std::string>(bool *ok) const;\n> +template std::vector<Size> YamlObject::getList<Size>(bool *ok) const;\n> +\n> +#endif /* __DOXYGEN__ */\n> +\n>  /**\n>   * \\fn YamlObject::asDict() const\n>   * \\brief Wrap a dictionary YamlObject in an adapter that exposes iterators\n> diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp\n> index 38f84823..bb96a11b 100644\n> --- a/test/yaml-parser.cpp\n> +++ b/test/yaml-parser.cpp\n> @@ -524,6 +524,12 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> +\t\tstd::vector<uint16_t> values = firstElement.getList<uint16_t>();\n> +\t\tif (values.size() != 2 || values[0] != 1 || values[1] != 2) {\n> +\t\t\tcerr << \"getList() failed to return correct vector\" << std::endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n>  \t\tauto &secondElement = level2Obj[1];\n>  \t\tif (!secondElement.isDictionary() ||\n>  \t\t    !secondElement.contains(\"one\") ||","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C46C5C3275\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Jul 2022 21:17:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4006763311;\n\tWed, 27 Jul 2022 23:17:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E7D0863309\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Jul 2022 23:17:35 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 30AFA56D;\n\tWed, 27 Jul 2022 23:17:35 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658956657;\n\tbh=3qrItgIl0B4PVJ7BihzbatC+fYoXoqpT/2iQ3pExEHs=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=V+RpZMfr0U1MyWqjksA6EWJihdXhYueOmEcdc8NpGLSoYHHsscXhmdiOPvXbbnbUC\n\tdcWD/ufC+KUcAWYOHIKfuoKh9R+0XTN9IvKwUNjsnkB9CqLI/IXN858+/HBJyHbdL8\n\tZQZR506e2M60UIru624Ke2AM45g6q06cbYwo62b6F+B7TRStMlTQnVocDe7N4YjnAO\n\tlxS59nBTCj8yOmGrBKuVvjVusG9bwUXAq0sBFhy1in1sbD0jgeOZN+IG506GYiyI2S\n\txHmoewA6IKEo67VUKM+L+NKNq751KSA+5tGGbsTMnbVJL6TKH9dC1nT9Fokl52J8r+\n\tYmND2K8NCD2tA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1658956655;\n\tbh=3qrItgIl0B4PVJ7BihzbatC+fYoXoqpT/2iQ3pExEHs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=O7xLwCuZIeV9pra6B91wZ7NxV3PQ+kpK0BV0HJAi86be2KAHUfOd8LdosP1dNKXJK\n\tfE9RXmNUr4+w+wcVT2g6kIZUoddP8O2vo9S8qPBvFwN0al31yidiGYQ8ZrN4p4PMhl\n\tp735i8Ps4GqC6iQruM1UJ3WVyY7lrXxkbSrZPmX4="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"O7xLwCuZ\"; dkim-atps=neutral","Date":"Thu, 28 Jul 2022 00:17:33 +0300","To":"Florian Sylvestre <fsylvestre@baylibre.com>","Message-ID":"<YuGrbW86UnnpbWTc@pendragon.ideasonboard.com>","References":"<20220727084052.590421-1-fsylvestre@baylibre.com>\n\t<20220727084052.590421-2-fsylvestre@baylibre.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220727084052.590421-2-fsylvestre@baylibre.com>","Subject":"Re: [libcamera-devel] [PATCH v4 1/5] libcamera: yaml_parser: Add\n\tgetList() function","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]