libcamera: controls: Expose string controls as std::string_view

When retrieving the value from a `ControlValue` usually one of two
things happen: a small, trivially copyable object is returned by
value; or a view into the internal buffer is provided. This is true
for everything except strings, which are returned in `std::string`,
incurring the overhead of string construction.

To guarantee no potentially "expensive" copies, use `std::string_view`
pointing to the internal buffer to return the value. This is similar
to how other array-like types are returned with a `Span<>`.

This is an API break, but its scope is limited to just `properties::Model`.

Bug: https://bugs.libcamera.org/show_bug.cgi?id=256
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Barnabás Pőcze
2025-04-16 13:57:06 +02:00
parent b320b20db7
commit f84522d7cd
9 changed files with 19 additions and 17 deletions
+1
View File
@@ -13,6 +13,7 @@
#include <map>
#include <stdint.h>
#include <string>
#include <string_view>
#include <libcamera/controls.h>
+8 -7
View File
@@ -12,6 +12,7 @@
#include <optional>
#include <stdint.h>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
@@ -95,7 +96,7 @@ struct control_type<float> {
};
template<>
struct control_type<std::string> {
struct control_type<std::string_view> {
static constexpr ControlType value = ControlTypeString;
static constexpr std::size_t size = 0;
};
@@ -137,7 +138,7 @@ public:
#ifndef __DOXYGEN__
template<typename T, std::enable_if_t<!details::is_span<T>::value &&
details::control_type<T>::value &&
!std::is_same<std::string, std::remove_cv_t<T>>::value,
!std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
ControlValue(const T &value)
: type_(ControlTypeNone), numElements_(0)
@@ -147,7 +148,7 @@ public:
}
template<typename T, std::enable_if_t<details::is_span<T>::value ||
std::is_same<std::string, std::remove_cv_t<T>>::value,
std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
#else
template<typename T>
@@ -181,7 +182,7 @@ public:
#ifndef __DOXYGEN__
template<typename T, std::enable_if_t<!details::is_span<T>::value &&
!std::is_same<std::string, std::remove_cv_t<T>>::value,
!std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
T get() const
{
@@ -192,7 +193,7 @@ public:
}
template<typename T, std::enable_if_t<details::is_span<T>::value ||
std::is_same<std::string, std::remove_cv_t<T>>::value,
std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
#else
template<typename T>
@@ -209,7 +210,7 @@ public:
#ifndef __DOXYGEN__
template<typename T, std::enable_if_t<!details::is_span<T>::value &&
!std::is_same<std::string, std::remove_cv_t<T>>::value,
!std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
void set(const T &value)
{
@@ -218,7 +219,7 @@ public:
}
template<typename T, std::enable_if_t<details::is_span<T>::value ||
std::is_same<std::string, std::remove_cv_t<T>>::value,
std::is_same<std::string_view, std::remove_cv_t<T>>::value,
std::nullptr_t> = nullptr>
#else
template<typename T>