From patchwork Sun Dec 15 23:02:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22327 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 7D6C2C32F6 for ; Sun, 15 Dec 2024 23:02:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 89DFB67F47; Mon, 16 Dec 2024 00:02:41 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="fKLOEEVt"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 613BE67F38 for ; Mon, 16 Dec 2024 00:02:32 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2F18C2C6; Mon, 16 Dec 2024 00:01:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734303716; bh=oCiYSbfCI97Mme8FpH/9YtMqHWwcNxZWhtoO3dbgo3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fKLOEEVtTjQmxfb24TbaUks3n6OqzfYmGmtPSlPqCItByWZOzvBK8ErMBoQ1iHcQq z97QRxDisoCehHBhFd3biTHcLwkpML0X1djHyPkYhwfcR/V+HYfjRInjuYUgA/OkQv XZkyF+HEGq5AZRJ0QeCtgOl0v4gEFJSuE/ZYFuAU= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH 7/8] [DNI] libcamera: Use std::string_view in controls Date: Mon, 16 Dec 2024 01:02:05 +0200 Message-ID: <20241215230206.11002-8-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> References: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This showcases potential code improvements by using std::string_view in control-related classes. As the std::string_view usage guidelines forbid its usage in the libcamera public API, this patch must not be integrated. Signed-off-by: Laurent Pinchart --- include/libcamera/control_ids.h.in | 3 ++- include/libcamera/controls.h | 21 +++++++++++---------- src/apps/cam/capture_script.h | 3 ++- src/gstreamer/gstlibcamera-controls.cpp.in | 21 ++++++++++++--------- src/libcamera/control_ids.cpp.in | 4 ++-- src/libcamera/controls.cpp | 15 +++++++-------- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/include/libcamera/control_ids.h.in b/include/libcamera/control_ids.h.in index 5d0594c687f8eb01..5317368839ffcfc7 100644 --- a/include/libcamera/control_ids.h.in +++ b/include/libcamera/control_ids.h.in @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -46,7 +47,7 @@ enum {{ctrl.name}}Enum { {%- endfor %} }; extern const std::array {{ctrl.name}}Values; -extern const std::map {{ctrl.name}}NameValueMap; +extern const std::map {{ctrl.name}}NameValueMap; {% endif -%} extern const Control<{{ctrl.type}}> {{ctrl.name}}; {% endfor -%} diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index b24336cc280f51db..636f5b980b688e16 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -249,28 +250,28 @@ private: class ControlId { public: - ControlId(unsigned int id, const std::string &name, const std::string &vendor, + ControlId(unsigned int id, std::string_view name, std::string_view vendor, ControlType type, std::size_t size = 0, - const std::map &enumStrMap = {}); + const std::map &enumStrMap = {}); unsigned int id() const { return id_; } - const std::string &name() const { return name_; } - const std::string &vendor() const { return vendor_; } + std::string_view name() const { return name_; } + std::string_view vendor() const { return vendor_; } ControlType type() const { return type_; } bool isArray() const { return size_ > 0; } std::size_t size() const { return size_; } - const std::map &enumerators() const { return reverseMap_; } + const std::map &enumerators() const { return reverseMap_; } private: LIBCAMERA_DISABLE_COPY_AND_MOVE(ControlId) unsigned int id_; - std::string name_; - std::string vendor_; + std::string_view name_; + std::string_view vendor_; ControlType type_; std::size_t size_; - std::map enumStrMap_; - std::map reverseMap_; + std::map enumStrMap_; + std::map reverseMap_; }; static inline bool operator==(unsigned int lhs, const ControlId &rhs) @@ -300,7 +301,7 @@ public: using type = T; Control(unsigned int id, const char *name, const char *vendor, - const std::map &enumStrMap = {}) + const std::map &enumStrMap = {}) : ControlId(id, name, vendor, details::control_type>::value, details::control_type>::size, enumStrMap) { diff --git a/src/apps/cam/capture_script.h b/src/apps/cam/capture_script.h index 294b920363bae01b..254768c259b9e246 100644 --- a/src/apps/cam/capture_script.h +++ b/src/apps/cam/capture_script.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -36,7 +37,7 @@ private: }; using EventPtr = std::unique_ptr; - std::map controls_; + std::map controls_; std::map frameControls_; std::shared_ptr camera_; yaml_parser_t parser_; diff --git a/src/gstreamer/gstlibcamera-controls.cpp.in b/src/gstreamer/gstlibcamera-controls.cpp.in index ace36b7106867736..31ca6462748d17a1 100644 --- a/src/gstreamer/gstlibcamera-controls.cpp.in +++ b/src/gstreamer/gstlibcamera-controls.cpp.in @@ -154,9 +154,9 @@ bool GstCameraControls::getProperty(guint propId, GValue *value, [[maybe_unused]] GParamSpec *pspec) { if (!controls_acc_.contains(propId)) { - GST_WARNING("Control '%s' is not available, default value will " - "be returned", - controls::controls.at(propId)->name().c_str()); + const std::string_view name = controls::controls.at(propId)->name(); + GST_WARNING("Control '%.*s' is not available, default value will be returned", + static_cast(name.size()), name.data()); return true; } const ControlValue &cv = controls_acc_.get(propId); @@ -211,9 +211,10 @@ bool GstCameraControls::setProperty(guint propId, const GValue *value, auto info = capabilities_.find(cid); if (info == capabilities_.end()) { - GST_WARNING("Control '%s' is not supported by the " + const std::string_view name = cid->name(); + GST_WARNING("Control '%.*s' is not supported by the " "camera and will be ignored", - cid->name().c_str()); + static_cast(name.size()), name.data()); return true; } } @@ -307,12 +308,14 @@ void GstCameraControls::setCamera(const std::shared_ptr &cam) auto info = capabilities_.find(cid); /* Only add controls which are supported. */ - if (info != capabilities_.end()) + if (info != capabilities_.end()) { new_controls.set(id, value); - else - GST_WARNING("Control '%s' is not supported by the " + } else { + const std::string_view name = cid->name(); + GST_WARNING("Control '%.*s' is not supported by the " "camera and will be ignored", - cid->name().c_str()); + static_cast(name.size()), name.data()); + } } controls_acc_ = new_controls; diff --git a/src/libcamera/control_ids.cpp.in b/src/libcamera/control_ids.cpp.in index afe9e2c9661041f6..caa1cee165d5c99b 100644 --- a/src/libcamera/control_ids.cpp.in +++ b/src/libcamera/control_ids.cpp.in @@ -51,7 +51,7 @@ namespace {{vendor}} { /** * \var {{ctrl.name}}NameValueMap - * \brief Map of all {{ctrl.name}} supported value names (in std::string format) to value + * \brief Map of all {{ctrl.name}} supported value names (in std::string_view format) to value */ {% endif -%} @@ -84,7 +84,7 @@ extern const std::array {{ctrl.n static_cast<{{ctrl.type}}>({{enum.name}}), {%- endfor %} }; -extern const std::map {{ctrl.name}}NameValueMap = { +extern const std::map {{ctrl.name}}NameValueMap = { {%- for enum in ctrl.enum_values %} { "{{enum.name}}", {{enum.name}} }, {%- endfor %} diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index 65eeef2db9c2398f..8c1e61b23c2abcfd 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -406,7 +406,6 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen */ /** - * \fn ControlId::ControlId(unsigned int id, const std::string &name, ControlType type) * \brief Construct a ControlId instance * \param[in] id The control numerical ID * \param[in] name The control name @@ -415,10 +414,10 @@ void ControlValue::reserve(ControlType type, bool isArray, std::size_t numElemen * \param[in] size The size of the array control, or 0 if scalar control * \param[in] enumStrMap The map from enum names to values (optional) */ -ControlId::ControlId(unsigned int id, const std::string &name, - const std::string &vendor, ControlType type, +ControlId::ControlId(unsigned int id, std::string_view name, + std::string_view vendor, ControlType type, std::size_t size, - const std::map &enumStrMap) + const std::map &enumStrMap) : id_(id), name_(name), vendor_(vendor), type_(type), size_(size), enumStrMap_(enumStrMap) { @@ -433,15 +432,15 @@ ControlId::ControlId(unsigned int id, const std::string &name, */ /** - * \fn const char *ControlId::name() const + * \fn std::string_view ControlId::name() const * \brief Retrieve the control name * \return The control name */ /** - * \fn const std::string &ControlId::vendor() const + * \fn std::string_view ControlId::vendor() const * \brief Retrieve the vendor name - * \return The vendor name, as a string + * \return The vendor name */ /** @@ -464,7 +463,7 @@ ControlId::ControlId(unsigned int id, const std::string &name, */ /** - * \fn const std::map &ControlId::enumerators() const + * \fn const std::map &ControlId::enumerators() const * \brief Retrieve the map of enum values to enum names * \return The map of enum values to enum names */