summaryrefslogtreecommitdiff
path: root/src/libcamera/utils.cpp
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-10-13 22:22:04 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2019-10-15 16:49:55 +0300
commitf391048a7b987a149d0ba5421846b9b5ab916338 (patch)
treede82297ab5c83cc530d51bd9e76040f6d0b909f3 /src/libcamera/utils.cpp
parentce739e616c42105e5c6f22bf4df5dc19b5e8c8f9 (diff)
libcamera: utils: Add hex stream output helper
Add a utils::hex() function that simplifies writing hexadecimal values to an ostream. The function handles the '0x' prefix, the field width and the fill character automatically. Use it through the libcamera code base, and add a test. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src/libcamera/utils.cpp')
-rw-r--r--src/libcamera/utils.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index 928db254..d632f6e6 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -143,6 +143,45 @@ std::string time_point_to_string(const time_point &time)
return ossTimestamp.str();
}
+std::basic_ostream<char, std::char_traits<char>> &
+operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h)
+{
+ stream << "0x";
+
+ std::ostream::fmtflags flags = stream.setf(std::ios_base::hex,
+ std::ios_base::basefield);
+ std::streamsize width = stream.width(h.w);
+ char fill = stream.fill('0');
+
+ stream << h.v;
+
+ stream.flags(flags);
+ stream.width(width);
+ stream.fill(fill);
+
+ return stream;
+}
+
+/**
+ * \fn hex(T value, unsigned int width)
+ * \brief Write an hexadecimal value to an output string
+ * \param value The value
+ * \param width The width
+ *
+ * Return an object of unspecified type such that, if \a os is the name of an
+ * output stream of type std::ostream, and T is an integer type, then the
+ * expression
+ *
+ * \code{.cpp}
+ * os << utils::hex(value)
+ * \endcode
+ *
+ * will output the \a value to the stream in hexadecimal form with the base
+ * prefix and the filling character set to '0'. The field width is set to \a
+ * width if specified to a non-zero value, or to the native width of type T
+ * otherwise. The \a os stream configuration is not modified.
+ */
+
} /* namespace utils */
} /* namespace libcamera */