diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-10-13 22:22:04 +0300 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-10-15 16:49:55 +0300 |
commit | f391048a7b987a149d0ba5421846b9b5ab916338 (patch) | |
tree | de82297ab5c83cc530d51bd9e76040f6d0b909f3 /src/libcamera/include/utils.h | |
parent | ce739e616c42105e5c6f22bf4df5dc19b5e8c8f9 (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/include/utils.h')
-rw-r--r-- | src/libcamera/include/utils.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 52eee8ac..3efb11c1 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -10,6 +10,7 @@ #include <algorithm> #include <chrono> #include <memory> +#include <ostream> #include <string> #include <sys/time.h> @@ -63,6 +64,45 @@ using time_point = std::chrono::steady_clock::time_point; struct timespec duration_to_timespec(const duration &value); std::string time_point_to_string(const time_point &time); +#ifndef __DOXYGEN__ +struct _hex { + uint64_t v; + unsigned int w; +}; + +std::basic_ostream<char, std::char_traits<char>> & +operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h); +#endif + +template<typename T> +_hex hex(T value, unsigned int width = 0); + +#ifndef __DOXYGEN__ +template<> +inline _hex hex<int32_t>(int32_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 8 }; +} + +template<> +inline _hex hex<uint32_t>(uint32_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 8 }; +} + +template<> +inline _hex hex<int64_t>(int64_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 16 }; +} + +template<> +inline _hex hex<uint64_t>(uint64_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 16 }; +} +#endif + } /* namespace utils */ } /* namespace libcamera */ |