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