From patchwork Thu Apr 21 17:25:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Utkarsh Tiwari X-Patchwork-Id: 15700 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 C69EDC3256 for ; Thu, 21 Apr 2022 17:26:10 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 25C8865644; Thu, 21 Apr 2022 19:26:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1650561970; bh=+urxSG/E5xvwO2xYJBZRCVQxtKl08cBhx9+Mfj7iAOU=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=T5QBD0YrjLiCuq8U82FO85os1g55zCJKj3DyVN1ozQraTzpxfLmzbtX66TNORrl28 VEqBQlzqs4aHCbXwe18NGHftwSziBnGcU5/F9kHlJ8/1+bdHq+htfCoPrPekBm65q/ UfXNqGLQa2cyTAXoVXsilu3N3UDoDVDiYwIpLUb/mgMcEklvwtl7np/y4QeKe1CGF0 8illYDa1EChgyTxvRtUi13BpiYsGIGEHCQVeaDzxoM+I6PH4zp1h7Y4DhHOdpG3Mge C8hQGDtdz5U55kShOKkFAaORLdarLIr6mWOaMhpHgdhdUBcb1avmPQRxRxZAApunYy dtoG4z2xAVmhA== Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C9118604AE for ; Thu, 21 Apr 2022 19:26:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Jw268Wp4"; dkim-atps=neutral Received: by mail-pj1-x1032.google.com with SMTP id ga4-20020a17090b038400b001d4b33c74ccso4451976pjb.1 for ; Thu, 21 Apr 2022 10:26:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=X0ZT5Pa+tAD/jLffaQn9PdWfu656Nph7ctdc2G4fb38=; b=Jw268Wp45T0FloBcLF73JI0SW2vqxlaWVVQHb5oEGkdX5dGHEh2SGPxCsBV0H3NiKc cUFCtAUP8crG4czsPOCIGpx7F+fANpB4uedmS8a0JHHldoEJgNjh+SL79mx7xG/fOWhN vrUDHyqsbCSgXKNHrt7F7ClDRUgigYB4DIoDvXarfqP/W4szBvkZ7tvBo/E9b6M/wtz4 9wblWZ9unY61bt+BR5ndGmHSQ+Mo4Oq5r43QQIleRbSnOO7xHHmPHfP0DylMi6VTAQ56 p1I5ZlNCNW0sevMSxGql//Ov90ibG3Z3JqxNdo0KTzm+/65aLrazju7vIwJocBA6g+em lj1Q== 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:mime-version :content-transfer-encoding; bh=X0ZT5Pa+tAD/jLffaQn9PdWfu656Nph7ctdc2G4fb38=; b=yp3k5gcGMQYXoNKR1CdUwDGKxPrcJSKMRU05Pnk/HKtoACHbRKSEKkFDOGP6serkhs CTEi6RGkUyzvflzF0NojTInw5KV8rlZ0laG5zN1hUw026Jqxy8JOyNFhC4XicxeJAwaA 5DTuAGwmji4czaJTBM488ZM3eHf+hIxWMLMPX9OrOXdVDuh/pOlwZEqnk6Kl1396yYuK DiiQoA10LYb7Ot6viVMxWv6kDQJxx42ibQLCU0CxKB+GaYVdkoufjnz4qfOcqkUngrIx qQAiPb3sGlThxoaUsDvHdumu77iMIY91+ft6v0CbsW1zi7SjB0B7zFYdpy6u6C3+aVPI TKGQ== X-Gm-Message-State: AOAM532uukPYFGHIQGk1PLmdO2wnigkSVfY5/i9BElS81WB3uTJNCT0g l2NpblrJ+UBXr5s6XlKgJPIIdab6Cps9Eg== X-Google-Smtp-Source: ABdhPJxA2jSj/sy4weAUwj1ZhKAmH8gcChLWRAwlVcXI+ulwmBxwRJEinUGtRDu7O8bPyb1kuDYx+g== X-Received: by 2002:a17:903:1205:b0:151:8ae9:93ea with SMTP id l5-20020a170903120500b001518ae993eamr461023plh.37.1650561966865; Thu, 21 Apr 2022 10:26:06 -0700 (PDT) Received: from localhost.localdomain ([2404:bd00:3:db75:9357:7a99:9595:33ae]) by smtp.gmail.com with ESMTPSA id iy2-20020a17090b16c200b001d75aabe050sm1080174pjb.34.2022.04.21.10.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Apr 2022 10:26:06 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 21 Apr 2022 22:55:48 +0530 Message-Id: <20220421172549.138360-1-utkarsh02t@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] Support float data type in options 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: Utkarsh Tiwari via libcamera-devel From: Utkarsh Tiwari Reply-To: Utkarsh Tiwari Cc: Utkarsh Tiwari Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" --- src/cam/options.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++-- src/cam/options.h | 6 +++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 4f7e8691..759328fc 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -6,9 +6,11 @@ */ #include +#include #include #include #include +#include #include #include "options.h" @@ -43,6 +45,9 @@ * * \var OptionType::OptionKeyValue * \brief key=value list argument + * + * \var OptionType::OptionFloat + * \brief Float argument */ /* ----------------------------------------------------------------------------- @@ -129,6 +134,9 @@ const char *Option::typeName() const case OptionKeyValue: return "key=value"; + + case OptionFloat: + return "float"; } return "unknown"; @@ -256,14 +264,14 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, integer = 0; } - value = OptionValue(integer); + value = OptionValue(static_cast(integer)); break; case OptionString: value = OptionValue(arg ? arg : ""); break; - case OptionKeyValue: + case OptionKeyValue: { KeyValueParser *kvParser = option.keyValueParser; KeyValueParser::Options keyValues = kvParser->parse(arg); if (!keyValues.valid()) @@ -273,6 +281,21 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, break; } + case OptionFloat: + float float_val; + + if (arg) { + char *endptr; + float_val = strtof(arg, &endptr); + if (*endptr != '\0' || !std::isfinite(float_val)) + return false; + } else { + float_val = 0; + } + value = OptionValue(float_val); + break; + } + if (option.isArray) values_[opt].addValue(value); else @@ -283,6 +306,7 @@ bool OptionsBase::parseValue(const T &opt, const Option &option, template class OptionsBase; template class OptionsBase; +template class OptionsBase; /* ----------------------------------------------------------------------------- * KeyValueParser @@ -505,6 +529,9 @@ void KeyValueParser::usage(int indent) * * \var OptionValue::ValueType::ValueArray * \brief Array value + * + * \var OptionValue::ValueType::ValueFloat + * \brief Float value (float) */ /** @@ -561,6 +588,17 @@ OptionValue::OptionValue(const KeyValueParser::Options &value) { } +/** + * \brief Construct an float OptionValue instance + * \param[in] value The float value + * + * The value type is set to ValueType::ValueFloat. + */ +OptionValue::OptionValue(const float value) + : type_(ValueFloat), integer_(0), float_(value) +{ +} + /** * \brief Add an entry to an array value * \param[in] value The entry value @@ -600,6 +638,16 @@ OptionValue::operator int() const return toInteger(); } +/** + * \brief Cast the value to an int + * \return The option value as an int, or 0 if the value type isn't + * ValueType::ValueInteger + */ +OptionValue::operator float() const +{ + return toFloat(); +} + /** * \brief Cast the value to a std::string * \return The option value as an std::string, or an empty string if the value @@ -662,6 +710,19 @@ const std::vector &OptionValue::toArray() const return array_; } +/** + * \brief Retrieve the value as an float + * \return The option value as an int, or signaling not-a-number if the value type isn't + * ValueType::ValueFloat + */ +float OptionValue::toFloat() const +{ + if (type_ != ValueFloat) + return std::numeric_limits::signaling_NaN(); + + return float_; +} + /** * \brief Retrieve the list of child values * \return The list of child values diff --git a/src/cam/options.h b/src/cam/options.h index 4ddd4987..f0636f82 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -28,6 +28,7 @@ enum OptionType { OptionInteger, OptionString, OptionKeyValue, + OptionFloat, }; template @@ -124,6 +125,7 @@ public: ValueString, ValueKeyValue, ValueArray, + ValueFloat, }; OptionValue(); @@ -131,6 +133,7 @@ public: OptionValue(const char *value); OptionValue(const std::string &value); OptionValue(const KeyValueParser::Options &value); + OptionValue(const float value); void addValue(const OptionValue &value); @@ -139,11 +142,13 @@ public: operator int() const; operator std::string() const; + operator float() const; int toInteger() const; std::string toString() const; const KeyValueParser::Options &toKeyValues() const; const std::vector &toArray() const; + float toFloat() const; const OptionsParser::Options &children() const; @@ -153,5 +158,6 @@ private: std::string string_; KeyValueParser::Options keyValues_; std::vector array_; + float float_; OptionsParser::Options children_; };