• 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 #include "base/log/log.h"
24 
25 #define CHECK_NULL_VOID(ptr) \
26     do {                           \
27         if (!(ptr)) {              \
28             return;                \
29         }                          \
30     } while (0)                    \
31 
32 #define CHECK_NULL_RETURN(ptr, ret) \
33     do {                                  \
34         if (!(ptr)) {                     \
35             return ret;                   \
36         }                                 \
37     } while (0)                           \
38 
39 #define CHECK_ERROR_CODE_RETURN(code) \
40     do {                                  \
41         if ((code) > 0) {                     \
42             return code;                   \
43         }                                 \
44     } while (0)                           \
45 
46 #define CHECK_EQUAL_VOID(var, value) \
47     do {                             \
48         if ((var) == (value)) {      \
49             return;                  \
50         }                            \
51     } while (0)                      \
52 
53 #define CHECK_EQUAL_RETURN(var, value, ret) \
54     do {                                    \
55         if ((var) == (value)) {             \
56             return ret;                     \
57         }                                   \
58     } while (0)                             \
59 
60 #define PRIMITIVE_CAT(x, y) x##y
61 #define CAT(x, y) PRIMITIVE_CAT(x, y)
62 
63 #define COPY_SENTENCE(x) x = other.x;
64 #define LOOP_COPY(x) CAT(LOOP_COPY1 x, _END)
65 #define LOOP_COPY1(x) COPY_SENTENCE(x) LOOP_COPY2
66 #define LOOP_COPY2(x) COPY_SENTENCE(x) LOOP_COPY1
67 #define LOOP_COPY1_END
68 #define LOOP_COPY2_END
69 
70 #define COMPARE_SENTENCE(x) (x == other.x)
71 #define LOOP_COMPARE(x) CAT(LOOP_COMPARE0 x, _END)
72 #define LOOP_COMPARE0(x) COMPARE_SENTENCE(x) LOOP_COMPARE1
73 #define LOOP_COMPARE1(x) &&COMPARE_SENTENCE(x) LOOP_COMPARE2
74 #define LOOP_COMPARE2(x) &&COMPARE_SENTENCE(x) LOOP_COMPARE1
75 #define LOOP_COMPARE1_END
76 #define LOOP_COMPARE2_END
77 
78 #define DEFINE_COPY_CONSTRUCTOR(type) \
79     type(const type& other)           \
80     {                                 \
81         *this = other;                \
82     }
83 
84 #define DEFINE_COPY_OPERATOR_WITH_PROPERTIES(type, PROPS) \
85     type& operator=(const type& other)                    \
86     {                                                     \
87         if (&other != this) {                             \
88             LOOP_COPY(PROPS)                              \
89         }                                                 \
90         return *this;                                     \
91     }
92 
93 #define DEFINE_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS) \
94     bool operator==(const type& other) const                 \
95     {                                                        \
96         if (&other == this) {                                \
97             return true;                                     \
98         }                                                    \
99         return LOOP_COMPARE(PROPS);                          \
100     }
101 
102 #define DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS) \
103     DEFINE_COPY_CONSTRUCTOR(type)                                                                   \
104     DEFINE_COPY_OPERATOR_WITH_PROPERTIES(type, PROPS) DEFINE_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS)
105 
106 namespace OHOS::Ace {
107 
108 template<typename T, std::size_t N>
ArraySize(T (&)[N])109 constexpr std::size_t ArraySize(T (&)[N]) noexcept
110 {
111     return N;
112 }
113 
114 template<typename T, int32_t N>
ConvertIntToEnum(int32_t index,const T (& values)[N],T defaultValue)115 T ConvertIntToEnum(int32_t index, const T (&values)[N], T defaultValue)
116 {
117     if (index >= 0 && index < N) {
118         return values[index];
119     }
120     return defaultValue;
121 }
122 
123 template<typename T>
Infinity()124 constexpr T Infinity()
125 {
126     return static_cast<const T>(1000000.0);
127 }
128 
NearEqual(const double left,const double right,const double epsilon)129 inline bool NearEqual(const double left, const double right, const double epsilon)
130 {
131     return (std::abs(left - right) <= epsilon);
132 }
133 
134 template<typename T>
135 constexpr bool NearEqual(const T& left, const T& right);
136 
137 template<>
138 inline bool NearEqual<float>(const float& left, const float& right)
139 {
140     constexpr double epsilon = 0.001f;
141     return NearEqual(left, right, epsilon);
142 }
143 
144 template<>
145 inline bool NearEqual<double>(const double& left, const double& right)
146 {
147     constexpr double epsilon = 0.00001f;
148     return NearEqual(left, right, epsilon);
149 }
150 
151 template<typename T>
NearEqual(const T & left,const T & right)152 constexpr bool NearEqual(const T& left, const T& right)
153 {
154     return left == right;
155 }
156 
NearZero(const double value,const double epsilon)157 inline bool NearZero(const double value, const double epsilon)
158 {
159     return NearEqual(value, 0.0, epsilon);
160 }
161 
NearEqual(const double left,const double right)162 inline bool NearEqual(const double left, const double right)
163 {
164     constexpr double epsilon = 0.001f;
165     return NearEqual(left, right, epsilon);
166 }
167 
NearZero(const double left)168 inline bool NearZero(const double left)
169 {
170     constexpr double epsilon = 0.001f;
171     return NearZero(left, epsilon);
172 }
173 
LessOrEqual(double left,double right)174 inline bool LessOrEqual(double left, double right)
175 {
176     constexpr double epsilon = 0.001f;
177     return (left - right) < epsilon;
178 }
179 
180 inline bool LessOrEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
181 {
182     return (left - right) < epsilon;
183 }
184 
LessNotEqual(double left,double right)185 inline bool LessNotEqual(double left, double right)
186 {
187     constexpr double epsilon = -0.001f;
188     return (left - right) < epsilon;
189 }
190 
191 inline bool LessNotEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
192 {
193     return (left - right) < epsilon;
194 }
195 
GreatOrEqual(double left,double right)196 inline bool GreatOrEqual(double left, double right)
197 {
198     constexpr double epsilon = -0.001f;
199     return (left - right) > epsilon;
200 }
201 
202 inline bool GreatOrEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
203 {
204     return (left - right) > epsilon;
205 }
206 
GreatNotEqual(double left,double right)207 inline bool GreatNotEqual(double left, double right)
208 {
209     constexpr double epsilon = 0.001f;
210     return (left - right) > epsilon;
211 }
212 
213 inline bool GreatNotEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
214 {
215     return (left - right) > epsilon;
216 }
217 
Round(double rawNum)218 inline double Round(double rawNum)
219 {
220     constexpr double epsilon = 0.001f;
221     return std::round(rawNum + epsilon);
222 }
223 
Negative(double value)224 inline bool Negative(double value)
225 {
226     return LessNotEqual(value, 0);
227 }
228 
NonNegative(double value)229 inline bool NonNegative(double value)
230 {
231     return GreatOrEqual(value, 0);
232 }
233 
Positive(double value)234 inline bool Positive(double value)
235 {
236     return GreatNotEqual(value, 0);
237 }
238 
NonPositive(double value)239 inline bool NonPositive(double value)
240 {
241     return LessOrEqual(value, 0);
242 }
243 
InRegion(double lowerBound,double upperBound,double destNum)244 inline bool InRegion(double lowerBound, double upperBound, double destNum)
245 {
246     return LessOrEqual(lowerBound, destNum) && LessOrEqual(destNum, upperBound);
247 }
248 
GreaterOrEqualToInfinity(float num)249 inline bool GreaterOrEqualToInfinity(float num)
250 {
251     return GreatOrEqual(num, Infinity<float>() / 2.0f);
252 }
253 
GetMilliseconds()254 inline uint64_t GetMilliseconds()
255 {
256     auto now = std::chrono::system_clock::now();
257     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
258     return millisecs.count();
259 }
260 
GetNanoseconds()261 inline uint64_t GetNanoseconds()
262 {
263     auto now = std::chrono::system_clock::now();
264     auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
265     return nanoseconds.count();
266 }
267 
CalculateFriction(float gamma)268 inline float CalculateFriction(float gamma)
269 {
270     constexpr float SCROLL_RATIO = 0.72f;
271     if (GreatOrEqual(gamma, 1.0)) {
272         gamma = 1.0;
273     }
274     return SCROLL_RATIO * static_cast<float>(std::pow(1.0 - gamma, 2));
275 }
276 
277 bool RealPath(const std::string fileName, char* realPath);
278 
279 } // namespace OHOS::Ace
280 
281 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
282