1 /*
2 * Copyright (c) 2025 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 #ifndef OHOS_ABILITY_BASE_ANI_ENUM_CONVERT_UTILS_H
16 #define OHOS_ABILITY_BASE_ANI_ENUM_CONVERT_UTILS_H
17 #include <string>
18 #include "hilog_tag_wrapper.h"
19 #include "ani.h"
20 namespace OHOS {
21 namespace AAFwk {
22
23 namespace AniEnumConvertUtil {
24 //enum convert
GetStdString(ani_env * env,ani_string str,std::string & res)25 [[maybe_unused]] static bool GetStdString(ani_env *env, ani_string str, std::string &res)
26 {
27 ani_size sz {};
28 ani_status status = ANI_ERROR;
29 if ((status = env->String_GetUTF8Size(str, &sz)) != ANI_OK) {
30 TAG_LOGE(AAFwkTag::JSNAPI, "status : %{public}d", status);
31 return false;
32 }
33 res.resize(sz + 1);
34 if ((status = env->String_GetUTF8SubString(str, 0, sz, res.data(), res.size(), &sz)) != ANI_OK) {
35 TAG_LOGE(AAFwkTag::JSNAPI, "status : %{public}d", status);
36 return false;
37 }
38 res.resize(sz);
39 return true;
40 }
41
42 template <class T>
EnumConvert_EtsToNative(ani_env * env,ani_enum_item enumItem,T & result)43 static bool EnumConvert_EtsToNative(ani_env *env, ani_enum_item enumItem, T &result)
44 {
45 ani_status status = ANI_ERROR;
46 if constexpr (std::is_enum<T>::value || std::is_integral<T>::value) {
47 ani_int intValue{};
48 status = env->EnumItem_GetValue_Int(enumItem, &intValue);
49 if (ANI_OK != status) {
50 TAG_LOGE(AAFwkTag::ETSRUNTIME, "EnumConvert_EtsToNative failed, status : %{public}d", status);
51 return false;
52 }
53 result = static_cast<T>(intValue);
54 return true;
55 } else if constexpr (std::is_same<T, std::string>::value) {
56 ani_string strValue{};
57 status = env->EnumItem_GetValue_String(enumItem, &strValue);
58 if (ANI_OK != status) {
59 TAG_LOGE(AAFwkTag::ETSRUNTIME, "EnumItem_GetValue_String failed, status : %{public}d", status);
60 return false;
61 }
62 return GetStdString(env, strValue, result);
63 } else {
64 TAG_LOGE(AAFwkTag::ETSRUNTIME, "Enum convert failed: type not supported");
65 return false;
66 }
67 }
68
69 template<class T>
EnumConvert_EtsToNative(ani_env * env,ani_object enumItem,T & result)70 static bool EnumConvert_EtsToNative(ani_env *env, ani_object enumItem, T &result)
71 {
72 return EnumConvert_EtsToNative<T>(env, static_cast<ani_enum_item>(enumItem), result);
73 }
74
75 template <class T>
EnumConvert_NativeToEts(ani_env * env,const char * enumName,const T enumValue,ani_enum_item & result)76 static bool EnumConvert_NativeToEts(ani_env *env, const char *enumName, const T enumValue, ani_enum_item &result)
77 {
78 ani_enum aniEnum{};
79 ani_status status = env->FindEnum(enumName, &aniEnum);
80 if (ANI_OK != status) {
81 TAG_LOGE(AAFwkTag::ETSRUNTIME, "Enum convert FindEnum failed: %{public}s status: %{public}d", enumName, status);
82 return false;
83 }
84 constexpr int32_t loopMaxNum = 1000;
85 for (int32_t index = 0U; index < loopMaxNum; index++) {
86 ani_enum_item enumItem{};
87 status = env->Enum_GetEnumItemByIndex(aniEnum, index, &enumItem);
88 if (ANI_OK != status) {
89 TAG_LOGE(AAFwkTag::ETSRUNTIME,
90 "Enum convert Enum_GetEnumItemByIndex failed: enumName:%{public}s index:%{public}d status:%{public}d",
91 enumName, index, status);
92 return false;
93 }
94 // compare value
95 T tmpValue{};
96 if (EnumConvert_EtsToNative<T>(env, enumItem, tmpValue) && tmpValue == enumValue) {
97 result = enumItem;
98 return true;
99 }
100 }
101 TAG_LOGE(AAFwkTag::ETSRUNTIME, "EnumConvert_NativeToEts failed enumName: %{public}s", enumName);
102 return false;
103 }
104 }
105 } // namespace AAFwk
106 } // namespace OHOS
107 #endif // OHOS_ABILITY_BASE_ANI_ENUM_CONVERT_UTILS_H
108