1 /*
2 * Copyright (c) 2023-2023 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 #include "common/log.h"
17 #include "meta/any.h"
18 #include <map>
19
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "Any" };
22 }
23
24 namespace {
25 using namespace OHOS::Media;
26 using BaseTypesMap = std::map<std::string, AnyValueType>;
27
GetBaseTypesMap()28 const BaseTypesMap &GetBaseTypesMap()
29 {
30 static const BaseTypesMap baseTypeMap([]() {
31 BaseTypesMap typeMap;
32 Any defaultBool = (bool)true;
33 typeMap[std::string(defaultBool.TypeName())] = AnyValueType::BOOL;
34 Any defaultInt32 = (int32_t)0;
35 typeMap[std::string(defaultInt32.TypeName())] = AnyValueType::INT32_T;
36 Any defaultInt64 = (int64_t)0;
37 typeMap[std::string(defaultInt64.TypeName())] = AnyValueType::INT64_T;
38 Any defaultFoalt = (float)0.0;
39 typeMap[std::string(defaultFoalt.TypeName())] = AnyValueType::FLOAT;
40 Any defaultDouble = (double)0.0;
41 typeMap[std::string(defaultDouble.TypeName())] = AnyValueType::DOUBLE;
42 Any defaultString = std::string();
43 typeMap[std::string(defaultString.TypeName())] = AnyValueType::STRING;
44 Any defaultVecUint8 = std::vector<uint8_t>();
45 typeMap[std::string(defaultVecUint8.TypeName())] = AnyValueType::VECTOR_UINT8;
46 Any defaultVecInt32 = std::vector<int32_t>();
47 typeMap[std::string(defaultVecInt32.TypeName())] = AnyValueType::VECTOR_INT32;
48 return typeMap;
49 }());
50 return baseTypeMap;
51 }
52 } // namespace
53
54 namespace OHOS {
55 namespace Media {
BaseTypesToParcel(const Any * operand,MessageParcel & parcel)56 bool Any::BaseTypesToParcel(const Any *operand, MessageParcel &parcel) noexcept
57 {
58 auto iter = GetBaseTypesMap().find(std::string(operand->TypeName()));
59 if (iter == GetBaseTypesMap().end()) {
60 parcel.WriteInt32(static_cast<int32_t>(AnyValueType::INVALID_TYPE));
61 return false;
62 }
63 bool ret = parcel.WriteInt32(static_cast<int32_t>(iter->second));
64 switch (iter->second) {
65 case AnyValueType::BOOL:
66 ret = ret && parcel.WriteBool(*AnyCast<bool>(operand));
67 break;
68 case AnyValueType::INT32_T:
69 ret = ret && parcel.WriteInt32(*AnyCast<int32_t>(operand));
70 break;
71 case AnyValueType::INT64_T:
72 ret = ret && parcel.WriteInt64(*AnyCast<int64_t>(operand));
73 break;
74 case AnyValueType::FLOAT:
75 ret = ret && parcel.WriteFloat(*AnyCast<float>(operand));
76 break;
77 case AnyValueType::DOUBLE:
78 ret = ret && parcel.WriteDouble(*AnyCast<double>(operand));
79 break;
80 case AnyValueType::STRING:
81 ret = ret && parcel.WriteString(*AnyCast<std::string>(operand));
82 break;
83 case AnyValueType::VECTOR_UINT8:
84 ret = ret && parcel.WriteUInt8Vector(*AnyCast<std::vector<uint8_t>>(operand));
85 break;
86 case AnyValueType::VECTOR_INT32:
87 ret = ret && parcel.WriteInt32Vector(*AnyCast<std::vector<int32_t>>(operand));
88 break;
89 default: {
90 parcel.WriteInt32(static_cast<int32_t>(AnyValueType::INVALID_TYPE));
91 return false;
92 }
93 }
94 return ret;
95 }
96
97 enum class StatusCodeFromParcel {
98 SUCCESS = 0,
99 ENUM_RETRY = 1,
100 NO_RETRY = 2,
101 };
102
BaseTypesVectorUint8(Any * operand,MessageParcel & parcel)103 static void BaseTypesVectorUint8(Any *operand, MessageParcel &parcel)
104 {
105 std::vector<uint8_t> val;
106 (void)parcel.ReadUInt8Vector(&val);
107 Any tmp(val);
108 operand->Swap(tmp);
109 }
110
BaseTypesVectorInt32(Any * operand,MessageParcel & parcel)111 static void BaseTypesVectorInt32(Any *operand, MessageParcel &parcel)
112 {
113 std::vector<int32_t> val;
114 (void)parcel.ReadInt32Vector(&val);
115 Any tmp(val);
116 operand->Swap(tmp);
117 }
118
119 // returnValue : 0 -- success; 1 -- retry for enum type; 2 -- failed no retry
BaseTypesFromParcel(Any * operand,MessageParcel & parcel)120 int Any::BaseTypesFromParcel(Any *operand, MessageParcel &parcel) noexcept
121 {
122 AnyValueType type = static_cast<AnyValueType>(parcel.ReadInt32());
123 switch (type) {
124 case AnyValueType::BOOL: {
125 Any tmp(parcel.ReadBool());
126 operand->Swap(tmp);
127 break;
128 }
129 case AnyValueType::INT32_T: {
130 Any tmp(parcel.ReadInt32());
131 operand->Swap(tmp);
132 break;
133 }
134 case AnyValueType::INT64_T: {
135 Any tmp(parcel.ReadInt64());
136 operand->Swap(tmp);
137 break;
138 }
139 case AnyValueType::FLOAT: {
140 Any tmp(parcel.ReadFloat());
141 operand->Swap(tmp);
142 break;
143 }
144 case AnyValueType::DOUBLE: {
145 Any tmp(parcel.ReadDouble());
146 operand->Swap(tmp);
147 break;
148 }
149 case AnyValueType::STRING: {
150 Any tmp(parcel.ReadString());
151 operand->Swap(tmp);
152 break;
153 }
154 case AnyValueType::VECTOR_UINT8: {
155 BaseTypesVectorUint8(operand, parcel);
156 break;
157 }
158 case AnyValueType::VECTOR_INT32: {
159 BaseTypesVectorInt32(operand, parcel);
160 break;
161 }
162 case AnyValueType::INVALID_TYPE:
163 return static_cast<int>(StatusCodeFromParcel::ENUM_RETRY);
164 default:
165 return static_cast<int>(StatusCodeFromParcel::NO_RETRY);
166 }
167 return static_cast<int>(StatusCodeFromParcel::SUCCESS);
168 }
169
170 /**
171 * Get TypeName From function info.
172 * Extract the Type name out of Function Info
173 * @param functionInfo Function Info
174 * @return Name of Type T ,Such as <b>bool int float double std::vector<unsigned char></b> etc.
175 * @example In windows with MEDIA_NO_OHOS define,
176 * FunctionInfo will be like <br>
177 * static constexpr std::string_view OHOS::Media::Any::GetTypeName()
178 * [with T = <b>bool</b>; std::string_view = std::basic_string_view<char>] <br>
179 * with MEDIA_OHOS define, FunctionInfo will be like <br>
180 * static std::string_view OHOS::Media::Any::GetTypeName() [T = <b>std::vector<unsigned char></b>] <br>
181 * For EnumType , FunctionInfo will be like <br>
182 * static std::string_view OHOS::Media::Any::GetTypeName() [T = <b>OHOS::Media::Plugins::VideoEncodeBitrateMode</b>]
183 */
GetTypeNameFromFunctionInfo(const char * functionInfo)184 std::string_view Any::GetTypeNameFromFunctionInfo(const char* functionInfo) noexcept
185 {
186 std::string_view stringInfo = functionInfo;
187 std::string_view retType = "Unknown";
188 size_t beginIndex = stringInfo.find_first_of('=');
189 if (beginIndex == std::string::npos) {
190 MEDIA_LOG_E("GetTypeNameFromFunctionInfo failed. Function: " PUBLIC_LOG_S, stringInfo.data());
191 return retType;
192 } else {
193 beginIndex += 2; // 2 表示右移两位
194 }
195 #ifdef MEDIA_OHOS
196 size_t endIndex = stringInfo.find_last_of(']');
197 #else
198 size_t endIndex = stringInfo.find_last_of(';');
199 #endif
200 FALSE_RETURN_V_MSG_E(endIndex != std::string::npos,
201 retType, "GetTypeNameFromFunctionInfo find Type failed. Function: " PUBLIC_LOG_S, stringInfo.data());
202 std::string_view typeNameRet(functionInfo + beginIndex, endIndex - beginIndex);
203 return typeNameRet;
204 }
205 } // namespace Media
206 } // namespace OHOS