summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNaushir Patuck <naush@raspberrypi.com>2021-06-08 12:03:32 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-06-08 23:56:03 +0300
commit5055ca747c4c0d635ed8f2ab272f493748c2f50a (patch)
tree66c5b36389a5d0f4bfe07f2edf5bf4877c413d30 /include
parent75c14908271d44edac3b7c8d0f94501a3c93477e (diff)
libcamera: utils: Add helper class for std::chrono::duration
A new utils::Duration class is defined to represent a std::chrono::duration type with double precision nanosecond timebase. Using a double minimises the loss of precision when converting timebases. This helper class may be used by IPAs to represent variables such as frame durations and exposure times. An operator << overload is defined to help with displaying utils::Duration value in stream objects. Currently, this will display the duration value in microseconds. Signed-off-by: Naushir Patuck <naush@raspberrypi.com> Reviewed-by: David Plowman <david.plowman@raspberrypi.com> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'include')
-rw-r--r--include/libcamera/internal/utils.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h
index 83dada7c..15beb0f4 100644
--- a/include/libcamera/internal/utils.h
+++ b/include/libcamera/internal/utils.h
@@ -316,8 +316,40 @@ auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
}
#endif
+class Duration : public std::chrono::duration<double, std::nano>
+{
+ using BaseDuration = std::chrono::duration<double, std::nano>;
+
+public:
+ Duration() = default;
+
+ template<typename Rep, typename Period>
+ constexpr Duration(const std::chrono::duration<Rep, Period> &d)
+ : BaseDuration(d)
+ {
+ }
+
+ template<typename Period>
+ double get() const
+ {
+ auto const c = std::chrono::duration_cast<std::chrono::duration<double, Period>>(*this);
+ return c.count();
+ }
+
+ explicit constexpr operator bool() const
+ {
+ return *this != BaseDuration::zero();
+ }
+};
+
} /* namespace utils */
+#ifndef __DOXYGEN__
+template<class CharT, class Traits>
+std::basic_ostream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os,
+ const utils::Duration &d);
+#endif
+
} /* namespace libcamera */
#endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */