{"id":16031,"url":"https://patchwork.libcamera.org/api/1.1/patches/16031/?format=json","web_url":"https://patchwork.libcamera.org/patch/16031/","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":"<20220524225816.6830-6-laurent.pinchart@ideasonboard.com>","date":"2022-05-24T22:58:09","name":"[libcamera-devel,RFC,05/12] libcamera: yaml_parser: Add iterator API","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"87824b2f356ec98eb8dbbe0c689565aa29ecc306","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/16031/mbox/","series":[{"id":3135,"url":"https://patchwork.libcamera.org/api/1.1/series/3135/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3135","date":"2022-05-24T22:58:04","name":"Replace boost JSON parser with libyaml in Raspberry Pi IPA","version":1,"mbox":"https://patchwork.libcamera.org/series/3135/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16031/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16031/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 45472C326D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 24 May 2022 22:58:34 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 980706567F;\n\tWed, 25 May 2022 00:58:32 +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 874E160416\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 25 May 2022 00:58:27 +0200 (CEST)","from pendragon.ideasonboard.com (ip-109-40-241-133.web.vodafone.de\n\t[109.40.241.133])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E3EA9102E;\n\tWed, 25 May 2022 00:58:26 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1653433112;\n\tbh=Vc024EDhS8mSSRGZH4WuAv7kzOTtH8qkRDZUS9RWDqg=;\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=RIIE4A4qAF15dQvX246C7vVfazpA6DDgPZyZLotu0l2sdywPU1mv8q1nIQ4Wq+f/d\n\tAU3kiZBzDqq2LP7rl2gODkEBBTn5n82xZs1FEUpIs6GIER3ezqgVyX/Q/gzaduLkSy\n\tyiGv/NGgYkyrQchXn9RPB93XCAmNDMJlXNf0y1+Gh4Ejhpfssb0gxRPjRXw3gzmztP\n\tttV4Q/q/NkrIn4wmpxE3hhXaxocIdxTAe6yKNZLiiBuGNs2nEPA4iaP6T/eycA/cF6\n\tFDCWF3JT7d3glI2J4u6dJ3oKkNnVTHrQz/Peg+yCBhCx5mNfX1JjsXVr5+Pz7/uC/e\n\tjiuQ46IIaMNCg==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1653433107;\n\tbh=Vc024EDhS8mSSRGZH4WuAv7kzOTtH8qkRDZUS9RWDqg=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=OPKL63MZ1MU2kY5RfoDkpALaOGEaR0d6pU8Xzj3vKIEzgoRYXn7G87tS4EDhw5XzN\n\tJXhQ0UBiEYyduIitT74MFW0KCe6ui8oBCwvFAQFsxJns3MO4Fnu2KgSkMamgqdqGPS\n\tjhg9dQ6Q/kFS4iHDaIYLePx9iHvZWwCgStAVl1fA="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"OPKL63MZ\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 25 May 2022 01:58:09 +0300","Message-Id":"<20220524225816.6830-6-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220524225816.6830-1-laurent.pinchart@ideasonboard.com>","References":"<20220524225816.6830-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH 05/12] libcamera: yaml_parser: Add\n\titerator API","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":"Allow using range-based for loops over YamlObject instances by\nimplementing iterators. New YamlObject::DictAdapter and\nYamlObject::ListAdapter adapter classes are introduced to provide\ndifferent iterators depending on the object type.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/yaml_parser.h | 115 ++++++++++++++++++++++-\n src/libcamera/yaml_parser.cpp            |  37 ++++++++\n 2 files changed, 150 insertions(+), 2 deletions(-)","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex be5f0914703f..22ed87efdf8f 100644\n--- a/include/libcamera/internal/yaml_parser.h\n+++ b/include/libcamera/internal/yaml_parser.h\n@@ -7,6 +7,7 @@\n \n #pragma once\n \n+#include <iterator>\n #include <map>\n #include <string>\n #include <vector>\n@@ -22,7 +23,114 @@ class YamlParserContext;\n \n class YamlObject\n {\n+private:\n+\tusing DictContainer = std::map<std::string, std::unique_ptr<YamlObject>>;\n+\tusing ListContainer = std::vector<std::unique_ptr<YamlObject>>;\n+\n public:\n+#ifndef __DOXYGEN__\n+\ttemplate<typename Container, typename Derived>\n+\tclass Iterator\n+\t{\n+\tpublic:\n+\t\tusing difference_type = std::ptrdiff_t;\n+\t\tusing iterator_category = std::bidirectional_iterator_tag;\n+\n+\t\tIterator(typename Container::const_iterator it)\n+\t\t\t: it_(it)\n+\t\t{\n+\t\t}\n+\n+\t\tDerived &operator++()\n+\t\t{\n+\t\t\t++it_;\n+\t\t\treturn *static_cast<Derived *>(this);\n+\t\t}\n+\n+\t\tDerived operator++(int)\n+\t\t{\n+\t\t\tDerived it = *static_cast<Derived *>(this);\n+\t\t\tit_++;\n+\t\t\treturn it;\n+\t\t}\n+\n+\t\tfriend bool operator==(const Iterator &a, const Iterator &b)\n+\t\t{\n+\t\t\treturn a.it_ == b.it_;\n+\t\t}\n+\n+\t\tfriend bool operator!=(const Iterator &a, const Iterator &b)\n+\t\t{\n+\t\t\treturn a.it_ != b.it_;\n+\t\t}\n+\n+\tprotected:\n+\t\ttypename Container::const_iterator it_;\n+\t};\n+\n+\ttemplate<typename Container, typename Iterator>\n+\tclass Adapter\n+\t{\n+\tpublic:\n+\t\tAdapter(const Container &container)\n+\t\t\t: container_(container)\n+\t\t{\n+\t\t}\n+\n+\t\tIterator begin() const\n+\t\t{\n+\t\t\treturn Iterator{ container_.begin() };\n+\t\t}\n+\n+\t\tIterator end() const\n+\t\t{\n+\t\t\treturn Iterator{ container_.end() };\n+\t\t}\n+\n+\tprotected:\n+\t\tconst Container &container_;\n+\t};\n+\n+\tclass ListIterator : public Iterator<ListContainer, ListIterator>\n+\t{\n+\tpublic:\n+\t\tusing value_type = const YamlObject &;\n+\t\tusing pointer = const YamlObject *;\n+\t\tusing reference = value_type;\n+\n+\t\tvalue_type operator*() const\n+\t\t{\n+\t\t\treturn *it_->get();\n+\t\t}\n+\n+\t\tpointer operator->() const\n+\t\t{\n+\t\t\treturn it_->get();\n+\t\t}\n+\t};\n+\n+\tclass DictIterator : public Iterator<DictContainer, DictIterator>\n+\t{\n+\tpublic:\n+\t\tusing value_type = std::pair<const std::string &, const YamlObject &>;\n+\t\tusing pointer = value_type *;\n+\t\tusing reference = value_type &;\n+\n+\t\tvalue_type operator*() const\n+\t\t{\n+\t\t\treturn { it_->first, *it_->second.get() };\n+\t\t}\n+\t};\n+\n+\tclass DictAdapter : public Adapter<DictContainer, DictIterator>\n+\t{\n+\t};\n+\n+\tclass ListAdapter : public Adapter<ListContainer, ListIterator>\n+\t{\n+\t};\n+#endif /* __DOXYGEN__ */\n+\n \tYamlObject();\n \t~YamlObject();\n \n@@ -55,6 +163,9 @@ public:\n #endif\n \tT get(const T &defaultValue, bool *ok = nullptr) const;\n \n+\tDictAdapter asDict() const { return DictAdapter{ dictionary_ }; }\n+\tListAdapter asList() const { return ListAdapter{ list_ }; }\n+\n \tconst YamlObject &operator[](std::size_t index) const;\n \n \tbool contains(const std::string &key) const;\n@@ -75,8 +186,8 @@ private:\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+\tListContainer list_;\n+\tDictContainer dictionary_;\n };\n \n class YamlParser final\ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 85f6694f5fde..a156df36e222 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -259,6 +259,43 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const\n \n #endif /* __DOXYGEN__ */\n \n+/**\n+ * \\fn YamlObject::asDict() const\n+ * \\brief Wrap a dictionary YamlObject in an adapter that exposes iterators\n+ *\n+ * The YamlObject class doesn't directly implement iterators, as the iterator\n+ * type depends on whether the object is a Dictionary or List. This function\n+ * wraps a YamlObject of Dictionary type into an adapter that exposes\n+ * iterators, as well as begin() and end() functions, allowing usage of\n+ * range-based for loops with YamlObject.\n+ *\n+ * The iterator's value_type is a\n+ * <em>std::pair<const std::string &, const \\ref YamlObject &></em>.\n+ *\n+ * If the YamlObject is not of Dictionary type, the returned adapter operates\n+ * as an empty container.\n+ *\n+ * \\return An adapter of unspecified type compatible with range-based for loops\n+ */\n+\n+/**\n+ * \\fn YamlObject::asList() const\n+ * \\brief Wrap a list YamlObject in an adapter that exposes iterators\n+ *\n+ * The YamlObject class doesn't directly implement iterators, as the iterator\n+ * type depends on whether the object is a Dictionary or List. This function\n+ * wraps a YamlObject of List type into an adapter that exposes iterators, as\n+ * well as begin() and end() functions, allowing usage of range-based for loops\n+ * with YamlObject.\n+ *\n+ * The iterator's value_type is a <em>const YamlObject &</em>.\n+ *\n+ * If the YamlObject is not of List type, the returned adapter operates as an\n+ * empty container.\n+ *\n+ * \\return An adapter of unspecified type compatible with range-based for loops\n+ */\n+\n /**\n  * \\fn YamlObject::operator[](std::size_t index) const\n  * \\brief Retrieve the element from list YamlObject by index\n","prefixes":["libcamera-devel","RFC","05/12"]}