1 /*
2 * Copyright (c) 2022-2022 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 HISTREAMER_PLUGIN_TYPE_INFO_EXT_H
16 #define HISTREAMER_PLUGIN_TYPE_INFO_EXT_H
17
18 #include <cstring>
19 #include <string_view>
20 #include <memory>
21 #include <typeinfo>
22 #include <string>
23
24 namespace OHOS {
25 namespace Media {
26 namespace Plugin {
27 #ifndef HST_ANY_WITH_NO_RTTI
28 /**
29 * This function is used to compare two type_info. Besides the basic compare using operator == of type_info,
30 * we also consider types with the same names are the same types.
31 * @param t1 type
32 * @param t2 type
33 * @return true if t1 and t2 are the same type. otherwise, false.
34 */
IsSameType(const std::type_info & t1,const std::type_info & t2)35 inline bool IsSameType(const std::type_info& t1, const std::type_info& t2) noexcept
36 {
37 if (t1 == t2) {
38 return true;
39 }
40 auto t1Length = strlen(t1.name());
41 if (t1Length == strlen(t2.name()) && strncmp(t1.name(), t2.name(), t1Length) == 0) {
42 return true;
43 }
44 return false;
45 }
46 #else
47 inline bool IsSameType(std::string_view t1, std::string_view t2) noexcept
48 {
49 return std::string(t1) == std::string(t2);
50 }
51 #endif
52
53 /**
54 * This function is used to reinterpret cast one shared_ptr into another shared_ptr.
55 * @tparam T type
56 * @tparam U type
57 * @param ptr pointer
58 * @return cast result
59 */
60 template<typename T, typename U>
ReinterpretPointerCast(const std::shared_ptr<U> & ptr)61 inline std::shared_ptr<T> ReinterpretPointerCast(const std::shared_ptr<U>& ptr) noexcept
62 {
63 return std::shared_ptr<T>(ptr, reinterpret_cast<T*>(ptr.get()));
64 }
65 } // Plugin
66 } // Media
67 } // OHOS
68
69 #endif // HISTREAMER_PLUGIN_TYPE_INFO_EXT_H
70