1 /* 2 * Copyright (c) 2021-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 OHOS_ABILITY_BASE_BASE_DEF_H 16 #define OHOS_ABILITY_BASE_BASE_DEF_H 17 18 #include "base_types.h" 19 #include "errors.h" 20 21 namespace OHOS { 22 namespace AAFwk { 23 #define INTERFACE_INNER struct 24 25 #define REFCOUNT_ADD(i) \ 26 if (i) { \ 27 (i)->IncStrongRef(); \ 28 } 29 30 #define REFCOUNT_RELEASE(i) \ 31 if (i) { \ 32 (i)->DecStrongRef(); \ 33 } 34 35 #define VALIDATE_NOT_NULL(i) \ 36 if ((i) == nullptr) { \ 37 return ERR_INVALID_VALUE; \ 38 } 39 40 #define INTERFACEID(id) extern const InterfaceID g_IID_##id; 41 42 #define INTERFACE(id, x) \ 43 extern const InterfaceID g_IID_##id; \ 44 INTERFACE_INNER id : public IInterface 45 46 #define INTERFACE2(id, pid, x) \ 47 do { \ 48 extern const InterfaceID g_IID_##id; \ 49 INTERFACE_INNER id : public##pid \ 50 } while (0); 51 52 #define CLASS(id, x) \ 53 do { \ 54 extern const ClassID CID_##id; \ 55 class id : public Object \ 56 } while (0); 57 58 #define INTERFACE_ID(x) 59 #define CLASS_ID(x) 60 61 #ifndef IINTERFACE_DECL 62 #define IINTERFACE_DECL() \ 63 void IncStrongRef(const void *id = nullptr) override; \ 64 \ 65 void DecStrongRef(const void *id = nullptr) override; \ 66 \ 67 IInterface *Query(const InterfaceID & iid) override; \ 68 \ 69 InterfaceID GetInterfaceID(IInterface *object) override; 70 71 #endif 72 73 #ifndef IINTERFACE_IMPL_1 74 #define IINTERFACE_IMPL_1(ClassName, SuperclassName, InterfaceName) \ 75 void ClassName::IncStrongRef(const void *id) \ 76 { \ 77 Object::IncStrongRef(id); \ 78 } \ 79 \ 80 void ClassName::DecStrongRef(const void *id) \ 81 { \ 82 Object::DecStrongRef(id); \ 83 } \ 84 \ 85 IInterface *ClassName::Query(const InterfaceID & iid) \ 86 { \ 87 if (iid == g_IID_##InterfaceName) { \ 88 return static_cast<InterfaceName *>(this); \ 89 } \ 90 if (iid == g_IID_IInterface) { \ 91 return static_cast<InterfaceName *>(this); \ 92 } \ 93 return SuperclassName::Query(iid); \ 94 } \ 95 \ 96 InterfaceID ClassName::GetInterfaceID(IInterface *object) \ 97 { \ 98 if (object == static_cast<InterfaceName *>(this)) { \ 99 return g_IID_##InterfaceName; \ 100 } \ 101 return SuperclassName::GetInterfaceID(object); \ 102 } 103 #endif 104 105 #ifndef IINTERFACE_IMPL_2 106 #define IINTERFACE_IMPL_2(ClassName, SuperclassName, InterfaceName1, InterfaceName2) \ 107 void ClassName::IncStrongRef(const void *id) \ 108 { \ 109 Object::IncStrongRef(id); \ 110 } \ 111 \ 112 void ClassName::DecStrongRef(const void *id) \ 113 { \ 114 Object::DecStrongRef(id); \ 115 } \ 116 IInterface *ClassName::Query(const InterfaceID & iid) \ 117 { \ 118 if (iid == g_IID_##InterfaceName1) { \ 119 return static_cast<InterfaceName1 *>(this); \ 120 } \ 121 if (iid == g_IID_##InterfaceName2) { \ 122 return static_cast<InterfaceName2 *>(this); \ 123 } \ 124 if (iid == g_IID_IInterface) { \ 125 return static_cast<InterfaceName1 *>(this); \ 126 } \ 127 return SuperclassName::Query(iid); \ 128 } \ 129 InterfaceID ClassName::GetInterfaceID(IInterface *object) \ 130 { \ 131 if (object == static_cast<InterfaceName1 *>(this)) { \ 132 return g_IID_##InterfaceName1; \ 133 } \ 134 if (object == static_cast<InterfaceName2 *>(this)) { \ 135 return g_IID_##InterfaceName2; \ 136 } \ 137 return SuperclassName::GetInterfaceID(object); \ 138 } 139 #endif 140 141 #ifndef OBJECT_DECL 142 #define OBJECT_DECL() ClassID GetClassID() override; 143 #endif 144 145 #ifndef OBJECT_IMPL 146 #define OBJECT_IMPL(ClassName) \ 147 do { \ 148 ClassID(ClassName)::GetClassID() \ 149 { \ 150 return CID_##ClassName; \ 151 } \ 152 } while (0); 153 #endif 154 } // namespace AAFwk 155 } // namespace OHOS 156 #endif // OHOS_ABILITY_BASE_BASE_DEF_H 157