From 2eab5b7b9a891f78e510fcdc42432212e69924d1 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 13 Mar 2021 18:46:09 +0300 Subject: [PATCH] Add memory size pretty printing --- .../util/include/psemek/util/pretty_print.hpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libs/util/include/psemek/util/pretty_print.hpp b/libs/util/include/psemek/util/pretty_print.hpp index 4bde9c97..ee5ccf89 100644 --- a/libs/util/include/psemek/util/pretty_print.hpp +++ b/libs/util/include/psemek/util/pretty_print.hpp @@ -2,6 +2,7 @@ #include #include +#include namespace psemek::util { @@ -27,6 +28,23 @@ namespace psemek::util return o; } + struct pretty_print_memsize_wrapper + { + std::size_t value; + }; + + inline std::ostream & operator << (std::ostream & o, pretty_print_memsize_wrapper m) + { + if (m.value < (1 << 10)) + return o << m.value << " b"; + else if (m.value < (1 << 20)) + return o << std::setprecision(3) << (m.value * 1.f / (1 << 10)) << " Kb"; + else if (m.value < (1 << 30)) + return o << std::setprecision(3) << (m.value * 1.f / (1 << 20)) << " Mb"; + else + return o << std::setprecision(3) << (m.value * 1.f / (1 << 30)) << " Gb"; + } + } template @@ -41,4 +59,9 @@ namespace psemek::util return pretty(d, std::chrono::seconds{1}); } + inline auto pretty_memsize(std::size_t value) + { + return detail::pretty_print_memsize_wrapper{value}; + } + }