• 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 FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
18 
19 #include <chrono>
20 #include <cmath>
21 #include <cstdint>
22 
23 namespace OHOS::Ace {
24 
25 template<typename T, std::size_t N>
ArraySize(T (&)[N])26 constexpr std::size_t ArraySize(T (&)[N]) noexcept
27 {
28     return N;
29 }
30 
31 template<typename T, int32_t N>
ConvertIntToEnum(int32_t index,const T (& values)[N],T defaultValue)32 T ConvertIntToEnum(int32_t index, const T (&values)[N], T defaultValue)
33 {
34     if (index >= 0 && index < N) {
35         return values[index];
36     }
37     return defaultValue;
38 }
39 
NearEqual(const double left,const double right,const double epsilon)40 inline bool NearEqual(const double left, const double right, const double epsilon)
41 {
42     return (std::abs(left - right) <= epsilon);
43 }
44 
NearZero(const double value,const double epsilon)45 inline bool NearZero(const double value, const double epsilon)
46 {
47     return NearEqual(value, 0.0, epsilon);
48 }
49 
NearEqual(const double left,const double right)50 inline bool NearEqual(const double left, const double right)
51 {
52     static constexpr double epsilon = 0.000001f;
53     return NearEqual(left, right, epsilon);
54 }
55 
NearZero(const double left)56 inline bool NearZero(const double left)
57 {
58     static constexpr double epsilon = 0.000001f;
59     return NearZero(left, epsilon);
60 }
61 
LessOrEqual(double left,double right)62 inline bool LessOrEqual(double left, double right)
63 {
64     static constexpr double epsilon = 0.000001f;
65     return (left - right) < epsilon;
66 }
67 
LessNotEqual(double left,double right)68 inline bool LessNotEqual(double left, double right)
69 {
70     static constexpr double epsilon = -0.000001f;
71     return (left - right) < epsilon;
72 }
73 
GreatOrEqual(double left,double right)74 inline bool GreatOrEqual(double left, double right)
75 {
76     static constexpr double epsilon = -0.000001f;
77     return (left - right) > epsilon;
78 }
79 
GreatNotEqual(double left,double right)80 inline bool GreatNotEqual(double left, double right)
81 {
82     static constexpr double epsilon = 0.000001f;
83     return (left - right) > epsilon;
84 }
85 
Round(double rawNum)86 inline double Round(double rawNum)
87 {
88     static constexpr double epsilon = 0.000001f;
89     return std::round(rawNum + epsilon);
90 }
91 
InRegion(double lowerBound,double upperBound,double destNum)92 inline bool InRegion(double lowerBound, double upperBound, double destNum)
93 {
94     return LessOrEqual(lowerBound, destNum) && LessOrEqual(destNum, upperBound);
95 }
96 
GetMilliseconds()97 inline uint64_t GetMilliseconds()
98 {
99     auto now = std::chrono::system_clock::now();
100     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
101     return millisecs.count();
102 }
103 
GetNanoseconds()104 inline uint64_t GetNanoseconds()
105 {
106     auto now = std::chrono::system_clock::now();
107     auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
108     return nanoseconds.count();
109 }
110 
111 } // namespace OHOS::Ace
112 
113 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
114