{"id":16238,"url":"https://patchwork.libcamera.org/api/1.1/patches/16238/?format=json","web_url":"https://patchwork.libcamera.org/patch/16238/","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":"<20220616142403.20723-3-laurent.pinchart@ideasonboard.com>","date":"2022-06-16T14:23:58","name":"[libcamera-devel,2/7] libcamera: yaml_parser: Add iterator API","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"75a084d1eabf8b318a684b183947807cc577834e","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/16238/mbox/","series":[{"id":3181,"url":"https://patchwork.libcamera.org/api/1.1/series/3181/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3181","date":"2022-06-16T14:23:56","name":"libcamera: yaml_parser: Add iterator API","version":1,"mbox":"https://patchwork.libcamera.org/series/3181/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16238/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16238/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 406ACC3274\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 16 Jun 2022 14:24:26 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1DA4865638;\n\tThu, 16 Jun 2022 16:24:22 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9E0C0600F0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 16 Jun 2022 16:24:19 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 0E0B48AF;\n\tThu, 16 Jun 2022 16:24:18 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1655389462;\n\tbh=eBhtyfWSH8NxgAETTgSClfRGZZdpaQDweJah+lUuLr8=;\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=rGrcWipz6eA8gBVZq0VUx4itdRPodySUJK2Rl+cdjX06L2qpvv6i4k6hViAFtfiw3\n\tOG2G2Rqfe4XZVuinQ52Y+ZDmcxgI4RJAXo/S50wmFkWxtRzZivKz4UYYVuKLh/Gu+n\n\t9Gf2F53/A7yC4H4a5kxK9x3Sjsov7jDp8LeJ1GodGXI8JV+UCyTnDAwV0akdVA6xlH\n\tXbDullhijzx9WQiB+oaRCMcrwZMyzpxURuca+ajgy5teoutqnaoogTcQBMaflomn/0\n\t3Qa0Byu6w76tQIaZjqKQe6nVpudOUwVm37I5B+mRiuUsCU7mNKR2/YK+VFBAiY/r3R\n\tv/Qb3zQ7yt3ww==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1655389459;\n\tbh=eBhtyfWSH8NxgAETTgSClfRGZZdpaQDweJah+lUuLr8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=WnkBj2h5DQzzZ7HxZzicXgdSvyRe+XSCtC1k1QFk+ZXFEqfPhA0xDo3hDxL2e5DDq\n\tKVrHVwn3HxJeBxIReYikAnG4xovPH9P0l65lWyTMup9hhSnF4XsXtJyZItJhoJ7I0s\n\tK88vofc9CKHtJo5xeVegyOL5ZXH1nUxXaCC6zBcM="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"WnkBj2h5\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 16 Jun 2022 17:23:58 +0300","Message-Id":"<20220616142403.20723-3-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.35.1","In-Reply-To":"<20220616142403.20723-1-laurent.pinchart@ideasonboard.com>","References":"<20220616142403.20723-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 2/7] libcamera: yaml_parser: Add iterator\n\tAPI","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 | 117 ++++++++++++++++++++++-\n src/libcamera/yaml_parser.cpp            |  39 ++++++++\n 2 files changed, 154 insertions(+), 2 deletions(-)","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex 29b7b218f0d9..8edd32354184 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,116 @@ 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+\tpublic:\n+\t\tusing key_type = std::string;\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 +165,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 +188,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..4df7e5a33d47 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -259,6 +259,45 @@ 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. As YAML mappings are not ordered, the\n+ * iteration order is not specified.\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. As YAML lists are ordered, the iteration order is identical\n+ * to the list order in the YAML data.\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","2/7"]}