[7/8] libcamera: vector: Add explicit cast and data accessor
diff mbox series

Message ID 20250411130423.2164577-8-stefan.klug@ideasonboard.com
State New
Headers show
Series
  • Implement WDR algorithm
Related show

Commit Message

Stefan Klug April 11, 2025, 1:04 p.m. UTC
When dealing with Vectors it is useful to be able to cast the to a
different underlying type or to access the underlying data.

Add functions for that.

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
---
 include/libcamera/internal/vector.h | 11 +++++++++++
 src/libcamera/vector.cpp            | 18 ++++++++++++++++++
 2 files changed, 29 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h
index 16b6aef0b38f..63da55019aef 100644
--- a/include/libcamera/internal/vector.h
+++ b/include/libcamera/internal/vector.h
@@ -63,6 +63,17 @@  public:
 		return data_[i];
 	}
 
+	template<typename T2>
+	explicit operator Vector<T2, Rows>() const
+	{
+		Vector<T2, Rows> ret;
+		for (unsigned int i = 0; i < Rows; i++)
+			ret[i] = static_cast<T2>(data_[i]);
+		return ret;
+	}
+
+	constexpr Span<const T, Rows> data() const { return data_; }
+
 	constexpr Vector<T, Rows> operator-() const
 	{
 		Vector<T, Rows> ret;
diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp
index 4dad1b9001c5..bb89f0de7c4f 100644
--- a/src/libcamera/vector.cpp
+++ b/src/libcamera/vector.cpp
@@ -64,6 +64,24 @@  LOG_DEFINE_CATEGORY(Vector)
  * \copydoc Vector::operator[](size_t i) const
  */
 
+/**
+ * \fn Vector::data()
+ * \brief Access the vector data
+ *
+ * Access the contents of the vector as linear array of values.
+ *
+ * \return A span referencing the vector data as a linear array
+ */
+
+/**
+ * \fn Vector::operator Vector<T2, Rows>()
+ * \brief Cast to a different underlying type
+ *
+ * Cast the vector to a different underlying type using static_cast().
+ *
+ * \return A vector of type T2
+ */
+
 /**
  * \fn Vector::operator-() const
  * \brief Negate a Vector by negating both all of its coordinates