1 | #ifndef FASTUIDRAW_DEMO_PRINT_UTILS_HPP |
2 | #define FASTUIDRAW_DEMO_PRINT_UTILS_HPP |
3 | |
4 | #include <iostream> |
5 | #include <stdint.h> |
6 | #include <fastuidraw/util/util.hpp> |
7 | |
8 | class PrintBytes |
9 | { |
10 | public: |
11 | enum rounding_mode_t |
12 | { |
13 | round_to_highest_unit = 0, |
14 | round_to_mb_or_highest_unit = 1, |
15 | round_to_kb_or_highest_unit = 2, |
16 | do_not_round= 3, |
17 | }; |
18 | |
19 | explicit |
20 | PrintBytes(uint64_t v, enum rounding_mode_t r = round_to_kb_or_highest_unit): |
21 | m_gb(fastuidraw::uint64_unpack_bits(30u, 34u, v)), |
22 | m_mb(fastuidraw::uint64_unpack_bits(20u, 10u, v)), |
23 | m_kb(fastuidraw::uint64_unpack_bits(10u, 10u, v)), |
24 | m_b(fastuidraw::uint64_unpack_bits(0u, 10u, v)), |
25 | m_rounding_mode(r) |
26 | {} |
27 | |
28 | uint64_t m_gb, m_mb, m_kb, m_b; |
29 | enum rounding_mode_t m_rounding_mode; |
30 | }; |
31 | |
32 | inline |
33 | std::ostream& |
34 | operator<<(std::ostream &str, const PrintBytes &obj) |
35 | { |
36 | bool print_spe(false), print(true); |
37 | char spe(' '); |
38 | |
39 | if (obj.m_gb && print) |
40 | { |
41 | str << obj.m_gb << "GB" ; |
42 | print_spe = true; |
43 | print = (obj.m_rounding_mode > PrintBytes::round_to_highest_unit); |
44 | } |
45 | |
46 | if (obj.m_mb && print) |
47 | { |
48 | if (print_spe) |
49 | { |
50 | str << spe; |
51 | } |
52 | str << obj.m_mb << "MB" ; |
53 | print_spe = true; |
54 | print = (obj.m_rounding_mode > PrintBytes::round_to_mb_or_highest_unit); |
55 | } |
56 | |
57 | if (obj.m_kb && print) |
58 | { |
59 | if (print_spe) |
60 | { |
61 | str << spe; |
62 | } |
63 | str << obj.m_kb << "KB" ; |
64 | print_spe = true; |
65 | print = (obj.m_rounding_mode > PrintBytes::round_to_kb_or_highest_unit); |
66 | } |
67 | |
68 | if (obj.m_b && print) |
69 | { |
70 | if (print_spe) |
71 | { |
72 | str << spe; |
73 | } |
74 | str << obj.m_b << "B" ; |
75 | } |
76 | return str; |
77 | } |
78 | |
79 | #endif |
80 | |