1 /*
2  * Copyright (c) 2024 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 OHOS_ACE_FRAMEWORK_UTILS_H
17 #define OHOS_ACE_FRAMEWORK_UTILS_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
24 #include "core/common/container.h"
25 #include "core/common/frontend.h"
26 
27 extern "C" struct ExternalString {
28     const char* value;
29     void (*free)(const void*);
30 };
31 
32 namespace OHOS::Ace::Framework {
33 
34 class CJ_EXPORT Utils {
35 public:
CheckParamsValid(int32_t index,size_t length)36     static bool CheckParamsValid(int32_t index, size_t length)
37     {
38         return size_t(index) < length;
39     }
40 
41     static ExternalString MallocCString(const std::string& origin);
42 
43     static RefPtr<Frontend> GetCurrentFrontend();
44 
45     static std::string GetFunctionKey(int32_t functionKey);
46 
47     template<typename T>
CheckRange(T min,T max,T defaultValue,T value)48     static T CheckRange(T min, T max, T defaultValue, T value)
49     {
50         if (value < min || value > max) {
51             return defaultValue;
52         }
53         return value;
54     }
55 
56     template<typename T>
CheckMin(T min,T defaultValue,T value)57     static T CheckMin(T min, T defaultValue, T value)
58     {
59         if (value < min) {
60             return defaultValue;
61         }
62         return value;
63     }
64 };
65 
66 }
67 #endif // OHOS_ACE_FRAMEWORK_UTILS_H
68