From patchwork Thu Dec 5 16:34:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22183 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 686E4C323E for ; Thu, 5 Dec 2024 16:37:23 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A26CA6608C; Thu, 5 Dec 2024 17:37:22 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="g9xFj6m8"; dkim-atps=neutral Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A76926608C for ; Thu, 5 Dec 2024 17:37:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1733416640; x=1733675840; bh=HhTjhbMk2safotm4T9XMUDnMbUuHywURi1D29AvbZ6s=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector: List-Unsubscribe:List-Unsubscribe-Post; b=g9xFj6m8rRKKoUsxcFXqg7fTOhkOKP3xAR8nqkaD5VyauH5L5+Owq2ONy6yIN0MwR ByoVDGDKhByjOutU/5E+MpxKK/kNh3eTv3+hwn+SqrWbVEKyP7wZHyvVb4m7VtgJuU 5RS5fzPGOrB/zoHIYkV1trquMKhN1/J5tUPkhhA3Y85gaV0c2uHi+N2O09VvVFmJ3E MpssldJcaypGttEYOmO4wTQdRrnMp1+TYxms2D+ZALQY98fZ8HHKDkD9eBvUrWs28g 2xCG+GeXZP3/xulFZ0HPGSxf+kadA0bni4TPaP6KZ97lfqIjZjjWqwQzxRYJHy1605 5KFtlhGrAavTw== Date: Thu, 05 Dec 2024 16:34:14 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [PATCH v1 1/3] libcamera: yaml_parser: Add `YamlObject::find(key)` Message-ID: <20241205163411.1160094-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: bd17c12cf3c838d95659c315085fb7c008f3bb89 MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This function is meant to replace `YamlObject::contains()` as it can be easily used for the same purpose, i.e. determining if a certain key exists in a dictionary, but it has the advantage that the result, if any, is immediately available for use without having to do a second lookup. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart --- include/libcamera/internal/yaml_parser.h | 1 + src/libcamera/yaml_parser.cpp | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 8c791656..796e3e90 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -209,6 +209,7 @@ public: bool contains(std::string_view key) const; const YamlObject &operator[](std::string_view key) const; + const YamlObject *find(std::string_view key) const; private: LIBCAMERA_DISABLE_COPY_AND_MOVE(YamlObject) diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index db256ec5..da8cb61f 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -397,6 +397,29 @@ const YamlObject &YamlObject::operator[](std::string_view key) const return *iter->second; } +/** + * \fn YamlObject::find(std::string_view key) const + * \brief Retrieve a member by name from the dictionary + * + * This function retrieves a member of a YamlObject by name. Only YamlObject + * instances of Dictionary type associate elements with names, calling this + * function on other types of instances or with a nonexistent key results in + * \a nullptr being returned. + * + * \return The YamlObject corresponding to the \a key member + */ +const YamlObject *YamlObject::find(std::string_view key) const +{ + if (type_ != Type::Dictionary) + return nullptr; + + auto iter = dictionary_.find(key); + if (iter == dictionary_.end()) + return nullptr; + + return iter->second; +} + #ifndef __DOXYGEN__ class YamlParserContext