• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBPANDABASE_UTILS_TYPE_CONVERTER_H
17 #define LIBPANDABASE_UTILS_TYPE_CONVERTER_H
18 
19 #include <array>
20 #include <cstddef>
21 #include <cstdint>
22 #include <iosfwd>
23 #include <string_view>
24 #include <variant>
25 
26 #include "macros.h"
27 
28 WEAK_FOR_LTO_START
29 
30 namespace panda::helpers {
31 
32 /**
33  * \brief Class for presentation \param value_ in a readable format.
34  * Presented as two fields: \param value_ and its \param literal_
35  * @param value_ - double number of value
36  * @param literal_ - string designations of the unit
37  * @param precision_ - precision for output
38  */
39 class ValueUnit {
40 public:
41     ValueUnit(uint64_t value, std::string_view literal);
42 
43     ValueUnit(double value, std::string_view literal);
44 
45     /**
46      *  \brief Set new \param precision for output
47      *  @param precision
48      */
49     void SetPrecision(size_t precision);
50 
51     std::variant<double, uint64_t> GetValue() const;
52 
53     double GetDoubleValue() const;
54 
55     uint64_t GetUint64Value() const;
56 
57     std::string_view GetLiteral() const;
58 
59     size_t GetPrecision() const;
60 
61     virtual ~ValueUnit() = default;
62 
63     DEFAULT_COPY_SEMANTIC(ValueUnit);
64     DEFAULT_MOVE_SEMANTIC(ValueUnit);
65 
66 private:
67     std::variant<double, uint64_t> value_;
68     std::string_view literal_;
69     size_t precision_ = DEFAULT_PRECISION;
70 
71     static constexpr size_t DEFAULT_PRECISION = 3;
72 };
73 
74 bool operator==(const ValueUnit &lhs, const ValueUnit &rhs);
75 bool operator!=(const ValueUnit &lhs, const ValueUnit &rhs);
76 
77 std::ostream &operator<<(std::ostream &os, const ValueUnit &element);
78 
79 enum class ValueType {
80     VALUE_TYPE_OBJECT,  // Type for objects
81     VALUE_TYPE_TIME,    // Type for time
82     VALUE_TYPE_MEMORY   // Type for memory
83 };
84 
85 constexpr std::array<double, 4> COEFFS_MEMORY = {1024, 1024, 1024, 1024};
86 constexpr std::array<double, 6> COEFFS_TIME = {1000, 1000, 1000, 60, 60, 24};
87 
88 constexpr std::array<std::string_view, 5> LITERALS_MEMORY = {"B", "KB", "MB", "GB", "TB"};
89 constexpr std::array<std::string_view, 7> LITERALS_TIME = {"ns", "us", "ms", "s", "m", "h", "day"};
90 
91 /**
92  *  Convert time from nanoseconds to readable format
93  *  @param time_in_nanos
94  */
95 ValueUnit TimeConverter(uint64_t times_in_nanos);
96 
97 /**
98  *  Convert memory from bytes to readable format
99  *  @param bytes
100  */
101 ValueUnit MemoryConverter(uint64_t bytes);
102 
103 /**
104  *  Convert any value
105  *  @param value of type
106  *  @param type what type is value
107  */
108 ValueUnit ValueConverter(uint64_t value, ValueType type);
109 
110 }  // namespace panda::helpers
111 
112 WEAK_FOR_LTO_END
113 
114 #endif  // LIBPANDABASE_UTILS_TYPE_CONVERTER_H
115