[{"id":24197,"web_url":"https://patchwork.libcamera.org/comment/24197/","msgid":"<YuF17shWm0MwWF6l@pendragon.ideasonboard.com>","date":"2022-07-27T17:29:18","subject":"Re: [libcamera-devel] [PATCH v7 13/14] 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\nThank you for the patch.\n\nOn Wed, Jul 27, 2022 at 05:38:15AM +0300, Laurent Pinchart via libcamera-devel wrote:\n> From: Florian Sylvestre <fsylvestre@baylibre.com>\n> \n> Allow to retrieve a YAML list of any already supported types in a\n> std::vector.\n> \n> Signed-off-by: Florian Sylvestre <fsylvestre@baylibre.com>\n> ---\n>  include/libcamera/internal/yaml_parser.h | 16 +++++++\n>  src/libcamera/yaml_parser.cpp            | 53 ++++++++++++++++++++++++\n>  test/yaml-parser.cpp                     |  6 +++\n>  3 files changed, 75 insertions(+)\n> \n> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\n> index 9c85d26a2a88..78c359f749bf 100644\n> --- a/include/libcamera/internal/yaml_parser.h\n> +++ b/include/libcamera/internal/yaml_parser.h\n> @@ -183,6 +183,22 @@ public:\n>  \t\treturn get<T>().value_or(defaultValue);\n>  \t}\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::optional<std::vector<T>> getList() const;\n\nI wonder if we could use more template magic to implement this just as\nget(), with the caller writing get<std::vector<T>>(), but I'm not sure\nthat would lead to more readable code. If desired, it could be done on\ntop anyway.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\n>  \tDictAdapter asDict() const { return DictAdapter{ list_ }; }\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 89c234fbbce5..440e35c47cab 100644\n> --- a/src/libcamera/yaml_parser.cpp\n> +++ b/src/libcamera/yaml_parser.cpp\n> @@ -292,6 +292,59 @@ std::optional<Size> YamlObject::get() const\n>  \n>  #endif /* __DOXYGEN__ */\n>  \n> +/**\n> + * \\fn template<typename T> YamlObject::getList<T>() const\n> + * \\brief Parse the YamlObject as a list of \\a T\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, std::nullopt\n> + * is returned.\n> + *\n> + * \\return The YamlObject value as a std::vector<T>, or std::nullopt if parsing\n> + * failed\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::optional<std::vector<T>> YamlObject::getList() const\n> +{\n> +\tif (type_ != Type::List)\n> +\t\treturn {};\n> +\n> +\tstd::vector<T> values;\n> +\tvalues.reserve(list_.size());\n> +\n> +\tfor (const YamlObject &entry : asList()) {\n> +\t\tconst auto value = entry.get<T>();\n> +\t\tif (!value)\n> +\t\t\treturn {};\n> +\t\tvalues.emplace_back(*value);\n> +\t}\n> +\n> +\treturn values;\n> +}\n> +\n> +template std::optional<std::vector<bool>> YamlObject::getList<bool>() const;\n> +template std::optional<std::vector<double>> YamlObject::getList<double>() const;\n> +template std::optional<std::vector<int16_t>> YamlObject::getList<int16_t>() const;\n> +template std::optional<std::vector<uint16_t>> YamlObject::getList<uint16_t>() const;\n> +template std::optional<std::vector<int32_t>> YamlObject::getList<int32_t>() const;\n> +template std::optional<std::vector<uint32_t>> YamlObject::getList<uint32_t>() const;\n> +template std::optional<std::vector<std::string>> YamlObject::getList<std::string>() const;\n> +template std::optional<std::vector<Size>> YamlObject::getList<Size>() 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 782331764346..9fd278664b3b 100644\n> --- a/test/yaml-parser.cpp\n> +++ b/test/yaml-parser.cpp\n> @@ -518,6 +518,12 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> +\t\tconst auto &values = firstElement.getList<uint16_t>();\n> +\t\tif (!values || 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 8282EBE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Jul 2022 17:29:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 43B1363311;\n\tWed, 27 Jul 2022 19:29:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0B2BD63309\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Jul 2022 19:29:21 +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 81D95835;\n\tWed, 27 Jul 2022 19:29:20 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1658942962;\n\tbh=Io2C48cg4kluGURVNz2vQRZWOwQQEsE7g/OVf3F+tlk=;\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:\n\tFrom;\n\tb=C9TcCbkDrS27HuRwa2UA1qlKUuLqLTRvHehbbc9H/0XL8O2uZOLBYYwf0DYDRmQJV\n\tDO82BSuNeB3C5XGrjm+M12gnpxW5qPGVd7lv0UC/1dft/RnXlBw9pdCSlhrvskarSu\n\tn396NsOOCkkRFANG2ycw2ySucfXy+3ZUv0CKMsRS+3K2Q4uB0R6GVWB2pPe79mhjYn\n\tIXP0gLPBVhTNzkcRIYwJRCC0Jz5uy+1VDKUTHGe27XUgDgzCb9ql1/mBzMv9wuyZsa\n\tknmPnHY7sNeOWTLcV3erlc+YtrvmERlXjCyLLZCu5/2BCFsPilTJXymbDOZr/hpIgi\n\tqrXnUOkKy+4MA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1658942960;\n\tbh=Io2C48cg4kluGURVNz2vQRZWOwQQEsE7g/OVf3F+tlk=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=UZfiAVfe0ndz7Yz2WNpQM3nUPxFpPy7w1gluxI3vro5HTNBo81XrYFhodnfKwmQiF\n\tYA5ibXbXTyAX7etVjOifDxnPdGFBDOCiQ4r1W58z826GnqL5PqBnUygiX4LfbbS1Zb\n\tiLJ4y8q+tmicqm4Y8G+UdBUDNqkm/tVJEKMDmmBQ="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"UZfiAVfe\"; dkim-atps=neutral","Date":"Wed, 27 Jul 2022 20:29:18 +0300","To":"libcamera-devel@lists.libcamera.org","Message-ID":"<YuF17shWm0MwWF6l@pendragon.ideasonboard.com>","References":"<20220727023816.30008-1-laurent.pinchart@ideasonboard.com>\n\t<20220727023816.30008-14-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220727023816.30008-14-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v7 13/14] 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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]