From patchwork Sat Jun 4 18:59:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16161 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 072B0C3277 for ; Sat, 4 Jun 2022 19:00:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id BDE0765633; Sat, 4 Jun 2022 21:00:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654369207; bh=n/JQvu7qEoXZjoWbSvBdtljGjeBswyT1Z/bFeWPyVN8=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=hAZcEzCVEGkC3Hef9Eal/rJOiy/t9zSqHhVjosTIdHzc1KG3Ru8XibUZVbHEhRcgw GZzBulOqYxsC1Jrh9TlO5mWqD7qJfKRcoHQlxR/26PQem0jXfGSVh+w04rtJG9qN/U u4E8gZaNbx+8L35KS+rDZV4BVqJBWggy5B7OwFeywd83ttwVli1L3WK+AoXIH6H5++ sRN6ZoDXnAFo1beehkhbyA1mqWGnivIBeuITjhnzLs+JIdu9bRnRTYQC74cueoHfZn nKZMtAiTzbxp7hWauFv5ooQDi3wztr0zCIgQCls/U9zy6fiiyOpA9dx/5gEBTlzXTT 1PrkhKw64vhvA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 7001365643 for ; Sat, 4 Jun 2022 21:00:06 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="GchkrjYE"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (85-76-79-203-nat.elisa-mobile.fi [85.76.79.203]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 299BE6D4; Sat, 4 Jun 2022 21:00:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1654369206; bh=n/JQvu7qEoXZjoWbSvBdtljGjeBswyT1Z/bFeWPyVN8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GchkrjYEhsw+OhVapLcczy+sf7jfhreo9ROOtdnxRSzDpHMJ2Xs45jiDhWiqOLRfr k+WbaQZTCV+ZrsKbTCAXLM6UHC+Bm35R8V5Uzfb06psJ2wA30Q0jE6PHkJbB5Z9q8y T1S+ZQvGz62MJlDFfuJh88PaQsIIKlPYgdNmfJw8= 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 Subject: [libcamera-devel] [RFC PATCH v2 06/14] libcamera: yaml_parser: Preserve order of items in dictionary 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: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" YAML dictionaries are ordered, but the std::map container used to store dictionary items in YamlObject doesn't preserve that order as maps are ordered by key, not by insertion order. As the order of items in the YAML file may be important, preserve it by storing items in list_ unconditionally. Turn the list_ vector from storing YamlObject unique pointers to storing key-value pairs, with the key being absent when the object is a list, not a dictionary. The YamlObject implementation is updated to preserve the existing API, with the only difference being that YamlObject::memberNames() now returns member names in the same order as in the YAML file. Signed-off-by: Laurent Pinchart --- include/libcamera/internal/yaml_parser.h | 13 +++++++-- src/libcamera/yaml_parser.cpp | 37 +++++++++++++++--------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index be5f0914703f..bd1d06d5c488 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -72,11 +72,20 @@ private: Value, }; + struct Value { + Value(std::string &&k, std::unique_ptr &&v) + : key(std::move(k)), value(std::move(v)) + { + } + std::string key; + std::unique_ptr value; + }; + Type type_; std::string value_; - std::vector> list_; - std::map> dictionary_; + std::vector list_; + std::map dictionary_; }; class YamlParser final diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 85f6694f5fde..83813e2a8552 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -90,7 +90,6 @@ std::size_t YamlObject::size() const { switch (type_) { case Type::Dictionary: - return dictionary_.size(); case Type::List: return list_.size(); default: @@ -245,11 +244,11 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const * that ok == nullptr. */ bool valid; - uint32_t width = list_[0]->get(0, &valid); + uint32_t width = list_[0].value->get(0, &valid); if (!valid) return defaultValue; - uint32_t height = list_[1]->get(0, &valid); + uint32_t height = list_[1].value->get(0, &valid); if (!valid) return defaultValue; @@ -274,7 +273,7 @@ const YamlObject &YamlObject::operator[](std::size_t index) const if (type_ != Type::List || index >= size()) return empty; - return *list_[index]; + return *list_[index].value; } /** @@ -290,7 +289,7 @@ const YamlObject &YamlObject::operator[](std::size_t index) const */ bool YamlObject::contains(const std::string &key) const { - if (dictionary_.find(key) == dictionary_.end()) + if (dictionary_.find(std::ref(key)) == dictionary_.end()) return false; return true; @@ -312,7 +311,7 @@ bool YamlObject::contains(const std::string &key) const std::vector YamlObject::memberNames() const { std::vector memberNames; - for (auto &[key, _] : dictionary_) + for (auto &[key, _] : list_) memberNames.push_back(key); return memberNames; @@ -584,16 +583,16 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even yamlObject.type_ = YamlObject::Type::List; auto &list = yamlObject.list_; auto handler = [this, &list](EventPtr evt) { - list.emplace_back(new YamlObject()); - return parseNextYamlObject(*list.back(), std::move(evt)); + list.emplace_back(std::string{}, std::make_unique()); + return parseNextYamlObject(*list.back().value, std::move(evt)); }; return parseDictionaryOrList(YamlObject::Type::List, handler); } case YAML_MAPPING_START_EVENT: { yamlObject.type_ = YamlObject::Type::Dictionary; - auto &dictionary = yamlObject.dictionary_; - auto handler = [this, &dictionary](EventPtr evtKey) { + auto &list = yamlObject.list_; + auto handler = [this, &list](EventPtr evtKey) { /* Parse key */ if (evtKey->type != YAML_SCALAR_EVENT) { LOG(YamlParser, Error) << "Expect key at line: " @@ -611,10 +610,19 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even if (!evtValue) return -EINVAL; - auto elem = dictionary.emplace(key, std::make_unique()); - return parseNextYamlObject(*elem.first->second.get(), std::move(evtValue)); + auto &elem = list.emplace_back(std::move(key), + std::make_unique()); + return parseNextYamlObject(*elem.value, std::move(evtValue)); }; - return parseDictionaryOrList(YamlObject::Type::Dictionary, handler); + int ret = parseDictionaryOrList(YamlObject::Type::Dictionary, handler); + if (ret) + return ret; + + auto &dictionary = yamlObject.dictionary_; + for (const auto &elem : list) + dictionary.emplace(elem.key, elem.value.get()); + + return 0; } default: @@ -670,6 +678,9 @@ int YamlParserContext::parseNextYamlObject(YamlObject &yamlObject, EventPtr even * The YamlParser::parse() function takes an open FILE, parses its contents, and * returns a pointer to a YamlObject corresponding to the root node of the YAML * document. + * + * The parser preserves the order of items in the YAML file, for both lists and + * dictionaries. */ /**