1 /* 2 * Copyright (c) 2024 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_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_ACE_TYPE_H 17 #define FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_ACE_TYPE_H 18 19 #include "ui/base/referenced.h" 20 21 #include "ui/base/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 friend class Referenced; 40 41 namespace OHOS::Ace { 42 43 enum class ColorMode : int32_t { 44 LIGHT = 0, 45 DARK, 46 COLOR_MODE_UNDEFINED, 47 }; 48 49 // Inherit 'AceType' to manager pointers using 'RefPtr', 'WeakPtr' and 'AceType::DynamicCast'. 50 class AceType : public virtual TypeInfoBase, public virtual Referenced { 51 DECLARE_ACE_TYPE(AceType, TypeInfoBase); 52 53 public: 54 ~AceType() override = default; 55 56 template<class T> DynamicCast(AceType * rawPtr)57 static T* DynamicCast(AceType* rawPtr) 58 { 59 return TypeInfoHelper::DynamicCast<T>(rawPtr); 60 } 61 template<class T> DynamicCast(const AceType * rawPtr)62 static const T* DynamicCast(const AceType* rawPtr) 63 { 64 return TypeInfoHelper::DynamicCast<T>(rawPtr); 65 } 66 template<class T, class O> DynamicCast(const RefPtr<O> & ptr)67 static RefPtr<T> DynamicCast(const RefPtr<O>& ptr) 68 { 69 return Claim(DynamicCast<T>(RawPtr(ptr))); 70 } 71 template<class T, class O> DynamicCast(const WeakPtr<O> & weak)72 static WeakPtr<T> DynamicCast(const WeakPtr<O>& weak) 73 { 74 auto ptr = weak.Upgrade(); 75 return WeakClaim(DynamicCast<T>(RawPtr(ptr))); 76 } 77 78 // Get type info by instance. TypeId(const AceType * rawPtr)79 static AceType::IdType TypeId(const AceType* rawPtr) 80 { 81 return TypeInfoHelper::TypeId(rawPtr); 82 } 83 template<class T> TypeId(const RefPtr<T> & ptr)84 static AceType::IdType TypeId(const RefPtr<T>& ptr) 85 { 86 return TypeId(AceType::RawPtr(ptr)); 87 } TypeId(const AceType & instance)88 static AceType::IdType TypeId(const AceType& instance) 89 { 90 return TypeInfoHelper::TypeId(instance); 91 } TypeName(const AceType * rawPtr)92 static const char* TypeName(const AceType* rawPtr) 93 { 94 return TypeInfoHelper::TypeName(rawPtr); 95 } 96 template<class T> TypeName(const RefPtr<T> & ptr)97 static const char* TypeName(const RefPtr<T>& ptr) 98 { 99 return TypeName(AceType::RawPtr(ptr)); 100 } TypeName(const AceType & instance)101 static const char* TypeName(const AceType& instance) 102 { 103 return TypeInfoHelper::TypeName(instance); 104 } 105 106 // Get type info by type itself. 107 template<class T> TypeId()108 static AceType::IdType TypeId() 109 { 110 return TypeInfoHelper::TypeId<T>(); 111 } 112 template<class T> TypeName()113 static const char* TypeName() 114 { 115 return TypeInfoHelper::TypeName<T>(); 116 } 117 118 // Check whether instance is the specified type 119 template<class T> InstanceOf(const AceType * rawPtr)120 static bool InstanceOf(const AceType* rawPtr) 121 { 122 return TypeInfoHelper::InstanceOf<T>(rawPtr); 123 } 124 template<class T> InstanceOf(const AceType & instance)125 static bool InstanceOf(const AceType& instance) 126 { 127 return TypeInfoHelper::InstanceOf<T>(&instance); 128 } 129 template<class T, class O> InstanceOf(const RefPtr<O> & ptr)130 static bool InstanceOf(const RefPtr<O>& ptr) 131 { 132 return TypeInfoHelper::InstanceOf<T>(RawPtr(ptr)); 133 } 134 135 protected: 136 AceType() = default; 137 }; 138 139 } // namespace OHOS::Ace 140 141 #endif // FOUNDATION_ACE_INTERFACES_INNER_API_ACE_KIT_INCLUDE_BASE_ACE_TYPE_H 142