[{"id":32563,"web_url":"https://patchwork.libcamera.org/comment/32563/","msgid":"<20241206011608.GM21014@pendragon.ideasonboard.com>","date":"2024-12-06T01:16:08","subject":"Re: [PATCH v1 3/3] libcamera: yaml_parser: Use `YamlObject::find()`\n\tin contains/operator[]","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Barnabás,\n\nThank you for the patch.\n\nOn Thu, Dec 05, 2024 at 04:34:24PM +0000, Barnabás Pőcze wrote:\n> Use `YamlObject::find()` to implement `YamlObject::{contains,operator[]}()`.\n> This way there is a single source of truth for dictionary lookups.\n> \n> Furthermore, inline `YamlObject::contains()` as it can trivially\n> be expressed as a call to `find()`.\n> \n> Signed-off-by: Barnabás Pőcze <pobrn@protonmail.com>\n> ---\n>  include/libcamera/internal/yaml_parser.h |  6 +++++-\n>  src/libcamera/yaml_parser.cpp            | 14 ++------------\n>  2 files changed, 7 insertions(+), 13 deletions(-)\n> \n> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\n> index 796e3e90..20dab2f3 100644\n> --- a/include/libcamera/internal/yaml_parser.h\n> +++ b/include/libcamera/internal/yaml_parser.h\n> @@ -207,7 +207,11 @@ public:\n>  \n>  \tconst YamlObject &operator[](std::size_t index) const;\n>  \n> -\tbool contains(std::string_view key) const;\n> +\tbool contains(std::string_view key) const\n> +\t{\n> +\t\treturn find(key);\n> +\t}\n> +\n>  \tconst YamlObject &operator[](std::string_view key) const;\n>  \tconst YamlObject *find(std::string_view key) const;\n>  \n> diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\n> index da8cb61f..72483ef3 100644\n> --- a/src/libcamera/yaml_parser.cpp\n> +++ b/src/libcamera/yaml_parser.cpp\n> @@ -369,10 +369,6 @@ const YamlObject &YamlObject::operator[](std::size_t index) const\n>   *\n>   * \\return True if an element exists, false otherwise\n>   */\n> -bool YamlObject::contains(std::string_view key) const\n> -{\n> -\treturn dictionary_.find(key) != dictionary_.end();\n> -}\n>  \n>  /**\n>   * \\fn YamlObject::operator[](std::string_view key) const\n> @@ -387,14 +383,8 @@ bool YamlObject::contains(std::string_view key) const\n>   */\n>  const YamlObject &YamlObject::operator[](std::string_view key) const\n>  {\n> -\tif (type_ != Type::Dictionary)\n> -\t\treturn empty;\n> -\n> -\tauto iter = dictionary_.find(key);\n> -\tif (iter == dictionary_.end())\n> -\t\treturn empty;\n> -\n> -\treturn *iter->second;\n> +\tauto *child = find(key);\n\nAs commented on 2/3 I'd use an explicit type here.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\treturn child ? *child : empty;\n>  }\n>  \n>  /**","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 1E0C2BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  6 Dec 2024 01:16:24 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 32E7366122;\n\tFri,  6 Dec 2024 02:16:23 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8534F660C8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Dec 2024 02:16:21 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4ABA2641;\n\tFri,  6 Dec 2024 02:15:52 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"kEIUZF5l\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1733447752;\n\tbh=qZfIbN470FkjvECrPM/r4oyzUNpPvKGszW6zQO20DO0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=kEIUZF5l52w4G54lPgSfVZaf/cu62dbGPSZO3rewfHMCER9siR0lms5tSoiSVZruV\n\t3je0899a1zioJOITnYJh8a5DZGKmIybCH3fl0tQ2A7I3eZPtLb2seNLpqa/M2qsM8V\n\txAZmaW/rn+e67uAYAx3ZlfiMhD7h7dxP36+i4+w0=","Date":"Fri, 6 Dec 2024 03:16:08 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v1 3/3] libcamera: yaml_parser: Use `YamlObject::find()`\n\tin contains/operator[]","Message-ID":"<20241206011608.GM21014@pendragon.ideasonboard.com>","References":"<20241205163411.1160094-1-pobrn@protonmail.com>\n\t<20241205163411.1160094-3-pobrn@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20241205163411.1160094-3-pobrn@protonmail.com>","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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]