{"id":16161,"url":"https://patchwork.libcamera.org/api/1.1/patches/16161/?format=json","web_url":"https://patchwork.libcamera.org/patch/16161/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220604185939.29163-7-laurent.pinchart@ideasonboard.com>","date":"2022-06-04T18:59:31","name":"[libcamera-devel,RFC,v2,06/14] libcamera: yaml_parser: Preserve order of items in dictionary","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"06f03efe2bb98ce5d4aacebbd445b9ce6af691df","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16161/mbox/","series":[{"id":3161,"url":"https://patchwork.libcamera.org/api/1.1/series/3161/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3161","date":"2022-06-04T18:59:25","name":"Replace boost JSON parser with libyaml in Raspberry Pi IPA","version":2,"mbox":"https://patchwork.libcamera.org/series/3161/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16161/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16161/checks/","tags":{},"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 072B0C3277\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat,  4 Jun 2022 19:00:08 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id BDE0765633;\n\tSat,  4 Jun 2022 21:00:07 +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 7001365643\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  4 Jun 2022 21:00:06 +0200 (CEST)","from pendragon.ideasonboard.com (85-76-79-203-nat.elisa-mobile.fi\n\t[85.76.79.203])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 299BE6D4;\n\tSat,  4 Jun 2022 21:00:04 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654369207;\n\tbh=n/JQvu7qEoXZjoWbSvBdtljGjeBswyT1Z/bFeWPyVN8=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=hAZcEzCVEGkC3Hef9Eal/rJOiy/t9zSqHhVjosTIdHzc1KG3Ru8XibUZVbHEhRcgw\n\tGZzBulOqYxsC1Jrh9TlO5mWqD7qJfKRcoHQlxR/26PQem0jXfGSVh+w04rtJG9qN/U\n\tu4E8gZaNbx+8L35KS+rDZV4BVqJBWggy5B7OwFeywd83ttwVli1L3WK+AoXIH6H5++\n\tsRN6ZoDXnAFo1beehkhbyA1mqWGnivIBeuITjhnzLs+JIdu9bRnRTYQC74cueoHfZn\n\tnKZMtAiTzbxp7hWauFv5ooQDi3wztr0zCIgQCls/U9zy6fiiyOpA9dx/5gEBTlzXTT\n\t1PrkhKw64vhvA==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654369206;\n\tbh=n/JQvu7qEoXZjoWbSvBdtljGjeBswyT1Z/bFeWPyVN8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=GchkrjYEhsw+OhVapLcczy+sf7jfhreo9ROOtdnxRSzDpHMJ2Xs45jiDhWiqOLRfr\n\tk+WbaQZTCV+ZrsKbTCAXLM6UHC+Bm35R8V5Uzfb06psJ2wA30Q0jE6PHkJbB5Z9q8y\n\tT1S+ZQvGz62MJlDFfuJh88PaQsIIKlPYgdNmfJw8="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"GchkrjYE\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Sat,  4 Jun 2022 21:59:31 +0300","Message-Id":"<20220604185939.29163-7-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220604185939.29163-1-laurent.pinchart@ideasonboard.com>","References":"<20220604185939.29163-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH v2 06/14] libcamera: yaml_parser:\n\tPreserve order of items in dictionary","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>"},"content":"YAML dictionaries are ordered, but the std::map container used to store\ndictionary items in YamlObject doesn't preserve that order as maps are\nordered by key, not by insertion order.\n\nAs the order of items in the YAML file may be important, preserve it by\nstoring items in list_ unconditionally. Turn the list_ vector from\nstoring YamlObject unique pointers to storing key-value pairs, with the\nkey being absent when the object is a list, not a dictionary.\n\nThe YamlObject implementation is updated to preserve the existing API,\nwith the only difference being that YamlObject::memberNames() now\nreturns member names in the same order as in the YAML file.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/yaml_parser.h | 13 +++++++--\n src/libcamera/yaml_parser.cpp            | 37 +++++++++++++++---------\n 2 files changed, 35 insertions(+), 15 deletions(-)","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex be5f0914703f..bd1d06d5c488 100644\n--- a/include/libcamera/internal/yaml_parser.h\n+++ b/include/libcamera/internal/yaml_parser.h\n@@ -72,11 +72,20 @@ private:\n \t\tValue,\n \t};\n \n+\tstruct Value {\n+\t\tValue(std::string &&k, std::unique_ptr<YamlObject> &&v)\n+\t\t\t: key(std::move(k)), value(std::move(v))\n+\t\t{\n+\t\t}\n+\t\tstd::string key;\n+\t\tstd::unique_ptr<YamlObject> value;\n+\t};\n+\n \tType type_;\n \n \tstd::string value_;\n-\tstd::vector<std::unique_ptr<YamlObject>> list_;\n-\tstd::map<const std::string, std::unique_ptr<YamlObject>> dictionary_;\n+\tstd::vector<Value> list_;\n+\tstd::map<std::string, YamlObject *> dictionary_;\n };\n \n class YamlParser final\ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 85f6694f5fde..83813e2a8552 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -90,7 +90,6 @@ std::size_t YamlObject::size() const\n {\n \tswitch (type_) {\n \tcase Type::Dictionary:\n-\t\treturn dictionary_.size();\n \tcase Type::List:\n \t\treturn list_.size();\n \tdefault:\n@@ -245,11 +244,11 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const\n \t * that ok == nullptr.\n \t */\n \tbool valid;\n-\tuint32_t width = list_[0]->get<uint32_t>(0, &valid);\n+\tuint32_t width = list_[0].value->get<uint32_t>(0, &valid);\n \tif (!valid)\n \t\treturn defaultValue;\n \n-\tuint32_t height = list_[1]->get<uint32_t>(0, &valid);\n+\tuint32_t height = list_[1].value->get<uint32_t>(0, &valid);\n \tif (!valid)\n \t\treturn defaultValue;\n \n@@ -274,7 +273,7 @@ const YamlObject &YamlObject::operator[](std::size_t index) const\n \tif (type_ != Type::List || index >= size())\n \t\treturn empty;\n \n-\treturn *list_[index];\n+\treturn *list_[index].value;\n }\n \n /**\n@@ -290,7 +289,7 @@ const YamlObject &YamlObject::operator[](std::size_t index) const\n  */\n bool YamlObject::contains(const std::string &key) const\n {\n-\tif (dictionary_.find(key) == dictionary_.end())\n+\tif (dictionary_.find(std::ref(key)) == dictionary_.end())\n \t\treturn false;\n \n \treturn true;\n@@ -312,7 +311,7 @@ bool YamlObject::contains(const std::string &key) const\n std::vector<std::string> YamlObject::memberNames() const\n {\n \tstd::vector<std::string> memberNames;\n-\tfor (auto &[key, _] : dictionary_)\n+\tfor (auto &[key, _] : list_)\n \t\tmemberNames.push_back(key);\n \n \treturn memberNames;\n@@ -584,16 +583,16 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\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(new YamlObject());\n-\t\t\treturn parseNextYamlObject(*list.back(), std::move(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\t};\n \t\treturn parseDictionaryOrList(YamlObject::Type::List, handler);\n \t}\n \n \tcase YAML_MAPPING_START_EVENT: {\n \t\tyamlObject.type_ = YamlObject::Type::Dictionary;\n-\t\tauto &dictionary = yamlObject.dictionary_;\n-\t\tauto handler = [this, &dictionary](EventPtr evtKey) {\n+\t\tauto &list = yamlObject.list_;\n+\t\tauto handler = [this, &list](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@@ -611,10 +610,19 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\n \t\t\tif (!evtValue)\n \t\t\t\treturn -EINVAL;\n \n-\t\t\tauto elem = dictionary.emplace(key, std::make_unique<YamlObject>());\n-\t\t\treturn parseNextYamlObject(*elem.first->second.get(), std::move(evtValue));\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};\n-\t\treturn parseDictionaryOrList(YamlObject::Type::Dictionary, handler);\n+\t\tint ret = parseDictionaryOrList(YamlObject::Type::Dictionary, 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 \n \tdefault:\n@@ -670,6 +678,9 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even\n  * The YamlParser::parse() function takes an open FILE, parses its contents, and\n  * returns a pointer to a YamlObject corresponding to the root node of the YAML\n  * document.\n+ *\n+ * The parser preserves the order of items in the YAML file, for both lists and\n+ * dictionaries.\n  */\n \n /**\n","prefixes":["libcamera-devel","RFC","v2","06/14"]}