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

View File

@@ -318,7 +318,7 @@ protected:
/*
* String type.
*/
std::string string{ "libcamera" };
std::string_view string{ "libcamera" };
value.set(string);
if (value.isNone() || !value.isArray() ||
value.type() != ControlTypeString ||
@@ -327,7 +327,7 @@ protected:
return TestFail;
}
if (value.get<std::string>() != string) {
if (value.get<std::string_view>() != string) {
cerr << "Control value mismatch after setting to string" << endl;
return TestFail;
}