123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include <chrono>
2023b3eb3cSopenharmony_ci#include <cmath>
2123b3eb3cSopenharmony_ci#include <cstdint>
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ci#include "base/log/log.h"
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_ci#define CHECK_NULL_VOID(ptr) \
2623b3eb3cSopenharmony_ci    do {                           \
2723b3eb3cSopenharmony_ci        if (!(ptr)) {              \
2823b3eb3cSopenharmony_ci            return;                \
2923b3eb3cSopenharmony_ci        }                          \
3023b3eb3cSopenharmony_ci    } while (0)                    \
3123b3eb3cSopenharmony_ci
3223b3eb3cSopenharmony_ci#define CHECK_NULL_RETURN(ptr, ret) \
3323b3eb3cSopenharmony_ci    do {                                  \
3423b3eb3cSopenharmony_ci        if (!(ptr)) {                     \
3523b3eb3cSopenharmony_ci            return ret;                   \
3623b3eb3cSopenharmony_ci        }                                 \
3723b3eb3cSopenharmony_ci    } while (0)                           \
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_ci#define CHECK_ERROR_CODE_RETURN(code) \
4023b3eb3cSopenharmony_ci    do {                                  \
4123b3eb3cSopenharmony_ci        if ((code) > 0) {                     \
4223b3eb3cSopenharmony_ci            return code;                   \
4323b3eb3cSopenharmony_ci        }                                 \
4423b3eb3cSopenharmony_ci    } while (0)                           \
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci#define CHECK_EQUAL_VOID(var, value) \
4723b3eb3cSopenharmony_ci    do {                             \
4823b3eb3cSopenharmony_ci        if ((var) == (value)) {      \
4923b3eb3cSopenharmony_ci            return;                  \
5023b3eb3cSopenharmony_ci        }                            \
5123b3eb3cSopenharmony_ci    } while (0)                      \
5223b3eb3cSopenharmony_ci
5323b3eb3cSopenharmony_ci#define CHECK_EQUAL_RETURN(var, value, ret) \
5423b3eb3cSopenharmony_ci    do {                                    \
5523b3eb3cSopenharmony_ci        if ((var) == (value)) {             \
5623b3eb3cSopenharmony_ci            return ret;                     \
5723b3eb3cSopenharmony_ci        }                                   \
5823b3eb3cSopenharmony_ci    } while (0)                             \
5923b3eb3cSopenharmony_ci
6023b3eb3cSopenharmony_ci#define CHECK_NULL_CONTINUE(ptr)                                    \
6123b3eb3cSopenharmony_ci    if (!(ptr)) {                                                   \
6223b3eb3cSopenharmony_ci        continue;                                                   \
6323b3eb3cSopenharmony_ci    }
6423b3eb3cSopenharmony_ci
6523b3eb3cSopenharmony_ci#define PRIMITIVE_CAT(x, y) x##y
6623b3eb3cSopenharmony_ci#define CAT(x, y) PRIMITIVE_CAT(x, y)
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_ci#define COPY_SENTENCE(x) x = other.x;
6923b3eb3cSopenharmony_ci#define LOOP_COPY(x) CAT(LOOP_COPY1 x, _END)
7023b3eb3cSopenharmony_ci#define LOOP_COPY1(x) COPY_SENTENCE(x) LOOP_COPY2
7123b3eb3cSopenharmony_ci#define LOOP_COPY2(x) COPY_SENTENCE(x) LOOP_COPY1
7223b3eb3cSopenharmony_ci#define LOOP_COPY1_END
7323b3eb3cSopenharmony_ci#define LOOP_COPY2_END
7423b3eb3cSopenharmony_ci
7523b3eb3cSopenharmony_ci#define COMPARE_SENTENCE(x) (x == other.x)
7623b3eb3cSopenharmony_ci#define LOOP_COMPARE(x) CAT(LOOP_COMPARE0 x, _END)
7723b3eb3cSopenharmony_ci#define LOOP_COMPARE0(x) COMPARE_SENTENCE(x) LOOP_COMPARE1
7823b3eb3cSopenharmony_ci#define LOOP_COMPARE1(x) &&COMPARE_SENTENCE(x) LOOP_COMPARE2
7923b3eb3cSopenharmony_ci#define LOOP_COMPARE2(x) &&COMPARE_SENTENCE(x) LOOP_COMPARE1
8023b3eb3cSopenharmony_ci#define LOOP_COMPARE1_END
8123b3eb3cSopenharmony_ci#define LOOP_COMPARE2_END
8223b3eb3cSopenharmony_ci
8323b3eb3cSopenharmony_ci#define DEFINE_COPY_CONSTRUCTOR(type) \
8423b3eb3cSopenharmony_ci    type(const type& other)           \
8523b3eb3cSopenharmony_ci    {                                 \
8623b3eb3cSopenharmony_ci        *this = other;                \
8723b3eb3cSopenharmony_ci    }
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_ci#define DEFINE_COPY_OPERATOR_WITH_PROPERTIES(type, PROPS) \
9023b3eb3cSopenharmony_ci    type& operator=(const type& other)                    \
9123b3eb3cSopenharmony_ci    {                                                     \
9223b3eb3cSopenharmony_ci        if (&other != this) {                             \
9323b3eb3cSopenharmony_ci            LOOP_COPY(PROPS)                              \
9423b3eb3cSopenharmony_ci        }                                                 \
9523b3eb3cSopenharmony_ci        return *this;                                     \
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_ci#define DEFINE_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS) \
9923b3eb3cSopenharmony_ci    bool operator==(const type& other) const                 \
10023b3eb3cSopenharmony_ci    {                                                        \
10123b3eb3cSopenharmony_ci        if (&other == this) {                                \
10223b3eb3cSopenharmony_ci            return true;                                     \
10323b3eb3cSopenharmony_ci        }                                                    \
10423b3eb3cSopenharmony_ci        return LOOP_COMPARE(PROPS);                          \
10523b3eb3cSopenharmony_ci    }
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_ci#define DEFINE_COPY_CONSTRUCTOR_AND_COPY_OPERATOR_AND_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS) \
10823b3eb3cSopenharmony_ci    DEFINE_COPY_CONSTRUCTOR(type)                                                                   \
10923b3eb3cSopenharmony_ci    DEFINE_COPY_OPERATOR_WITH_PROPERTIES(type, PROPS) DEFINE_COMPARE_OPERATOR_WITH_PROPERTIES(type, PROPS)
11023b3eb3cSopenharmony_ci
11123b3eb3cSopenharmony_cinamespace OHOS::Ace {
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_citemplate<typename T, std::size_t N>
11423b3eb3cSopenharmony_ciconstexpr std::size_t ArraySize(T (&)[N]) noexcept
11523b3eb3cSopenharmony_ci{
11623b3eb3cSopenharmony_ci    return N;
11723b3eb3cSopenharmony_ci}
11823b3eb3cSopenharmony_ci
11923b3eb3cSopenharmony_citemplate<typename T, int32_t N>
12023b3eb3cSopenharmony_ciT ConvertIntToEnum(int32_t index, const T (&values)[N], T defaultValue)
12123b3eb3cSopenharmony_ci{
12223b3eb3cSopenharmony_ci    if (index >= 0 && index < N) {
12323b3eb3cSopenharmony_ci        return values[index];
12423b3eb3cSopenharmony_ci    }
12523b3eb3cSopenharmony_ci    return defaultValue;
12623b3eb3cSopenharmony_ci}
12723b3eb3cSopenharmony_ci
12823b3eb3cSopenharmony_citemplate<typename T>
12923b3eb3cSopenharmony_ciconstexpr T Infinity()
13023b3eb3cSopenharmony_ci{
13123b3eb3cSopenharmony_ci    return static_cast<const T>(1000000.0);
13223b3eb3cSopenharmony_ci}
13323b3eb3cSopenharmony_ci
13423b3eb3cSopenharmony_cinamespace {
13523b3eb3cSopenharmony_ciconstexpr float INF_APPROACH = Infinity<float>() * 0.5f;
13623b3eb3cSopenharmony_ci}
13723b3eb3cSopenharmony_ci
13823b3eb3cSopenharmony_ciinline bool NearEqual(const double left, const double right, const double epsilon)
13923b3eb3cSopenharmony_ci{
14023b3eb3cSopenharmony_ci    return (std::abs(left - right) <= epsilon);
14123b3eb3cSopenharmony_ci}
14223b3eb3cSopenharmony_ci
14323b3eb3cSopenharmony_citemplate<typename T>
14423b3eb3cSopenharmony_ciconstexpr bool NearEqual(const T& left, const T& right);
14523b3eb3cSopenharmony_ci
14623b3eb3cSopenharmony_citemplate<>
14723b3eb3cSopenharmony_ciinline bool NearEqual<float>(const float& left, const float& right)
14823b3eb3cSopenharmony_ci{
14923b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
15023b3eb3cSopenharmony_ci    return NearEqual(left, right, epsilon);
15123b3eb3cSopenharmony_ci}
15223b3eb3cSopenharmony_ci
15323b3eb3cSopenharmony_citemplate<>
15423b3eb3cSopenharmony_ciinline bool NearEqual<double>(const double& left, const double& right)
15523b3eb3cSopenharmony_ci{
15623b3eb3cSopenharmony_ci    constexpr double epsilon = 0.00001f;
15723b3eb3cSopenharmony_ci    return NearEqual(left, right, epsilon);
15823b3eb3cSopenharmony_ci}
15923b3eb3cSopenharmony_ci
16023b3eb3cSopenharmony_citemplate<typename T>
16123b3eb3cSopenharmony_ciconstexpr bool NearEqual(const T& left, const T& right)
16223b3eb3cSopenharmony_ci{
16323b3eb3cSopenharmony_ci    return left == right;
16423b3eb3cSopenharmony_ci}
16523b3eb3cSopenharmony_ci
16623b3eb3cSopenharmony_ciinline bool NearZero(const double value, const double epsilon)
16723b3eb3cSopenharmony_ci{
16823b3eb3cSopenharmony_ci    return NearEqual(value, 0.0, epsilon);
16923b3eb3cSopenharmony_ci}
17023b3eb3cSopenharmony_ci
17123b3eb3cSopenharmony_ciinline bool NearEqual(const double left, const double right)
17223b3eb3cSopenharmony_ci{
17323b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
17423b3eb3cSopenharmony_ci    return NearEqual(left, right, epsilon);
17523b3eb3cSopenharmony_ci}
17623b3eb3cSopenharmony_ci
17723b3eb3cSopenharmony_ciinline bool NearZero(const double left)
17823b3eb3cSopenharmony_ci{
17923b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
18023b3eb3cSopenharmony_ci    return NearZero(left, epsilon);
18123b3eb3cSopenharmony_ci}
18223b3eb3cSopenharmony_ci
18323b3eb3cSopenharmony_ciinline bool LessOrEqual(double left, double right)
18423b3eb3cSopenharmony_ci{
18523b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
18623b3eb3cSopenharmony_ci    return (left - right) < epsilon;
18723b3eb3cSopenharmony_ci}
18823b3eb3cSopenharmony_ci
18923b3eb3cSopenharmony_ciinline bool LessOrEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
19023b3eb3cSopenharmony_ci{
19123b3eb3cSopenharmony_ci    return (left - right) < epsilon;
19223b3eb3cSopenharmony_ci}
19323b3eb3cSopenharmony_ci
19423b3eb3cSopenharmony_ciinline bool LessNotEqual(double left, double right)
19523b3eb3cSopenharmony_ci{
19623b3eb3cSopenharmony_ci    constexpr double epsilon = -0.001f;
19723b3eb3cSopenharmony_ci    return (left - right) < epsilon;
19823b3eb3cSopenharmony_ci}
19923b3eb3cSopenharmony_ci
20023b3eb3cSopenharmony_ciinline bool LessNotEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
20123b3eb3cSopenharmony_ci{
20223b3eb3cSopenharmony_ci    return (left - right) < epsilon;
20323b3eb3cSopenharmony_ci}
20423b3eb3cSopenharmony_ci
20523b3eb3cSopenharmony_ciinline bool GreatOrEqual(double left, double right)
20623b3eb3cSopenharmony_ci{
20723b3eb3cSopenharmony_ci    constexpr double epsilon = -0.001f;
20823b3eb3cSopenharmony_ci    return (left - right) > epsilon;
20923b3eb3cSopenharmony_ci}
21023b3eb3cSopenharmony_ci
21123b3eb3cSopenharmony_ciinline bool GreatOrEqualCustomPrecision(double left, double right, double epsilon = -0.000001f)
21223b3eb3cSopenharmony_ci{
21323b3eb3cSopenharmony_ci    return (left - right) > epsilon;
21423b3eb3cSopenharmony_ci}
21523b3eb3cSopenharmony_ci
21623b3eb3cSopenharmony_ciinline bool GreatNotEqual(double left, double right)
21723b3eb3cSopenharmony_ci{
21823b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
21923b3eb3cSopenharmony_ci    return (left - right) > epsilon;
22023b3eb3cSopenharmony_ci}
22123b3eb3cSopenharmony_ci
22223b3eb3cSopenharmony_ciinline bool GreatNotEqualCustomPrecision(double left, double right, double epsilon = 0.000001f)
22323b3eb3cSopenharmony_ci{
22423b3eb3cSopenharmony_ci    return (left - right) > epsilon;
22523b3eb3cSopenharmony_ci}
22623b3eb3cSopenharmony_ci
22723b3eb3cSopenharmony_ciinline double Round(double rawNum)
22823b3eb3cSopenharmony_ci{
22923b3eb3cSopenharmony_ci    constexpr double epsilon = 0.001f;
23023b3eb3cSopenharmony_ci    return std::round(rawNum + epsilon);
23123b3eb3cSopenharmony_ci}
23223b3eb3cSopenharmony_ci
23323b3eb3cSopenharmony_ciinline bool Negative(double value)
23423b3eb3cSopenharmony_ci{
23523b3eb3cSopenharmony_ci    return LessNotEqual(value, 0);
23623b3eb3cSopenharmony_ci}
23723b3eb3cSopenharmony_ci
23823b3eb3cSopenharmony_ciinline bool NonNegative(double value)
23923b3eb3cSopenharmony_ci{
24023b3eb3cSopenharmony_ci    return GreatOrEqual(value, 0);
24123b3eb3cSopenharmony_ci}
24223b3eb3cSopenharmony_ci
24323b3eb3cSopenharmony_ciinline bool Positive(double value)
24423b3eb3cSopenharmony_ci{
24523b3eb3cSopenharmony_ci    return GreatNotEqual(value, 0);
24623b3eb3cSopenharmony_ci}
24723b3eb3cSopenharmony_ci
24823b3eb3cSopenharmony_ciinline bool NonPositive(double value)
24923b3eb3cSopenharmony_ci{
25023b3eb3cSopenharmony_ci    return LessOrEqual(value, 0);
25123b3eb3cSopenharmony_ci}
25223b3eb3cSopenharmony_ci
25323b3eb3cSopenharmony_ciinline bool InRegion(double lowerBound, double upperBound, double destNum)
25423b3eb3cSopenharmony_ci{
25523b3eb3cSopenharmony_ci    return LessOrEqual(lowerBound, destNum) && LessOrEqual(destNum, upperBound);
25623b3eb3cSopenharmony_ci}
25723b3eb3cSopenharmony_ci
25823b3eb3cSopenharmony_ciinline bool GreaterOrEqualToInfinity(float num)
25923b3eb3cSopenharmony_ci{
26023b3eb3cSopenharmony_ci    return GreatOrEqual(num, INF_APPROACH);
26123b3eb3cSopenharmony_ci}
26223b3eb3cSopenharmony_ci
26323b3eb3cSopenharmony_ciinline uint64_t GetMilliseconds()
26423b3eb3cSopenharmony_ci{
26523b3eb3cSopenharmony_ci    auto now = std::chrono::system_clock::now();
26623b3eb3cSopenharmony_ci    auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
26723b3eb3cSopenharmony_ci    return millisecs.count();
26823b3eb3cSopenharmony_ci}
26923b3eb3cSopenharmony_ci
27023b3eb3cSopenharmony_ciinline uint64_t GetNanoseconds()
27123b3eb3cSopenharmony_ci{
27223b3eb3cSopenharmony_ci    auto now = std::chrono::system_clock::now();
27323b3eb3cSopenharmony_ci    auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
27423b3eb3cSopenharmony_ci    return nanoseconds.count();
27523b3eb3cSopenharmony_ci}
27623b3eb3cSopenharmony_ci
27723b3eb3cSopenharmony_ciinline float CalculateFriction(float gamma)
27823b3eb3cSopenharmony_ci{
27923b3eb3cSopenharmony_ci    constexpr float SCROLL_RATIO = 0.72f;
28023b3eb3cSopenharmony_ci    if (GreatOrEqual(gamma, 1.0)) {
28123b3eb3cSopenharmony_ci        gamma = 1.0;
28223b3eb3cSopenharmony_ci    }
28323b3eb3cSopenharmony_ci    return SCROLL_RATIO * static_cast<float>(std::pow(1.0 - gamma, 2));
28423b3eb3cSopenharmony_ci}
28523b3eb3cSopenharmony_ci
28623b3eb3cSopenharmony_ciinline bool IsDarkColor(uint32_t color)
28723b3eb3cSopenharmony_ci{
28823b3eb3cSopenharmony_ci    constexpr int lightThresholds = 128;
28923b3eb3cSopenharmony_ci    int r = (color >> 16) & 0xFF;
29023b3eb3cSopenharmony_ci    int g = (color >> 8) & 0xFF;
29123b3eb3cSopenharmony_ci    int b = color & 0xFF;
29223b3eb3cSopenharmony_ci    int gray = (r * 299 + g * 587 + b * 114) / 1000;
29323b3eb3cSopenharmony_ci    return gray < lightThresholds;
29423b3eb3cSopenharmony_ci}
29523b3eb3cSopenharmony_ci
29623b3eb3cSopenharmony_cibool RealPath(const std::string& fileName, char* realPath);
29723b3eb3cSopenharmony_ci
29823b3eb3cSopenharmony_ci} // namespace OHOS::Ace
29923b3eb3cSopenharmony_ci
30023b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_UTILS_UTILS_H
301