[libcamera-devel] [PATCH 4/5] libcamera: Add operator<<() for V4L2 format classes

Laurent Pinchart laurent.pinchart at ideasonboard.com
Fri Apr 29 23:23:47 CEST 2022


Implement the stream output operator<<() for the V4L2DeviceFormat and
V4L2SubdeviceFormat classes to simplify printing them.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 include/libcamera/internal/v4l2_subdevice.h   |  3 ++
 include/libcamera/internal/v4l2_videodevice.h |  3 ++
 src/libcamera/v4l2_subdevice.cpp              | 34 +++++++++++++------
 src/libcamera/v4l2_videodevice.cpp            | 16 ++++++++-
 4 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h
index 58d1e5114e7b..6fda52ada41b 100644
--- a/include/libcamera/internal/v4l2_subdevice.h
+++ b/include/libcamera/internal/v4l2_subdevice.h
@@ -9,6 +9,7 @@
 
 #include <memory>
 #include <optional>
+#include <ostream>
 #include <string>
 #include <vector>
 
@@ -35,6 +36,8 @@ struct V4L2SubdeviceFormat {
 	uint8_t bitsPerPixel() const;
 };
 
+std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f);
+
 class V4L2Subdevice : public V4L2Device
 {
 public:
diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h
index 9c9493cc16ed..23f37f83f8e2 100644
--- a/include/libcamera/internal/v4l2_videodevice.h
+++ b/include/libcamera/internal/v4l2_videodevice.h
@@ -11,6 +11,7 @@
 #include <atomic>
 #include <memory>
 #include <optional>
+#include <ostream>
 #include <stdint.h>
 #include <string>
 #include <vector>
@@ -180,6 +181,8 @@ public:
 	const std::string toString() const;
 };
 
+std::ostream &operator<<(std::ostream &out, const V4L2DeviceFormat &f);
+
 class V4L2VideoDevice : public V4L2Device
 {
 public:
diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp
index d5ae460571d8..b3d0ddad83db 100644
--- a/src/libcamera/v4l2_subdevice.cpp
+++ b/src/libcamera/v4l2_subdevice.cpp
@@ -190,17 +190,10 @@ const std::map<uint32_t, V4L2SubdeviceFormatInfo> formatInfoMap = {
  */
 const std::string V4L2SubdeviceFormat::toString() const
 {
-	std::stringstream mbus;
-	mbus << size << "-";
+	std::stringstream ss;
+	ss << *this;
 
-	const auto it = formatInfoMap.find(mbus_code);
-
-	if (it == formatInfoMap.end())
-		mbus << utils::hex(mbus_code, 4);
-	else
-		mbus << it->second.name;
-
-	return mbus.str();
+	return ss.str();
 }
 
 /**
@@ -220,6 +213,27 @@ uint8_t V4L2SubdeviceFormat::bitsPerPixel() const
 	return it->second.bitsPerPixel;
 }
 
+/**
+ * \brief Insert a text representation of a V4L2SubdeviceFormat into an output
+ * stream
+ * \param[in] out The output stream
+ * \param[in] f The V4L2SubdeviceFormat
+ * \return The output stream \a out
+ */
+std::ostream &operator<<(std::ostream &out, const V4L2SubdeviceFormat &f)
+{
+	out << f.size << "-";
+
+	const auto it = formatInfoMap.find(f.mbus_code);
+
+	if (it == formatInfoMap.end())
+		out << utils::hex(f.mbus_code, 4);
+	else
+		out << it->second.name;
+
+	return out;
+}
+
 /**
  * \class V4L2Subdevice
  * \brief A V4L2 subdevice as exposed by the Linux kernel
diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp
index 5ba866ad71bb..5b4637b1a39e 100644
--- a/src/libcamera/v4l2_videodevice.cpp
+++ b/src/libcamera/v4l2_videodevice.cpp
@@ -433,10 +433,24 @@ bool V4L2BufferCache::Entry::operator==(const FrameBuffer &buffer) const
 const std::string V4L2DeviceFormat::toString() const
 {
 	std::stringstream ss;
-	ss << size << "-" << fourcc.toString();
+	ss << *this;
+
 	return ss.str();
 }
 
+/**
+ * \brief Insert a text representation of a V4L2DeviceFormat into an output
+ * stream
+ * \param[in] out The output stream
+ * \param[in] f The V4L2DeviceFormat
+ * \return The output stream \a out
+ */
+std::ostream &operator<<(std::ostream &out, const V4L2DeviceFormat &f)
+{
+	out << f.size << "-" << f.fourcc;
+	return out;
+}
+
 /**
  * \class V4L2VideoDevice
  * \brief V4L2VideoDevice object and API
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list