From patchwork Sat Jun 4 18:59:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 16162 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 708FABD161 for ; Sat, 4 Jun 2022 19:00:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3115265643; Sat, 4 Jun 2022 21:00:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654369210; bh=sv54BM/pRTcqIkDYh3/i2g58b1KvyQ0LCmSCunIt60M=; 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=sKMYzAwXR6FzByeEvE1IcqbwqvAKujDFC7lTA0bZafBWvxiYqYQ+FmR4b+4cRIQ9v I1LGvI3U4Py+Rce/gi7f12IjYvxlaPZYCIRtEqIJLWbKL06ckV8ATKNe5scKJx4+N+ sBZqUNG0S+JyjJGLwVPPoHAPCKzuW+DO3rX0a1XukDntt455UseuW2xb7PnOlie1Zf UfaYdT5JHXt8sktf6NIFn61GeEZsMV7qzlnwbSqyuuXq7+dPAVjl0h734ijW/rnatJ +u5yHwPgBUJrevvoG0Jr1Mo6gJ5ghSouu8E16sc7ZfqrOSIHfQlSeAAPaiiqnb3nkY v69jEWTGg472w== 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 D573F65637 for ; Sat, 4 Jun 2022 21:00:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EDy3DXDz"; 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 09DC36D4; Sat, 4 Jun 2022 21:00:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1654369208; bh=sv54BM/pRTcqIkDYh3/i2g58b1KvyQ0LCmSCunIt60M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EDy3DXDzdAtEjrLiAghIKEn4TSSUz/juYYCMsBz1CEaKWj4CvBs6e8SpJTkVcU8iz HmSUH4U0msU8kcDcflnxDH6QpxZI6Wk+h3FsyJYdANFIRkAh8DZjaRX8tRyTHNEG+Z kMPi5zEsb83AO2g6qHgrMLOD8vdPjc2yXVfjCw+Q= 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 Subject: [libcamera-devel] [RFC PATCH v2 07/14] libcamera: yaml_parser: Add iterator API 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" Allow using range-based for loops over YamlObject instances by implementing iterators. New YamlObject::DictAdapter and YamlObject::ListAdapter adapter classes are introduced to provide different iterators depending on the object type. Signed-off-by: Laurent Pinchart --- include/libcamera/internal/yaml_parser.h | 131 +++++++++++++++++++++-- src/libcamera/yaml_parser.cpp | 37 +++++++ 2 files changed, 158 insertions(+), 10 deletions(-) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index bd1d06d5c488..e142cde0fb6f 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -22,7 +23,123 @@ class YamlParserContext; class YamlObject { +private: + 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; + }; + + using Container = std::vector; + using ListContainer = std::vector>; + public: +#ifndef __DOXYGEN__ + template + class Iterator + { + public: + using difference_type = std::ptrdiff_t; + using iterator_category = std::bidirectional_iterator_tag; + + Iterator(typename Container::const_iterator it) + : it_(it) + { + } + + Derived &operator++() + { + ++it_; + return *static_cast(this); + } + + Derived operator++(int) + { + Derived it = *static_cast(this); + it_++; + return it; + } + + friend bool operator==(const Iterator &a, const Iterator &b) + { + return a.it_ == b.it_; + } + + friend bool operator!=(const Iterator &a, const Iterator &b) + { + return a.it_ != b.it_; + } + + protected: + Container::const_iterator it_; + }; + + template + class Adapter + { + public: + Adapter(const Container &container) + : container_(container) + { + } + + Iterator begin() const + { + return Iterator{ container_.begin() }; + } + + Iterator end() const + { + return Iterator{ container_.end() }; + } + + protected: + const Container &container_; + }; + + class ListIterator : public Iterator + { + public: + using value_type = const YamlObject &; + using pointer = const YamlObject *; + using reference = value_type; + + value_type operator*() const + { + return *it_->value.get(); + } + + pointer operator->() const + { + return it_->value.get(); + } + }; + + class DictIterator : public Iterator + { + public: + using value_type = std::pair; + using pointer = value_type *; + using reference = value_type &; + + value_type operator*() const + { + return { it_->key, *it_->value.get() }; + } + }; + + class DictAdapter : public Adapter + { + }; + + class ListAdapter : public Adapter + { + }; +#endif /* __DOXYGEN__ */ + YamlObject(); ~YamlObject(); @@ -55,6 +172,9 @@ public: #endif T get(const T &defaultValue, bool *ok = nullptr) const; + DictAdapter asDict() const { return DictAdapter{ list_ }; } + ListAdapter asList() const { return ListAdapter{ list_ }; } + const YamlObject &operator[](std::size_t index) const; bool contains(const std::string &key) const; @@ -72,19 +192,10 @@ 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_; + Container list_; std::map dictionary_; }; diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 83813e2a8552..9cdb370d0f2e 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -258,6 +258,43 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const #endif /* __DOXYGEN__ */ +/** + * \fn YamlObject::asDict() const + * \brief Wrap a dictionary YamlObject in an adapter that exposes iterators + * + * The YamlObject class doesn't directly implement iterators, as the iterator + * type depends on whether the object is a Dictionary or List. This function + * wraps a YamlObject of Dictionary type into an adapter that exposes + * iterators, as well as begin() and end() functions, allowing usage of + * range-based for loops with YamlObject. + * + * The iterator's value_type is a + * std::pair. + * + * If the YamlObject is not of Dictionary type, the returned adapter operates + * as an empty container. + * + * \return An adapter of unspecified type compatible with range-based for loops + */ + +/** + * \fn YamlObject::asList() const + * \brief Wrap a list YamlObject in an adapter that exposes iterators + * + * The YamlObject class doesn't directly implement iterators, as the iterator + * type depends on whether the object is a Dictionary or List. This function + * wraps a YamlObject of List type into an adapter that exposes iterators, as + * well as begin() and end() functions, allowing usage of range-based for loops + * with YamlObject. + * + * The iterator's value_type is a const YamlObject &. + * + * If the YamlObject is not of List type, the returned adapter operates as an + * empty container. + * + * \return An adapter of unspecified type compatible with range-based for loops + */ + /** * \fn YamlObject::operator[](std::size_t index) const * \brief Retrieve the element from list YamlObject by index