From patchwork Wed Jun 22 15:19:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Sylvestre X-Patchwork-Id: 16325 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 CA33CBE173 for ; Wed, 22 Jun 2022 15:19:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3F93E6563A; Wed, 22 Jun 2022 17:19:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655911165; bh=3Ii/EC6Crwfa61DMI8UWdPMO7Asz9MwoR1YFNELjJbE=; 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=iL4xoZW9Em7FykxKjkXOYIcZbOjMGMYxkaL2Zlq3eFkx7fzYWplYX6Vra6NwSlUIi +/m7y/9hC9Sgc0T6gUbTdHitB3+ajxerPKDThbhTHnyxuR43MR16qYjgavWXZ4gmyV KcfBWQcznOTHnUJAeTck6/SdbnDkqTG5rEovv7i+g3F+/PDlDinpNYvlSr8bMqDBaX YRcuunx0bWdk2rGkT+k/WIuDoQewvWL3KHIcNLJyUJv2pBgvKohyTXURBX6c8FtkaH QjIXGBvP1FLIPnefk1oIrGBlOp1hj8TNtu4nN1kMAblvuY0H1EtNKnfffBkJ7q9B7E 4POcKH09jo4sw== Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B44A1633A7 for ; Wed, 22 Jun 2022 17:19:22 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="Hhv3ocGq"; dkim-atps=neutral Received: by mail-wr1-x436.google.com with SMTP id r20so3470784wra.1 for ; Wed, 22 Jun 2022 08:19:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AE6oXn0AJ2vVpFGOXOrAOauwGaTg8VttfgGqOkuBWk0=; b=Hhv3ocGq9n0oyTO4+rPGUTHYOx4gmwuNdDvH/qyewObwvkbZwnb17dTJmaRDdPqH2S jSuLaWOo8mvXc6eXqw129YzpaNjVIEO8odf/WGL31m+PaYtlllNpu8WUR7mP8sSUdDs4 wFcrbWcLDysTZa+ps5+FjF83sUNN6R+Tc2EqG3xxoB7OhW1by96PAEgrc0X4Or1Soc/z wXXYXsQLAqXk1Q3TO2RINKoyr3HCy23FpkSn7wLS862a2gGLEk+1MLw0utLTxRayjDaT NPiiXDsSnRe3TYMZvAbI158DXLA49ByRFwI84J04env7bHS/hW3UXDaepnGOnP/2DTAw BDWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AE6oXn0AJ2vVpFGOXOrAOauwGaTg8VttfgGqOkuBWk0=; b=TLf8JNc116PQOjB/suXoLRFqaK2QTotg3yHA+LKyY2azQyas35TR4sOrUKJXVYjZtI 4zx+A30jkgGo7VgE8m/HlLZwbDCMNLYwt3h+3yGYEr5/1k9seoeCilQznznLUVg3SNVy 0xuKxPu+N5mB8S89kcqxCy2C0dQe/6mPt0K82rzt14F9n4oFn3S8wQCLiEIJNoS8zL5h 97Bus74cDNbM1bZfEOznc07r6CCItN3xfAtCWOkB5rW1thL90ArHGr0GDRMRyumh5cRm ZW8WLdQIIb0+abywolx7XOdrrIVY47vWQ3FWoDWdN6LED5K+ATh1DKeniZdHqHXtjCEi RSag== X-Gm-Message-State: AJIora9+DjZV/aRpsXK3ikHfmePKqQWJZMt1boZseZ2kFkOdOC7h8m3G UiulkcxS+seWNxMQ+vfyCCH9wUregIV0Eg== X-Google-Smtp-Source: AGRyM1uS4AuXwkFv4w5ZfFomqTNfiuNtrOmTmiDGY2rZfNk67TWfg3eznO/7iQq3u4mQNGZ/cXPF7w== X-Received: by 2002:a05:6000:22a:b0:21b:9bc4:b166 with SMTP id l10-20020a056000022a00b0021b9bc4b166mr3897295wrz.287.1655911162113; Wed, 22 Jun 2022 08:19:22 -0700 (PDT) Received: from BL087.. ([2a01:e34:eea9:e630:4175:47f:ec:c265]) by smtp.gmail.com with ESMTPSA id bi11-20020a05600c3d8b00b0039c362311d2sm1745388wmb.9.2022.06.22.08.19.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jun 2022 08:19:21 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Wed, 22 Jun 2022 17:19:14 +0200 Message-Id: <20220622151918.451635-2-fsylvestre@baylibre.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220622151918.451635-1-fsylvestre@baylibre.com> References: <20220622151918.451635-1-fsylvestre@baylibre.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/5] libcamera: yaml_parser: Add getList() function 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: Florian Sylvestre via libcamera-devel From: Florian Sylvestre Reply-To: Florian Sylvestre Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Allow to retrieve a YAML list of any already supported types in a std::vector. Signed-off-by: Florian Sylvestre Reviewed-by: Paul Elder --- include/libcamera/internal/yaml_parser.h | 16 ++++++ src/libcamera/yaml_parser.cpp | 73 ++++++++++++++++++++++++ test/yaml-parser.cpp | 6 ++ 3 files changed, 95 insertions(+) diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h index 064cf443..ea9189bd 100644 --- a/include/libcamera/internal/yaml_parser.h +++ b/include/libcamera/internal/yaml_parser.h @@ -167,6 +167,22 @@ public: #endif T get(const T &defaultValue, bool *ok = nullptr) const; +#ifndef __DOXYGEN__ + template::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value> * = nullptr> +#else + template +#endif + std::vector getList(bool *ok = nullptr) const; + DictAdapter asDict() const { return DictAdapter{ dictionary_ }; } ListAdapter asList() const { return ListAdapter{ list_ }; } diff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp index 5c45e44e..95c0038b 100644 --- a/src/libcamera/yaml_parser.cpp +++ b/src/libcamera/yaml_parser.cpp @@ -117,6 +117,23 @@ std::size_t YamlObject::size() const * \return Value as a bool type */ +/** + * \fn template YamlObject::getList( + * bool *ok) const + * \brief Parse the YamlObject as a list of \a T. + * \param[out] ok The result of whether the parse succeeded + * + * This function parses the value of the YamlObject as a list of \a T objects, + * and returns the value as a \a std::vector. If parsing fails \a ok is set + * to false and a default empty vector is returned. Otherwise, the + * YamlObject list of values is returned, and \a ok is set to true. + * + * The \a ok pointer is optional and can be a nullptr if the caller doesn't + * need to know if parsing succeeded. + * + * \return Value as a std::vector type + */ + #ifndef __DOXYGEN__ template<> @@ -323,6 +340,62 @@ Size YamlObject::get(const Size &defaultValue, bool *ok) const return Size(width, height); } +template::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value> *> +std::vector YamlObject::getList(bool *ok) const +{ + setOk(ok, false); + + std::vector defaultValue; + + if (type_ != Type::List) + return defaultValue; + + std::vector value = std::vector(list_.size()); + + /* + * Provide somme dummy defaultValue to get() function + * and rely on the ok variable to know if the parsing + * was correct. + */ + T defaultArgument = T(); + bool result; + for (unsigned int i = 0; i < list_.size(); ++i) { + result = true; + value[i] = list_[i]->get(defaultArgument, &result); + + if (!result) + return defaultValue; + } + + setOk(ok, true); + return value; +} + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + +template std::vector YamlObject::getList(bool *ok) const; + #endif /* __DOXYGEN__ */ /** diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 38f84823..bb96a11b 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -524,6 +524,12 @@ protected: return TestFail; } + std::vector values = firstElement.getList(); + if (values.size() != 2 || values[0] != 1 || values[1] != 2) { + cerr << "getList() failed to return correct vector" << std::endl; + return TestFail; + } + auto &secondElement = level2Obj[1]; if (!secondElement.isDictionary() || !secondElement.contains("one") ||