• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         return WeakClaim(DynamicCast<T>(RawPtr(weak.Upgrade())));
68     }
69 
70     // Get type info by instance.
TypeId(const AceType * rawPtr)71     static AceType::IdType TypeId(const AceType* rawPtr)
72     {
73         return TypeInfoHelper::TypeId(rawPtr);
74     }
75     template<class T>
TypeId(const RefPtr<T> & ptr)76     static AceType::IdType TypeId(const RefPtr<T>& ptr)
77     {
78         return TypeId(AceType::RawPtr(ptr));
79     }
TypeId(const AceType & instance)80     static AceType::IdType TypeId(const AceType& instance)
81     {
82         return TypeInfoHelper::TypeId(instance);
83     }
TypeName(const AceType * rawPtr)84     static const char* TypeName(const AceType* rawPtr)
85     {
86         return TypeInfoHelper::TypeName(rawPtr);
87     }
88     template<class T>
TypeName(const RefPtr<T> & ptr)89     static const char* TypeName(const RefPtr<T>& ptr)
90     {
91         return TypeName(AceType::RawPtr(ptr));
92     }
TypeName(const AceType & instance)93     static const char* TypeName(const AceType& instance)
94     {
95         return TypeInfoHelper::TypeName(instance);
96     }
97 
98     // Get type info by type itself.
99     template<class T>
TypeId()100     static AceType::IdType TypeId()
101     {
102         return TypeInfoHelper::TypeId<T>();
103     }
104     template<class T>
TypeName()105     static const char* TypeName()
106     {
107         return TypeInfoHelper::TypeName<T>();
108     }
109 
110     // Check whether instance is the specified type
111     template<class T>
InstanceOf(const AceType * rawPtr)112     static bool InstanceOf(const AceType* rawPtr)
113     {
114         return TypeInfoHelper::InstanceOf<T>(rawPtr);
115     }
116     template<class T>
InstanceOf(const AceType & instance)117     static bool InstanceOf(const AceType& instance)
118     {
119         return TypeInfoHelper::InstanceOf<T>(&instance);
120     }
121     template<class T, class O>
InstanceOf(const RefPtr<O> & ptr)122     static bool InstanceOf(const RefPtr<O>& ptr)
123     {
124         return TypeInfoHelper::InstanceOf<T>(RawPtr(ptr));
125     }
126 
127 protected:
128     AceType() = default;
129 };
130 
131 } // namespace OHOS::Ace
132 
133 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_MEMORY_ACE_TYPE_H
134