[{"id":38630,"web_url":"https://patchwork.libcamera.org/comment/38630/","msgid":"<20260423195414.GD3086691@killaraus.ideasonboard.com>","date":"2026-04-23T19:54:14","subject":"Re: [PATCH v2 14/42] libcamera: yaml_parser: Un-friend\n\tYamlParserContext from YamlObject","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"On Thu, Apr 23, 2026 at 03:59:28PM +0200, Barnabás Pőcze wrote:\n> 2026. 04. 07. 17:33 keltezéssel, Laurent Pinchart írta:\n> > YamlParserContext is a friend of the YamlObject class to access private\n> > member variables. Now that YamlObject exposes functions to set a value\n> > and add children, this is not needed anymore. Decouple the two classes.\n> > \n> > The YamlParserContext::readValue() function now takes a const reference\n> > to the EventPtr as the YamlParserContext::parseNextYamlObject() function\n> > needs to access it in the error handler.\n> > \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> > Changes since v1:\n> > \n> > - Add error checking for add() failures\n> > - Pass const reference to readValue() function\n> > ---\n> >   include/libcamera/internal/yaml_parser.h |  1 -\n> >   src/libcamera/yaml_parser.cpp            | 63 +++++++++++-------------\n> >   2 files changed, 28 insertions(+), 36 deletions(-)\n> > \n> > diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\n> > index 3be61c503c88..c41397df67ab 100644\n> > --- a/include/libcamera/internal/yaml_parser.h\n> > +++ b/include/libcamera/internal/yaml_parser.h\n> > @@ -205,7 +205,6 @@ private:\n> >   \n> >   \ttemplate<typename T>\n> >   \tfriend struct Accessor;\n> > -\tfriend class YamlParserContext;\n> >   \n> >   \tenum class Type {\n> >   \t\tDictionary,\n> > diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\n> > index 61088c7aed4f..6caacc3111c4 100644\n> > --- a/src/libcamera/yaml_parser.cpp\n> > +++ b/src/libcamera/yaml_parser.cpp\n> > @@ -517,8 +517,8 @@ private:\n> >   \n> >   \tEventPtr nextEvent();\n> >   \n> > -\tvoid readValue(std::string &value, EventPtr event);\n> > -\tint parseDictionaryOrList(YamlObject::Type type,\n> > +\tstd::string readValue(const EventPtr &event);\n> > +\tint parseDictionaryOrList(yaml_event_type_t endEventType,\n> >   \t\t\t\t  const std::function<int(EventPtr event)> &parseItem);\n> >   \tint parseNextYamlObject(YamlObject &yamlObject, EventPtr event);\n> >   \n> > @@ -660,22 +660,22 @@ int YamlParserContext::parseContent(YamlObject &yamlObject)\n> >   /**\n> >    * \\fn YamlParserContext::readValue()\n> >    * \\brief Parse event scalar and fill its content into a string\n> > - * \\param[in] value The string reference to fill value\n> >    *\n> >    * A helper function to parse a scalar event as string. The caller needs to\n> > - * guarantee the event is of scaler type.\n> > + * guarantee the event is of scalar type.\n> > + *\n> > + * \\return The scalar event value as a string\n> >    */\n> > -void YamlParserContext::readValue(std::string &value, EventPtr event)\n> > +std::string YamlParserContext::readValue(const EventPtr &event)\n> \n> I'm wondering how this came to be a non-const member function when it could\n> easily be a free function in an anon namespace, or at least `static`...\n\nProbably because it's an internal API, so it received less care. It\ncould be fixed on top if desired, but it shouldn't matter much as it's a\nprivate function.\n\n> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> \n> >   {\n> > -\tvalue.assign(reinterpret_cast<char *>(event->data.scalar.value),\n> > -\t\t     event->data.scalar.length);\n> > +\treturn std::string{ reinterpret_cast<const char *>(event->data.scalar.value),\n> > +\t\t\t    event->data.scalar.length };\n> >   }\n> >   \n> >   /**\n> >    * \\fn YamlParserContext::parseDictionaryOrList()\n> >    * \\brief A helper function to abstract the common part of parsing dictionary or list\n> > - *\n> > - * \\param[in] isDictionary True for parsing a dictionary, and false for a list\n> > + * \\param[in] endEventType The YAML end event type (sequence or mapping)\n> >    * \\param[in] parseItem The callback to handle an item\n> >    *\n> >    * A helper function to abstract parsing an item from a dictionary or a list.\n> > @@ -690,13 +690,9 @@ void YamlParserContext::readValue(std::string &value, EventPtr event)\n> >    * \\return 0 on success or a negative error code otherwise\n> >    * \\retval -EINVAL The parser is failed to initialize\n> >    */\n> > -int YamlParserContext::parseDictionaryOrList(YamlObject::Type type,\n> > +int YamlParserContext::parseDictionaryOrList(yaml_event_type_t endEventType,\n> >   \t\t\t\t\t     const std::function<int(EventPtr event)> &parseItem)\n> >   {\n> > -\tyaml_event_type_t endEventType = YAML_SEQUENCE_END_EVENT;\n> > -\tif (type == YamlObject::Type::Dictionary)\n> > -\t\tendEventType = YAML_MAPPING_END_EVENT;\n> > -\n> >   \t/*\n> >   \t * Add a safety counter to make sure we don't loop indefinitely in case\n> >   \t * the YAML file is malformed.\n> > @@ -738,24 +734,19 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\n> >   \n> >   \tswitch (event->type) {\n> >   \tcase YAML_SCALAR_EVENT:\n> > -\t\tyamlObject.type_ = YamlObject::Type::Value;\n> > -\t\treadValue(yamlObject.value_, std::move(event));\n> > +\t\tyamlObject.set(readValue(event));\n> >   \t\treturn 0;\n> >   \n> >   \tcase YAML_SEQUENCE_START_EVENT: {\n> > -\t\tyamlObject.type_ = YamlObject::Type::List;\n> > -\t\tauto &list = yamlObject.list_;\n> > -\t\tauto handler = [this, &list](EventPtr evt) {\n> > -\t\t\tlist.emplace_back(std::string{}, std::make_unique<YamlObject>());\n> > -\t\t\treturn parseNextYamlObject(*list.back().value, std::move(evt));\n> > +\t\tauto handler = [this, &yamlObject](EventPtr evt) {\n> > +\t\t\tYamlObject *child = yamlObject.add(std::make_unique<YamlObject>());\n> > +\t\t\treturn parseNextYamlObject(*child, std::move(evt));\n> >   \t\t};\n> > -\t\treturn parseDictionaryOrList(YamlObject::Type::List, handler);\n> > +\t\treturn parseDictionaryOrList(YAML_SEQUENCE_END_EVENT, handler);\n> >   \t}\n> >   \n> >   \tcase YAML_MAPPING_START_EVENT: {\n> > -\t\tyamlObject.type_ = YamlObject::Type::Dictionary;\n> > -\t\tauto &list = yamlObject.list_;\n> > -\t\tauto handler = [this, &list](EventPtr evtKey) {\n> > +\t\tauto handler = [this, &yamlObject](EventPtr evtKey) {\n> >   \t\t\t/* Parse key */\n> >   \t\t\tif (evtKey->type != YAML_SCALAR_EVENT) {\n> >   \t\t\t\tLOG(YamlParser, Error) << \"Expect key at line: \"\n> > @@ -765,26 +756,28 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\n> >   \t\t\t\treturn -EINVAL;\n> >   \t\t\t}\n> >   \n> > -\t\t\tstd::string key;\n> > -\t\t\treadValue(key, std::move(evtKey));\n> > +\t\t\tstd::string key = readValue(evtKey);\n> >   \n> >   \t\t\t/* Parse value */\n> >   \t\t\tEventPtr evtValue = nextEvent();\n> >   \t\t\tif (!evtValue)\n> >   \t\t\t\treturn -EINVAL;\n> >   \n> > -\t\t\tauto &elem = list.emplace_back(std::move(key),\n> > -\t\t\t\t\t\t       std::make_unique<YamlObject>());\n> > -\t\t\treturn parseNextYamlObject(*elem.value, std::move(evtValue));\n> > +\t\t\tYamlObject *child = yamlObject.add(std::move(key),\n> > +\t\t\t\t\t\t\t   std::make_unique<YamlObject>());\n> > +\t\t\tif (!child) {\n> > +\t\t\t\tLOG(YamlParser, Error)\n> > +\t\t\t\t\t<< \"Duplicated key at line \"\n> > +\t\t\t\t\t<< evtKey->start_mark.line;\n> > +\t\t\t\treturn -EINVAL;\n> > +\t\t\t}\n> > +\n> > +\t\t\treturn parseNextYamlObject(*child, std::move(evtValue));\n> >   \t\t};\n> > -\t\tint ret = parseDictionaryOrList(YamlObject::Type::Dictionary, handler);\n> > +\t\tint ret = parseDictionaryOrList(YAML_MAPPING_END_EVENT, handler);\n> >   \t\tif (ret)\n> >   \t\t\treturn ret;\n> >   \n> > -\t\tauto &dictionary = yamlObject.dictionary_;\n> > -\t\tfor (const auto &elem : list)\n> > -\t\t\tdictionary.emplace(elem.key, elem.value.get());\n> > -\n> >   \t\treturn 0;\n> >   \t}\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 F0264BE173\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Apr 2026 19:54:18 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E20B862F58;\n\tThu, 23 Apr 2026 21:54:17 +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 5845662010\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Apr 2026 21:54:16 +0200 (CEST)","from killaraus.ideasonboard.com\n\t(2001-14ba-703d-e500--2a1.rev.dnainternet.fi\n\t[IPv6:2001:14ba:703d:e500::2a1])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B5D38802;\n\tThu, 23 Apr 2026 21:52:36 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"U/hCgmlb\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1776973956;\n\tbh=sAIYzoNzoYI7aCcbJJ/n/r7d/OTFQr98en6SV2vVayA=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=U/hCgmlbsEEjHN4x2HiEifZWqYJuG8dgFiC9PDde25zU2+5p1gN3obQiot7hxwP8r\n\tXn9S5RTtzPj6ovjfNI7BKZyZY6uviM/fx9yoQylIoLvCzRBBYRspAQbi27rXFQHcmt\n\tMlOBXI7Q/8bemFjV+zFuHe+mCk652LjkuJUkcEF4=","Date":"Thu, 23 Apr 2026 22:54:14 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 14/42] libcamera: yaml_parser: Un-friend\n\tYamlParserContext from YamlObject","Message-ID":"<20260423195414.GD3086691@killaraus.ideasonboard.com>","References":"<20260407153427.1825999-1-laurent.pinchart@ideasonboard.com>\n\t<20260407153427.1825999-15-laurent.pinchart@ideasonboard.com>\n\t<92111283-281d-40cd-beb8-57fe7f4c6d87@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<92111283-281d-40cd-beb8-57fe7f4c6d87@ideasonboard.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>"}}]