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