• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 PANDA_LIBPANDABASE_UTILS_TYPE_CONVERTER_H_
17 #define PANDA_LIBPANDABASE_UTILS_TYPE_CONVERTER_H_
18 
19 #include <array>
20 #include <cassert>
21 #include <cmath>
22 #include <iomanip>
23 #include <string_view>
24 #include <variant>
25 
26 #include "macros.h"
27 
28 namespace panda::helpers {
29 
30 /**
31  * \brief Class for presentation \param value_ in a readable format.
32  * Presented as two fields: \param value_ and its \param literal_
33  * @param value_ - double number of value
34  * @param literal_ - string designations of the unit
35  * @param precision_ - precision for output
36  */
37 class ValueUnit {
38 public:
39     ValueUnit(uint64_t value, std::string_view literal);
40 
41     ValueUnit(double value, std::string_view literal);
42 
43     /**
44      *  \brief Set new \param precision for output
45      *  @param precision
46      */
47     void SetPrecision(size_t precision);
48 
49     std::variant<double, uint64_t> GetValue() const;
50 
51     double GetDoubleValue() const;
52 
53     uint64_t GetUint64Value() const;
54 
55     std::string_view GetLiteral() const;
56 
57     size_t GetPrecision() const;
58 
59     virtual ~ValueUnit() = default;
60 
61     DEFAULT_COPY_SEMANTIC(ValueUnit);
62     DEFAULT_MOVE_SEMANTIC(ValueUnit);
63 
64 private:
65     std::variant<double, uint64_t> value_;
66     std::string_view literal_;
67     size_t precision_ = DEFAULT_PRECISION;
68 
69     static constexpr size_t DEFAULT_PRECISION = 3;
70 };
71 
72 bool operator==(const ValueUnit &lhs, const ValueUnit &rhs);
73 bool operator!=(const ValueUnit &lhs, const ValueUnit &rhs);
74 
75 std::ostream &operator<<(std::ostream &os, const ValueUnit &element);
76 
77 enum class ValueType {
78     VALUE_TYPE_OBJECT,  // Type for objects
79     VALUE_TYPE_TIME,    // Type for time
80     VALUE_TYPE_MEMORY   // Type for memory
81 };
82 
83 constexpr std::array<double, 4> COEFFS_MEMORY = {1024, 1024, 1024, 1024};
84 constexpr std::array<double, 6> COEFFS_TIME = {1000, 1000, 1000, 60, 60, 24};
85 
86 constexpr std::array<std::string_view, 5> LITERALS_MEMORY = {"B", "KB", "MB", "GB", "TB"};
87 constexpr std::array<std::string_view, 7> LITERALS_TIME = {"ns", "us", "ms", "s", "m", "h", "day"};
88 
89 /**
90  *  Convert time from nanoseconds to readable format
91  *  @param time_in_nanos
92  */
93 ValueUnit TimeConverter(uint64_t times_in_nanos);
94 
95 /**
96  *  Convert memory from bytes to readable format
97  *  @param bytes
98  */
99 ValueUnit MemoryConverter(uint64_t bytes);
100 
101 /**
102  *  Convert any value
103  *  @param value of type
104  *  @param type what type is value
105  */
106 ValueUnit ValueConverter(uint64_t value, ValueType type);
107 
108 }  // namespace panda::helpers
109 
110 #endif  // PANDA_LIBPANDABASE_UTILS_TYPE_CONVERTER_H_
111