1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_MEMORY_ACE_TYPE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_MEMORY_ACE_TYPE_H 18 19 #include "base/log/log.h" 20 #include "base/memory/referenced.h" 21 #include "base/memory/type_info_base.h" 22 23 /** 24 * Example: 25 * class BaseA : public virtual AceType { 26 * DECLARE_ACE_TYPE(BaseA, AceType); 27 * }; 28 * 29 * class BaseB : public virtual AceType { 30 * DECLARE_ACE_TYPE(BaseB, AceType); 31 * }; 32 * 33 * class BaseC : public BaseA, public BaseB { 34 * DECLARE_ACE_TYPE(BaseC, BaseA, BaseB); 35 * }; 36 */ 37 // Integrate it into class declaration to support 'DynamicCast'. 38 #define DECLARE_ACE_TYPE(...) DECLARE_RELATIONSHIP_OF_CLASSES(__VA_ARGS__) 39 40 namespace OHOS::Ace { 41 42 // Inherit 'AceType' to manager pointers using 'RefPtr', 'WeakPtr' and 'AceType::DynamicCast'. 43 class AceType : public virtual TypeInfoBase, public virtual Referenced { 44 DECLARE_ACE_TYPE(AceType, TypeInfoBase); 45 46 public: 47 ~AceType() override = default; 48 49 template<class T> DynamicCast(AceType * rawPtr)50 static T* DynamicCast(AceType* rawPtr) 51 { 52 return TypeInfoHelper::DynamicCast<T>(rawPtr); 53 } 54 template<class T> DynamicCast(const AceType * rawPtr)55 static const T* DynamicCast(const AceType* rawPtr) 56 { 57 return TypeInfoHelper::DynamicCast<T>(rawPtr); 58 } 59 template<class T, class O> DynamicCast(const RefPtr<O> & ptr)60 static RefPtr<T> DynamicCast(const RefPtr<O>& ptr) 61 { 62 return Claim(DynamicCast<T>(RawPtr(ptr))); 63 } 64 template<class T, class O> DynamicCast(const WeakPtr<O> & weak)65 static WeakPtr<T> DynamicCast(const WeakPtr<O>& weak) 66 { 67 auto ptr = weak.Upgrade(); 68 return WeakClaim(DynamicCast<T>(RawPtr(ptr))); 69 } 70 71 // Get type info by instance. TypeId(const AceType * rawPtr)72 static AceType::IdType TypeId(const AceType* rawPtr) 73 { 74 return TypeInfoHelper::TypeId(rawPtr); 75 } 76 template<class T> TypeId(const RefPtr<T> & ptr)77 static AceType::IdType TypeId(const RefPtr<T>& ptr) 78 { 79 return TypeId(AceType::RawPtr(ptr)); 80 } TypeId(const AceType & instance)81 static AceType::IdType TypeId(const AceType& instance) 82 { 83 return TypeInfoHelper::TypeId(instance); 84 } TypeName(const AceType * rawPtr)85 static const char* TypeName(const AceType* rawPtr) 86 { 87 return TypeInfoHelper::TypeName(rawPtr); 88 } 89 template<class T> TypeName(const RefPtr<T> & ptr)90 static const char* TypeName(const RefPtr<T>& ptr) 91 { 92 return TypeName(AceType::RawPtr(ptr)); 93 } TypeName(const AceType & instance)94 static const char* TypeName(const AceType& instance) 95 { 96 return TypeInfoHelper::TypeName(instance); 97 } 98 99 // Get type info by type itself. 100 template<class T> TypeId()101 static AceType::IdType TypeId() 102 { 103 return TypeInfoHelper::TypeId<T>(); 104 } 105 template<class T> TypeName()106 static const char* TypeName() 107 { 108 return TypeInfoHelper::TypeName<T>(); 109 } 110 111 // Check whether instance is the specified type 112 template<class T> InstanceOf(const AceType * rawPtr)113 static bool InstanceOf(const AceType* rawPtr) 114 { 115 return TypeInfoHelper::InstanceOf<T>(rawPtr); 116 } 117 template<class T> InstanceOf(const AceType & instance)118 static bool InstanceOf(const AceType& instance) 119 { 120 return TypeInfoHelper::InstanceOf<T>(&instance); 121 } 122 template<class T, class O> InstanceOf(const RefPtr<O> & ptr)123 static bool InstanceOf(const RefPtr<O>& ptr) 124 { 125 return TypeInfoHelper::InstanceOf<T>(RawPtr(ptr)); 126 } 127 128 protected: 129 AceType() = default; 130 }; 131 132 } // namespace OHOS::Ace 133 134 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_MEMORY_ACE_TYPE_H 135