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