[RFC PATCH v1] treewide: Prefer `std::string_view` over `const std::string&` in parameters

Laurent Pinchart laurent.pinchart at ideasonboard.com
Sun Dec 15 21:56:43 CET 2024


Hi Barnabás,

Thank you for the patch.

On Sun, Dec 15, 2024 at 08:48:06PM +0000, Barnabás Pőcze wrote:
> A parameter of type `const std::string&` is not the right choice in
> almost all cases. It forces the caller to construct an `std::string`
> if they don't already have one, and accessing its data requires
> an extra indirection. Furthermore, `std::string` is just one
> instantiation of `std::basic_string<>`, using e.g. a different
> allocator would immediately make it incompatible.

I don't think the allocator is an issue in libcamera.

> 
> In contrast to that using an `std::string_view` is better if
> NUL termination is not needed since no `std::string` construction
> is required, and in many cases it can be passed directly in registers.

I had a go at this previously, and had very mixed feelings about the
result. I'll post a WIP series for discussion.

> 
> Signed-off-by: Barnabás Pőcze <pobrn at protonmail.com>
> ---
>  include/libcamera/base/utils.h                     | 10 +++++-----
>  include/libcamera/camera_manager.h                 |  3 ++-
>  .../libcamera/internal/camera_sensor_properties.h  |  4 ++--
>  include/libcamera/internal/device_enumerator.h     |  3 ++-
>  include/libcamera/internal/formats.h               |  3 ++-
>  include/libcamera/internal/media_device.h          |  7 ++++---
>  include/libcamera/internal/pipeline_handler.h      |  3 ++-
>  include/libcamera/internal/v4l2_subdevice.h        |  3 ++-
>  include/libcamera/internal/v4l2_videodevice.h      |  3 ++-
>  include/libcamera/pixel_format.h                   |  3 ++-
>  src/apps/cam/capture_script.cpp                    |  2 +-
>  src/apps/cam/capture_script.h                      |  3 ++-
>  src/apps/cam/drm.cpp                               |  8 ++++----
>  src/apps/cam/drm.h                                 |  9 +++++----
>  src/ipa/libipa/camera_sensor_helper.cpp            |  2 +-
>  src/ipa/libipa/camera_sensor_helper.h              |  3 ++-
>  src/ipa/libipa/module.cpp                          |  2 +-
>  src/ipa/libipa/module.h                            |  4 ++--
>  src/libcamera/base/utils.cpp                       |  8 ++++----
>  src/libcamera/camera_manager.cpp                   |  2 +-
>  src/libcamera/device_enumerator.cpp                |  2 +-
>  src/libcamera/device_enumerator_udev.cpp           |  2 +-
>  src/libcamera/formats.cpp                          |  2 +-
>  src/libcamera/media_device.cpp                     | 14 +++++++-------
>  src/libcamera/pipeline_handler.cpp                 |  2 +-
>  src/libcamera/pixel_format.cpp                     |  2 +-
>  src/libcamera/sensor/camera_sensor_properties.cpp  |  4 ++--
>  src/libcamera/v4l2_subdevice.cpp                   |  2 +-
>  src/libcamera/v4l2_videodevice.cpp                 |  2 +-
>  src/py/libcamera/py_camera_manager.h               |  2 +-
>  src/py/libcamera/py_main.cpp                       |  2 +-
>  31 files changed, 66 insertions(+), 55 deletions(-)
> 
> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h
> index 780aeda6a..3b8b3dea9 100644
> --- a/include/libcamera/base/utils.h
> +++ b/include/libcamera/base/utils.h
> @@ -38,7 +38,7 @@ namespace utils {
>  const char *basename(const char *path);
>  
>  char *secure_getenv(const char *name);
> -std::string dirname(const std::string &path);
> +std::string dirname(std::string_view path);
>  
>  template<typename T>
>  std::vector<typename T::key_type> map_keys(const T &map)
> @@ -143,7 +143,7 @@ size_t strlcpy(char *dst, const char *src, size_t size);
>  
>  #ifndef __DOXYGEN__
>  template<typename Container, typename UnaryOp>
> -std::string join(const Container &items, const std::string &sep, UnaryOp op)
> +std::string join(const Container &items, std::string_view sep, UnaryOp op)
>  {
>  	std::ostringstream ss;
>  	bool first = true;
> @@ -162,7 +162,7 @@ std::string join(const Container &items, const std::string &sep, UnaryOp op)
>  }
>  
>  template<typename Container>
> -std::string join(const Container &items, const std::string &sep)
> +std::string join(const Container &items, std::string_view sep)
>  {
>  	std::ostringstream ss;
>  	bool first = true;
> @@ -181,7 +181,7 @@ std::string join(const Container &items, const std::string &sep)
>  }
>  #else
>  template<typename Container, typename UnaryOp>
> -std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
> +std::string join(const Container &items, std::string_view sep, UnaryOp op = nullptr);
>  #endif
>  
>  namespace details {
> @@ -240,7 +240,7 @@ private:
>  
>  details::StringSplitter split(const std::string &str, const std::string &delim);
>  
> -std::string toAscii(const std::string &str);
> +std::string toAscii(std::string_view str);
>  
>  std::string libcameraBuildPath();
>  std::string libcameraSourcePath();
> diff --git a/include/libcamera/camera_manager.h b/include/libcamera/camera_manager.h
> index b50df7825..27835500f 100644
> --- a/include/libcamera/camera_manager.h
> +++ b/include/libcamera/camera_manager.h
> @@ -9,6 +9,7 @@
>  
>  #include <memory>
>  #include <string>
> +#include <string_view>
>  #include <sys/types.h>
>  #include <vector>
>  
> @@ -31,7 +32,7 @@ public:
>  	void stop();
>  
>  	std::vector<std::shared_ptr<Camera>> cameras() const;
> -	std::shared_ptr<Camera> get(const std::string &id);
> +	std::shared_ptr<Camera> get(std::string_view id);
>  
>  	static const std::string &version() { return version_; }
>  
> diff --git a/include/libcamera/internal/camera_sensor_properties.h b/include/libcamera/internal/camera_sensor_properties.h
> index d7d4dab62..b44093906 100644
> --- a/include/libcamera/internal/camera_sensor_properties.h
> +++ b/include/libcamera/internal/camera_sensor_properties.h
> @@ -9,7 +9,7 @@
>  
>  #include <map>
>  #include <stdint.h>
> -#include <string>
> +#include <string_view>
>  
>  #include <libcamera/control_ids.h>
>  #include <libcamera/geometry.h>
> @@ -24,7 +24,7 @@ struct CameraSensorProperties {
>  		uint8_t hblankDelay;
>  	};
>  
> -	static const CameraSensorProperties *get(const std::string &sensor);
> +	static const CameraSensorProperties *get(std::string_view sensor);
>  
>  	Size unitCellSize;
>  	std::map<controls::draft::TestPatternModeEnum, int32_t> testPatternModes;
> diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
> index db3532a98..eecc39cfb 100644
> --- a/include/libcamera/internal/device_enumerator.h
> +++ b/include/libcamera/internal/device_enumerator.h
> @@ -9,6 +9,7 @@
>  
>  #include <memory>
>  #include <string>
> +#include <string_view>
>  #include <vector>
>  
>  #include <libcamera/base/signal.h>
> @@ -48,7 +49,7 @@ public:
>  protected:
>  	std::unique_ptr<MediaDevice> createDevice(const std::string &deviceNode);
>  	void addDevice(std::unique_ptr<MediaDevice> media);
> -	void removeDevice(const std::string &deviceNode);
> +	void removeDevice(std::string_view deviceNode);
>  
>  private:
>  	std::vector<std::shared_ptr<MediaDevice>> devices_;
> diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h
> index 6a3e9c16a..bd7ac6ed1 100644
> --- a/include/libcamera/internal/formats.h
> +++ b/include/libcamera/internal/formats.h
> @@ -8,6 +8,7 @@
>  #pragma once
>  
>  #include <array>
> +#include <string_view>
>  #include <vector>
>  
>  #include <libcamera/geometry.h>
> @@ -35,7 +36,7 @@ public:
>  
>  	static const PixelFormatInfo &info(const PixelFormat &format);
>  	static const PixelFormatInfo &info(const V4L2PixelFormat &format);
> -	static const PixelFormatInfo &info(const std::string &name);
> +	static const PixelFormatInfo &info(std::string_view name);
>  
>  	unsigned int stride(unsigned int width, unsigned int plane,
>  			    unsigned int align = 1) const;
> diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
> index e412d3a0b..91610a7aa 100644
> --- a/include/libcamera/internal/media_device.h
> +++ b/include/libcamera/internal/media_device.h
> @@ -9,6 +9,7 @@
>  
>  #include <map>
>  #include <string>
> +#include <string_view>
>  #include <vector>
>  
>  #include <linux/media.h>
> @@ -44,10 +45,10 @@ public:
>  	unsigned int hwRevision() const { return hwRevision_; }
>  
>  	const std::vector<MediaEntity *> &entities() const { return entities_; }
> -	MediaEntity *getEntityByName(const std::string &name) const;
> +	MediaEntity *getEntityByName(std::string_view name) const;
>  
> -	MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
> -			const std::string &sinkName, unsigned int sinkIdx);
> +	MediaLink *link(std::string_view sourceName, unsigned int sourceIdx,
> +			std::string_view sinkName, unsigned int sinkIdx);
>  	MediaLink *link(const MediaEntity *source, unsigned int sourceIdx,
>  			const MediaEntity *sink, unsigned int sinkIdx);
>  	MediaLink *link(const MediaPad *source, const MediaPad *sink);
> diff --git a/include/libcamera/internal/pipeline_handler.h b/include/libcamera/internal/pipeline_handler.h
> index fb28a18d0..45eafce9b 100644
> --- a/include/libcamera/internal/pipeline_handler.h
> +++ b/include/libcamera/internal/pipeline_handler.h
> @@ -10,6 +10,7 @@
>  #include <memory>
>  #include <queue>
>  #include <string>
> +#include <string_view>
>  #include <sys/types.h>
>  #include <vector>
>  
> @@ -112,7 +113,7 @@ public:
>  	const std::string &name() const { return name_; }
>  
>  	static std::vector<PipelineHandlerFactoryBase *> &factories();
> -	static const PipelineHandlerFactoryBase *getFactoryByName(const std::string &name);
> +	static const PipelineHandlerFactoryBase *getFactoryByName(std::string_view name);
>  
>  private:
>  	static void registerType(PipelineHandlerFactoryBase *factory);
> diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h
> index 194382f84..d17365e14 100644
> --- a/include/libcamera/internal/v4l2_subdevice.h
> +++ b/include/libcamera/internal/v4l2_subdevice.h
> @@ -11,6 +11,7 @@
>  #include <optional>
>  #include <ostream>
>  #include <string>
> +#include <string_view>
>  #include <vector>
>  
>  #include <linux/v4l2-subdev.h>
> @@ -161,7 +162,7 @@ public:
>  	const V4L2SubdeviceCapability &caps() const { return caps_; }
>  
>  	static std::unique_ptr<V4L2Subdevice>
> -	fromEntityName(const MediaDevice *media, const std::string &entity);
> +	fromEntityName(const MediaDevice *media, std::string_view entity);
>  
>  protected:
>  	std::string logPrefix() const override;
> diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
> index f021c2a01..c256e82af 100644
> --- a/include/libcamera/internal/v4l2_videodevice.h
> +++ b/include/libcamera/internal/v4l2_videodevice.h
> @@ -14,6 +14,7 @@
>  #include <ostream>
>  #include <stdint.h>
>  #include <string>
> +#include <string_view>
>  #include <unordered_set>
>  #include <vector>
>  
> @@ -228,7 +229,7 @@ public:
>  	Signal<> dequeueTimeout;
>  
>  	static std::unique_ptr<V4L2VideoDevice>
> -	fromEntityName(const MediaDevice *media, const std::string &entity);
> +	fromEntityName(const MediaDevice *media, std::string_view entity);
>  
>  	V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat) const;
>  
> diff --git a/include/libcamera/pixel_format.h b/include/libcamera/pixel_format.h
> index 1b4d8c7c8..874b2528b 100644
> --- a/include/libcamera/pixel_format.h
> +++ b/include/libcamera/pixel_format.h
> @@ -10,6 +10,7 @@
>  #include <ostream>
>  #include <stdint.h>
>  #include <string>
> +#include <string_view>
>  
>  namespace libcamera {
>  
> @@ -38,7 +39,7 @@ public:
>  
>  	std::string toString() const;
>  
> -	static PixelFormat fromString(const std::string &name);
> +	static PixelFormat fromString(std::string_view name);
>  
>  private:
>  	uint32_t fourcc_;
> diff --git a/src/apps/cam/capture_script.cpp b/src/apps/cam/capture_script.cpp
> index fc1dfa75f..d4760ea78 100644
> --- a/src/apps/cam/capture_script.cpp
> +++ b/src/apps/cam/capture_script.cpp
> @@ -432,7 +432,7 @@ std::vector<std::string> CaptureScript::parseSingleArray()
>  	}
>  }
>  
> -void CaptureScript::unpackFailure(const ControlId *id, const std::string &repr)
> +void CaptureScript::unpackFailure(const ControlId *id, std::string_view repr)
>  {
>  	static const std::map<unsigned int, const char *> typeNames = {
>  		{ ControlTypeNone, "none" },
> diff --git a/src/apps/cam/capture_script.h b/src/apps/cam/capture_script.h
> index 294b92036..fb40371f2 100644
> --- a/src/apps/cam/capture_script.h
> +++ b/src/apps/cam/capture_script.h
> @@ -10,6 +10,7 @@
>  #include <map>
>  #include <memory>
>  #include <string>
> +#include <string_view>
>  
>  #include <libcamera/camera.h>
>  #include <libcamera/controls.h>
> @@ -67,7 +68,7 @@ private:
>  	std::vector<std::string> parseSingleArray();
>  
>  	void unpackFailure(const libcamera::ControlId *id,
> -			   const std::string &repr);
> +			   std::string_view repr);
>  	libcamera::ControlValue unpackControl(const libcamera::ControlId *id);
>  	libcamera::Rectangle unpackRectangle(const std::vector<std::string> &strVec);
>  };
> diff --git a/src/apps/cam/drm.cpp b/src/apps/cam/drm.cpp
> index 47bbb6b05..fd9c59ec4 100644
> --- a/src/apps/cam/drm.cpp
> +++ b/src/apps/cam/drm.cpp
> @@ -57,7 +57,7 @@ Object::~Object()
>  {
>  }
>  
> -const Property *Object::property(const std::string &name) const
> +const Property *Object::property(std::string_view name) const
>  {
>  	for (const PropertyValue &pv : properties_) {
>  		const Property *property = static_cast<const Property *>(dev_->object(pv.id()));
> @@ -68,7 +68,7 @@ const Property *Object::property(const std::string &name) const
>  	return nullptr;
>  }
>  
> -const PropertyValue *Object::propertyValue(const std::string &name) const
> +const PropertyValue *Object::propertyValue(std::string_view name) const
>  {
>  	for (const PropertyValue &pv : properties_) {
>  		const Property *property = static_cast<const Property *>(dev_->object(pv.id()));
> @@ -320,7 +320,7 @@ AtomicRequest::~AtomicRequest()
>  		drmModeAtomicFree(request_);
>  }
>  
> -int AtomicRequest::addProperty(const Object *object, const std::string &property,
> +int AtomicRequest::addProperty(const Object *object, std::string_view property,
>  			       uint64_t value)
>  {
>  	if (!valid_)
> @@ -335,7 +335,7 @@ int AtomicRequest::addProperty(const Object *object, const std::string &property
>  	return addProperty(object->id(), prop->id(), value);
>  }
>  
> -int AtomicRequest::addProperty(const Object *object, const std::string &property,
> +int AtomicRequest::addProperty(const Object *object, std::string_view property,
>  			       std::unique_ptr<Blob> blob)
>  {
>  	if (!valid_)
> diff --git a/src/apps/cam/drm.h b/src/apps/cam/drm.h
> index 1ba83b6eb..aa1b06400 100644
> --- a/src/apps/cam/drm.h
> +++ b/src/apps/cam/drm.h
> @@ -13,6 +13,7 @@
>  #include <memory>
>  #include <stdint.h>
>  #include <string>
> +#include <string_view>
>  #include <vector>
>  
>  #include <libcamera/base/signal.h>
> @@ -57,8 +58,8 @@ public:
>  	uint32_t id() const { return id_; }
>  	Type type() const { return type_; }
>  
> -	const Property *property(const std::string &name) const;
> -	const PropertyValue *propertyValue(const std::string &name) const;
> +	const Property *property(std::string_view name) const;
> +	const PropertyValue *propertyValue(std::string_view name) const;
>  	const std::vector<PropertyValue> &properties() const { return properties_; }
>  
>  protected:
> @@ -260,9 +261,9 @@ public:
>  	Device *device() const { return dev_; }
>  	bool isValid() const { return valid_; }
>  
> -	int addProperty(const Object *object, const std::string &property,
> +	int addProperty(const Object *object, std::string_view property,
>  			uint64_t value);
> -	int addProperty(const Object *object, const std::string &property,
> +	int addProperty(const Object *object, std::string_view property,
>  			std::unique_ptr<Blob> blob);
>  	int commit(unsigned int flags = 0);
>  
> diff --git a/src/ipa/libipa/camera_sensor_helper.cpp b/src/ipa/libipa/camera_sensor_helper.cpp
> index 7c66cd57d..4ed050b14 100644
> --- a/src/ipa/libipa/camera_sensor_helper.cpp
> +++ b/src/ipa/libipa/camera_sensor_helper.cpp
> @@ -240,7 +240,7 @@ CameraSensorHelperFactoryBase::CameraSensorHelperFactoryBase(const std::string n
>   * corresponding to the named factory or a null pointer if no such factory
>   * exists
>   */
> -std::unique_ptr<CameraSensorHelper> CameraSensorHelperFactoryBase::create(const std::string &name)
> +std::unique_ptr<CameraSensorHelper> CameraSensorHelperFactoryBase::create(std::string_view name)
>  {
>  	const std::vector<CameraSensorHelperFactoryBase *> &factories =
>  		CameraSensorHelperFactoryBase::factories();
> diff --git a/src/ipa/libipa/camera_sensor_helper.h b/src/ipa/libipa/camera_sensor_helper.h
> index a9300a64f..eedc80114 100644
> --- a/src/ipa/libipa/camera_sensor_helper.h
> +++ b/src/ipa/libipa/camera_sensor_helper.h
> @@ -11,6 +11,7 @@
>  #include <optional>
>  #include <stdint.h>
>  #include <string>
> +#include <string_view>
>  #include <variant>
>  #include <vector>
>  
> @@ -56,7 +57,7 @@ public:
>  	CameraSensorHelperFactoryBase(const std::string name);
>  	virtual ~CameraSensorHelperFactoryBase() = default;
>  
> -	static std::unique_ptr<CameraSensorHelper> create(const std::string &name);
> +	static std::unique_ptr<CameraSensorHelper> create(std::string_view name);
>  
>  	static std::vector<CameraSensorHelperFactoryBase *> &factories();
>  
> diff --git a/src/ipa/libipa/module.cpp b/src/ipa/libipa/module.cpp
> index 64ca91419..91fb588e7 100644
> --- a/src/ipa/libipa/module.cpp
> +++ b/src/ipa/libipa/module.cpp
> @@ -107,7 +107,7 @@ namespace ipa {
>   */
>  
>  /**
> - * \fn Module::createAlgorithm(const std::string &name)
> + * \fn Module::createAlgorithm(std::string_view name)
>   * \brief Create an instance of an Algorithm by name
>   * \param[in] name The algorithm name
>   *
> diff --git a/src/ipa/libipa/module.h b/src/ipa/libipa/module.h
> index 0fb51916f..62c700a64 100644
> --- a/src/ipa/libipa/module.h
> +++ b/src/ipa/libipa/module.h
> @@ -9,7 +9,7 @@
>  
>  #include <list>
>  #include <memory>
> -#include <string>
> +#include <string_view>
>  #include <vector>
>  
>  #include <libcamera/base/log.h>
> @@ -95,7 +95,7 @@ private:
>  		return 0;
>  	}
>  
> -	static std::unique_ptr<Algorithm<Module>> createAlgorithm(const std::string &name)
> +	static std::unique_ptr<Algorithm<Module>> createAlgorithm(std::string_view name)
>  	{
>  		for (const AlgorithmFactoryBase<Module> *factory : factories()) {
>  			if (factory->name() == name)
> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp
> index bcfc1941a..de6e2afd6 100644
> --- a/src/libcamera/base/utils.cpp
> +++ b/src/libcamera/base/utils.cpp
> @@ -80,7 +80,7 @@ char *secure_getenv(const char *name)
>   *
>   * \return A string of the directory component of the path
>   */
> -std::string dirname(const std::string &path)
> +std::string dirname(std::string_view path)
>  {
>  	if (path.empty())
>  		return ".";
> @@ -116,7 +116,7 @@ std::string dirname(const std::string &path)
>  		pos--;
>  	}
>  
> -	return path.substr(0, pos + 1);
> +	return std::string(path.substr(0, pos + 1));
>  }
>  
>  /**
> @@ -278,7 +278,7 @@ std::string details::StringSplitter::iterator::operator*() const
>  
>  /**
>   * \fn template<typename Container, typename UnaryOp> \
> - * std::string utils::join(const Container &items, const std::string &sep, UnaryOp op)
> + * std::string utils::join(const Container &items, std::string_view sep, UnaryOp op)
>   * \brief Join elements of a container in a string with a separator
>   * \param[in] items The container
>   * \param[in] sep The separator to add between elements
> @@ -319,7 +319,7 @@ details::StringSplitter split(const std::string &str, const std::string &delim)
>   *
>   * \return A string equal to \a str stripped out of all non-ASCII characters
>   */
> -std::string toAscii(const std::string &str)
> +std::string toAscii(std::string_view str)
>  {
>  	std::string ret;
>  	for (const char &c : str)
> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> index 87e6717ec..179fa37ea 100644
> --- a/src/libcamera/camera_manager.cpp
> +++ b/src/libcamera/camera_manager.cpp
> @@ -382,7 +382,7 @@ std::vector<std::shared_ptr<Camera>> CameraManager::cameras() const
>   *
>   * \return Shared pointer to Camera object or nullptr if camera not found
>   */
> -std::shared_ptr<Camera> CameraManager::get(const std::string &id)
> +std::shared_ptr<Camera> CameraManager::get(std::string_view id)
>  {
>  	Private *const d = _d();
>  
> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
> index ae17862f6..8744a90f3 100644
> --- a/src/libcamera/device_enumerator.cpp
> +++ b/src/libcamera/device_enumerator.cpp
> @@ -272,7 +272,7 @@ void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> media)
>   * enumerator with addDevice(). The media device's MediaDevice::disconnected
>   * signal is emitted.
>   */
> -void DeviceEnumerator::removeDevice(const std::string &deviceNode)
> +void DeviceEnumerator::removeDevice(std::string_view deviceNode)
>  {
>  	std::shared_ptr<MediaDevice> media;
>  
> diff --git a/src/libcamera/device_enumerator_udev.cpp b/src/libcamera/device_enumerator_udev.cpp
> index 4e20a3cc0..c330f30bc 100644
> --- a/src/libcamera/device_enumerator_udev.cpp
> +++ b/src/libcamera/device_enumerator_udev.cpp
> @@ -351,7 +351,7 @@ void DeviceEnumeratorUdev::udevNotify()
>  	} else if (action == "remove") {
>  		const char *subsystem = udev_device_get_subsystem(dev);
>  		if (subsystem && !strcmp(subsystem, "media"))
> -			removeDevice(std::string(deviceNode));
> +			removeDevice(deviceNode);
>  	}
>  
>  	udev_device_unref(dev);
> diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
> index bfcdfc089..112dfe66b 100644
> --- a/src/libcamera/formats.cpp
> +++ b/src/libcamera/formats.cpp
> @@ -1037,7 +1037,7 @@ const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)
>   * \return The PixelFormatInfo describing the PixelFormat matching the
>   * \a name if known, or an invalid PixelFormatInfo otherwise
>   */
> -const PixelFormatInfo &PixelFormatInfo::info(const std::string &name)
> +const PixelFormatInfo &PixelFormatInfo::info(std::string_view name)
>  {
>  	for (const auto &info : pixelFormatInfo) {
>  		if (info.second.name == name)
> diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
> index d71dad74d..131b34446 100644
> --- a/src/libcamera/media_device.cpp
> +++ b/src/libcamera/media_device.cpp
> @@ -331,7 +331,7 @@ done:
>   * \param[in] name The entity name
>   * \return The entity with \a name, or nullptr if no such entity is found
>   */
> -MediaEntity *MediaDevice::getEntityByName(const std::string &name) const
> +MediaEntity *MediaDevice::getEntityByName(std::string_view name) const
>  {
>  	for (MediaEntity *e : entities_)
>  		if (e->name() == name)
> @@ -359,8 +359,8 @@ MediaEntity *MediaDevice::getEntityByName(const std::string &name) const
>   * \return The link that connects the two pads, or nullptr if no such a link
>   * exists
>   */
> -MediaLink *MediaDevice::link(const std::string &sourceName, unsigned int sourceIdx,
> -			     const std::string &sinkName, unsigned int sinkIdx)
> +MediaLink *MediaDevice::link(std::string_view sourceName, unsigned int sourceIdx,
> +			     std::string_view sinkName, unsigned int sinkIdx)
>  {
>  	const MediaEntity *source = getEntityByName(sourceName);
>  	const MediaEntity *sink = getEntityByName(sinkName);
> @@ -382,8 +382,8 @@ MediaLink *MediaDevice::link(const std::string &sourceName, unsigned int sourceI
>   * entity \a source, to the pad at index \a sinkIdx of the sink entity \a
>   * sink, if any.
>   *
> - * \sa link(const std::string &sourceName, unsigned int sourceIdx,
> - *          const std::string &sinkName, unsigned int sinkIdx)
> + * \sa link(std::string_view sourceName, unsigned int sourceIdx,
> + *          std::string_view sinkName, unsigned int sinkIdx)
>   * \sa link(const MediaPad *source, const MediaPad *sink)
>   *
>   * \return The link that connects the two pads, or nullptr if no such a link
> @@ -406,8 +406,8 @@ MediaLink *MediaDevice::link(const MediaEntity *source, unsigned int sourceIdx,
>   * \param[in] source The source pad
>   * \param[in] sink The sink pad
>   *
> - * \sa link(const std::string &sourceName, unsigned int sourceIdx,
> - *          const std::string &sinkName, unsigned int sinkIdx)
> + * \sa link(std::string_view sourceName, unsigned int sourceIdx,
> + *          std::string_view sinkName, unsigned int sinkIdx)
>   * \sa link(const MediaEntity *source, unsigned int sourceIdx,
>   *          const MediaEntity *sink, unsigned int sinkIdx)
>   *
> diff --git a/src/libcamera/pipeline_handler.cpp b/src/libcamera/pipeline_handler.cpp
> index caa5c20e7..6b395c666 100644
> --- a/src/libcamera/pipeline_handler.cpp
> +++ b/src/libcamera/pipeline_handler.cpp
> @@ -852,7 +852,7 @@ std::vector<PipelineHandlerFactoryBase *> &PipelineHandlerFactoryBase::factories
>   * \param[in] name The pipeline handler name
>   * \return The factory of the pipeline with name \a name, or nullptr if not found
>   */
> -const PipelineHandlerFactoryBase *PipelineHandlerFactoryBase::getFactoryByName(const std::string &name)
> +const PipelineHandlerFactoryBase *PipelineHandlerFactoryBase::getFactoryByName(std::string_view name)
>  {
>  	const std::vector<PipelineHandlerFactoryBase *> &factories =
>  		PipelineHandlerFactoryBase::factories();
> diff --git a/src/libcamera/pixel_format.cpp b/src/libcamera/pixel_format.cpp
> index 314179a81..22f1520fd 100644
> --- a/src/libcamera/pixel_format.cpp
> +++ b/src/libcamera/pixel_format.cpp
> @@ -135,7 +135,7 @@ std::string PixelFormat::toString() const
>   * \return The PixelFormat represented by the \a name if known, or an
>   * invalid pixel format otherwise.
>   */
> -PixelFormat PixelFormat::fromString(const std::string &name)
> +PixelFormat PixelFormat::fromString(std::string_view name)
>  {
>  	return PixelFormatInfo::info(name).format;
>  }
> diff --git a/src/libcamera/sensor/camera_sensor_properties.cpp b/src/libcamera/sensor/camera_sensor_properties.cpp
> index 2b06c5a1a..4283cacfa 100644
> --- a/src/libcamera/sensor/camera_sensor_properties.cpp
> +++ b/src/libcamera/sensor/camera_sensor_properties.cpp
> @@ -78,9 +78,9 @@ LOG_DEFINE_CATEGORY(CameraSensorProperties)
>   * \return A pointer to the CameraSensorProperties instance associated with a sensor
>   * or nullptr if the sensor is not supported
>   */
> -const CameraSensorProperties *CameraSensorProperties::get(const std::string &sensor)
> +const CameraSensorProperties *CameraSensorProperties::get(std::string_view sensor)
>  {
> -	static const std::map<std::string, const CameraSensorProperties> sensorProps = {
> +	static const std::map<std::string_view, const CameraSensorProperties> sensorProps = {
>  		{ "ar0144", {
>  			.unitCellSize = { 3000, 3000 },
>  			.testPatternModes = {
> diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp
> index 3a0d075f9..7e85b33be 100644
> --- a/src/libcamera/v4l2_subdevice.cpp
> +++ b/src/libcamera/v4l2_subdevice.cpp
> @@ -1688,7 +1688,7 @@ const std::string &V4L2Subdevice::model()
>   */
>  std::unique_ptr<V4L2Subdevice>
>  V4L2Subdevice::fromEntityName(const MediaDevice *media,
> -			      const std::string &entity)
> +			      std::string_view entity)
>  {
>  	MediaEntity *mediaEntity = media->getEntityByName(entity);
>  	if (!mediaEntity)
> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
> index a5cf67845..834a1bd0d 100644
> --- a/src/libcamera/v4l2_videodevice.cpp
> +++ b/src/libcamera/v4l2_videodevice.cpp
> @@ -2077,7 +2077,7 @@ void V4L2VideoDevice::watchdogExpired()
>   */
>  std::unique_ptr<V4L2VideoDevice>
>  V4L2VideoDevice::fromEntityName(const MediaDevice *media,
> -				const std::string &entity)
> +				std::string_view entity)
>  {
>  	MediaEntity *mediaEntity = media->getEntityByName(entity);
>  	if (!mediaEntity)
> diff --git a/src/py/libcamera/py_camera_manager.h b/src/py/libcamera/py_camera_manager.h
> index 3574db236..af69b915e 100644
> --- a/src/py/libcamera/py_camera_manager.h
> +++ b/src/py/libcamera/py_camera_manager.h
> @@ -20,7 +20,7 @@ public:
>  	~PyCameraManager();
>  
>  	pybind11::list cameras();
> -	std::shared_ptr<Camera> get(const std::string &name) { return cameraManager_->get(name); }
> +	std::shared_ptr<Camera> get(std::string_view name) { return cameraManager_->get(name); }
>  
>  	static const std::string &version() { return CameraManager::version(); }
>  
> diff --git a/src/py/libcamera/py_main.cpp b/src/py/libcamera/py_main.cpp
> index 441a70ab4..820768c24 100644
> --- a/src/py/libcamera/py_main.cpp
> +++ b/src/py/libcamera/py_main.cpp
> @@ -510,7 +510,7 @@ PYBIND11_MODULE(_libcamera, m)
>  	pyPixelFormat
>  		.def(py::init<>())
>  		.def(py::init<uint32_t, uint64_t>())
> -		.def(py::init<>([](const std::string &str) {
> +		.def(py::init<>([](std::string_view str) {
>  			return PixelFormat::fromString(str);
>  		}))
>  		.def_property_readonly("fourcc", &PixelFormat::fourcc)

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list