{"id":16162,"url":"https://patchwork.libcamera.org/api/patches/16162/?format=json","web_url":"https://patchwork.libcamera.org/patch/16162/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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-8-laurent.pinchart@ideasonboard.com>","date":"2022-06-04T18:59:32","name":"[libcamera-devel,RFC,v2,07/14] libcamera: yaml_parser: Add iterator API","commit_ref":null,"pull_url":null,"state":"rfc","archived":false,"hash":"4e58df17b87f2c5c68b1f5b61b9937ee6ebc39ad","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16162/mbox/","series":[{"id":3161,"url":"https://patchwork.libcamera.org/api/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/16162/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16162/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 708FABD161\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat,  4 Jun 2022 19:00:10 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3115265643;\n\tSat,  4 Jun 2022 21:00:10 +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 D573F65637\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  4 Jun 2022 21:00:08 +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 09DC36D4;\n\tSat,  4 Jun 2022 21:00:06 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1654369210;\n\tbh=sv54BM/pRTcqIkDYh3/i2g58b1KvyQ0LCmSCunIt60M=;\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=sKMYzAwXR6FzByeEvE1IcqbwqvAKujDFC7lTA0bZafBWvxiYqYQ+FmR4b+4cRIQ9v\n\tI1LGvI3U4Py+Rce/gi7f12IjYvxlaPZYCIRtEqIJLWbKL06ckV8ATKNe5scKJx4+N+\n\tsBZqUNG0S+JyjJGLwVPPoHAPCKzuW+DO3rX0a1XukDntt455UseuW2xb7PnOlie1Zf\n\tUfaYdT5JHXt8sktf6NIFn61GeEZsMV7qzlnwbSqyuuXq7+dPAVjl0h734ijW/rnatJ\n\t+u5yHwPgBUJrevvoG0Jr1Mo6gJ5ghSouu8E16sc7ZfqrOSIHfQlSeAAPaiiqnb3nkY\n\tv69jEWTGg472w==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1654369208;\n\tbh=sv54BM/pRTcqIkDYh3/i2g58b1KvyQ0LCmSCunIt60M=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=EDy3DXDzdAtEjrLiAghIKEn4TSSUz/juYYCMsBz1CEaKWj4CvBs6e8SpJTkVcU8iz\n\tHmSUH4U0msU8kcDcflnxDH6QpxZI6Wk+h3FsyJYdANFIRkAh8DZjaRX8tRyTHNEG+Z\n\tkMPi5zEsb83AO2g6qHgrMLOD8vdPjc2yXVfjCw+Q="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"EDy3DXDz\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Sat,  4 Jun 2022 21:59:32 +0300","Message-Id":"<20220604185939.29163-8-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 07/14] 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 | 131 +++++++++++++++++++++--\n src/libcamera/yaml_parser.cpp            |  37 +++++++\n 2 files changed, 158 insertions(+), 10 deletions(-)","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex bd1d06d5c488..e142cde0fb6f 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,123 @@ class YamlParserContext;\n \n class YamlObject\n {\n+private:\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+\tusing Container = std::vector<Value>;\n+\tusing ListContainer = std::vector<std::unique_ptr<YamlObject>>;\n+\n public:\n+#ifndef __DOXYGEN__\n+\ttemplate<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\tContainer::const_iterator it_;\n+\t};\n+\n+\ttemplate<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<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_->value.get();\n+\t\t}\n+\n+\t\tpointer operator->() const\n+\t\t{\n+\t\t\treturn it_->value.get();\n+\t\t}\n+\t};\n+\n+\tclass DictIterator : public Iterator<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_->key, *it_->value.get() };\n+\t\t}\n+\t};\n+\n+\tclass DictAdapter : public Adapter<DictIterator>\n+\t{\n+\t};\n+\n+\tclass ListAdapter : public Adapter<ListIterator>\n+\t{\n+\t};\n+#endif /* __DOXYGEN__ */\n+\n \tYamlObject();\n \t~YamlObject();\n \n@@ -55,6 +172,9 @@ public:\n #endif\n \tT get(const T &defaultValue, bool *ok = nullptr) const;\n \n+\tDictAdapter asDict() const { return DictAdapter{ list_ }; }\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@@ -72,19 +192,10 @@ 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<Value> list_;\n+\tContainer list_;\n \tstd::map<std::string, YamlObject *> dictionary_;\n };\n \ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 83813e2a8552..9cdb370d0f2e 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -258,6 +258,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","v2","07/14"]}