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 
129 namespace {
130 constexpr float INF_APPROACH = Infinity<float>() * 0.5f;
131 }
132 
NearEqual(const double left,const double right,const double epsilon)133 inline bool NearEqual(const double left, const double right, const double epsilon)
134 {
135     return (std::abs(left - right) <= epsilon);
136 }
137 
138 template<typename T>
139 constexpr bool NearEqual(const T& left, const T& right);
140 
141 template<>
142 inline bool NearEqual<float>(const float& left, const float& right)
143 {
144     constexpr double epsilon = 0.001f;
145     return NearEqual(left, right, epsilon);
146 }
147 
148 template<>
149 inline bool NearEqual<double>(const double& left, const double& right)
150 {
151     constexpr double epsilon = 0.00001f;
152     return NearEqual(left, right, epsilon);
153 }
154 
155 template<typename T>
NearEqual(const T & left,const T & right)156 constexpr bool NearEqual(const T& left, const T& right)
157 {
158     return left == right;
159 }
160 
NearZero(const double value,const double epsilon)161 inline bool NearZero(const double value, const double epsilon)
162 {
163     return NearEqual(value, 0.0, epsilon);
164 }
165 
NearEqual(const double left,const double right)166 inline bool NearEqual(const double left, const double right)
167 {
168     constexpr double epsilon = 0.001f;
169     return NearEqual(left, right, epsilon);
170 }
171 
NearZero(const double left)172 inline bool NearZero(const double left)
173 {
174     constexpr double epsilon = 0.001f;
175     return NearZero(left, epsilon);
176 }
177 
LessOrEqual(double left,double right)178 inline bool LessOrEqual(double left, double right)
179 {
180     constexpr double epsilon = 0.001f;
181     return (left - right) < epsilon;
182 }
183 
184 inline bool LessOrEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
185 {
186     return (left - right) < epsilon;
187 }
188 
LessNotEqual(double left,double right)189 inline bool LessNotEqual(double left, double right)
190 {
191     constexpr double epsilon = -0.001f;
192     return (left - right) < epsilon;
193 }
194 
195 inline bool LessNotEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
196 {
197     return (left - right) < epsilon;
198 }
199 
GreatOrEqual(double left,double right)200 inline bool GreatOrEqual(double left, double right)
201 {
202     constexpr double epsilon = -0.001f;
203     return (left - right) > epsilon;
204 }
205 
206 inline bool GreatOrEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
207 {
208     return (left - right) > epsilon;
209 }
210 
GreatNotEqual(double left,double right)211 inline bool GreatNotEqual(double left, double right)
212 {
213     constexpr double epsilon = 0.001f;
214     return (left - right) > epsilon;
215 }
216 
217 inline bool GreatNotEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
218 {
219     return (left - right) > epsilon;
220 }
221 
Round(double rawNum)222 inline double Round(double rawNum)
223 {
224     constexpr double epsilon = 0.001f;
225     return std::round(rawNum + epsilon);
226 }
227 
Negative(double value)228 inline bool Negative(double value)
229 {
230     return LessNotEqual(value, 0);
231 }
232 
NonNegative(double value)233 inline bool NonNegative(double value)
234 {
235     return GreatOrEqual(value, 0);
236 }
237 
Positive(double value)238 inline bool Positive(double value)
239 {
240     return GreatNotEqual(value, 0);
241 }
242 
NonPositive(double value)243 inline bool NonPositive(double value)
244 {
245     return LessOrEqual(value, 0);
246 }
247 
InRegion(double lowerBound,double upperBound,double destNum)248 inline bool InRegion(double lowerBound, double upperBound, double destNum)
249 {
250     return LessOrEqual(lowerBound, destNum) && LessOrEqual(destNum, upperBound);
251 }
252 
GreaterOrEqualToInfinity(float num)253 inline bool GreaterOrEqualToInfinity(float num)
254 {
255     return GreatOrEqual(num, INF_APPROACH);
256 }
257 
GetMilliseconds()258 inline uint64_t GetMilliseconds()
259 {
260     auto now = std::chrono::system_clock::now();
261     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
262     return millisecs.count();
263 }
264 
GetNanoseconds()265 inline uint64_t GetNanoseconds()
266 {
267     auto now = std::chrono::system_clock::now();
268     auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
269     return nanoseconds.count();
270 }
271 
CalculateFriction(float gamma)272 inline float CalculateFriction(float gamma)
273 {
274     constexpr float SCROLL_RATIO = 0.72f;
275     if (GreatOrEqual(gamma, 1.0)) {
276         gamma = 1.0;
277     }
278     return SCROLL_RATIO * static_cast<float>(std::pow(1.0 - gamma, 2));
279 }
280 
IsDarkColor(uint32_t color)281 inline bool IsDarkColor(uint32_t color)
282 {
283     constexpr int lightThresholds = 128;
284     int r = (color >> 16) & 0xFF;
285     int g = (color >> 8) & 0xFF;
286     int b = color & 0xFF;
287     int gray = (r * 299 + g * 587 + b * 114) / 1000;
288     return gray < lightThresholds;
289 }
290 
291 bool RealPath(const std::string& fileName, char* realPath);
292 
293 } // namespace OHOS::Ace
294 
295 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
296