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