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 #ifndef META_SRC_BASE_OBJECT_H 16 #define META_SRC_BASE_OBJECT_H 17 18 #include <base/containers/array_view.h> 19 #include <base/containers/vector.h> 20 21 #include <meta/base/types.h> 22 #include <meta/ext/implementation_macros.h> 23 #include <meta/ext/metadata_helpers.h> 24 #include <meta/ext/object_factory.h> 25 #include <meta/interface/builtin_objects.h> 26 #include <meta/interface/interface_helpers.h> 27 #include <meta/interface/intf_derived.h> 28 #include <meta/interface/intf_lifecycle.h> 29 #include <meta/interface/intf_object.h> 30 #include <meta/interface/intf_object_flags.h> 31 #include <meta/interface/intf_object_registry.h> 32 #include <meta/interface/object_type_info.h> 33 34 META_BEGIN_NAMESPACE() 35 36 namespace Internal { 37 38 class BaseObject : public IntroduceInterfaces<IObjectInstance, IObjectFlags, IDerived, ILifecycle> { 39 protected: 40 BaseObject() = default; 41 ~BaseObject() override = default; 42 43 // IObject 44 // BASE_NS::Uid GetClassId() const override; //Must be implemented by derived class 45 // BASE_NS::string_view GetClassName() const override; //Must be implemented by derived class 46 InstanceId GetInstanceId() const override; 47 BASE_NS::string GetName() const override; 48 IObject::Ptr Resolve(const RefUri& uri) const override; 49 IObject::Ptr GetSelf() const override; 50 BASE_NS::vector<BASE_NS::Uid> GetInterfaces() const override; 51 // IObjectFlags 52 ObjectFlagBitsValue GetObjectFlags() const override; 53 void SetObjectFlags(const ObjectFlagBitsValue& value) override; 54 ObjectFlagBitsValue GetObjectDefaultFlags() const override; 55 56 template<typename Interface> GetSelf() const57 typename Interface::Ptr GetSelf() const 58 { 59 return interface_pointer_cast<Interface>(GetSelf()); 60 } 61 62 // IDerived 63 void SetSuperInstance(const IObject::Ptr&, const IObject::Ptr&) override; 64 BASE_NS::Uid GetSuperClassUid() const override; 65 66 // ILifecycle 67 bool Build(const IMetadata::Ptr& data) override; 68 void SetInstanceId(InstanceId uid) override; 69 void Destroy() override; 70 71 protected: 72 IObjectRegistry& GetObjectRegistry() const; 73 74 protected: StaticObjectMeta()75 static StaticObjectMetadata& StaticObjectMeta() 76 { 77 static StaticObjectMetadata meta { META_NS::ClassId::BaseObject, nullptr }; 78 return meta; 79 } 80 81 public: GetStaticObjectMetadata()82 static const META_NS::StaticObjectMetadata& GetStaticObjectMetadata() 83 { 84 return StaticObjectMeta(); 85 } 86 87 private: 88 InstanceId instanceId_; 89 IObjectInstance::WeakPtr me_; 90 ObjectFlagBitsValue flags_ { ObjectFlagBits::DEFAULT_FLAGS }; 91 }; 92 93 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class ConcreteBaseClass, class... Interfaces> 94 class ConcreteBaseFwd : public IntroduceInterfaces<Interfaces...>, public ConcreteBaseClass { 95 STATIC_METADATA_MACHINERY(ClassInfo, ConcreteBaseClass) 96 STATIC_INTERFACES_WITH_CONCRETE_BASE(IntroduceInterfaces<Interfaces...>, ConcreteBaseClass) 97 META_DEFINE_OBJECT_TYPE_INFO(FinalClass, ClassInfo) 98 public: 99 using ConcreteBaseClass::GetInterfacesVector; 100 101 protected: 102 ObjectId GetClassId() const override 103 { 104 return ClassInfo.Id(); 105 } 106 BASE_NS::string_view GetClassName() const override 107 { 108 return ClassInfo.Name(); 109 } 110 BASE_NS::vector<BASE_NS::Uid> GetInterfaces() const override 111 { 112 return GetStaticInterfaces(); 113 } 114 115 public: 116 const CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) const override 117 { 118 auto* me = const_cast<ConcreteBaseFwd*>(this); 119 return me->ConcreteBaseFwd::GetInterface(uid); 120 } 121 CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) override 122 { 123 CORE_NS::IInterface* ret = ConcreteBaseClass::GetInterface(uid); 124 if (!ret) { 125 ret = IntroduceInterfaces<Interfaces...>::GetInterface(uid); 126 } 127 return ret; 128 } 129 130 protected: 131 void Ref() override 132 { 133 ConcreteBaseClass::Ref(); 134 } 135 void Unref() override 136 { 137 ConcreteBaseClass::Unref(); 138 } 139 }; 140 141 template<class FinalClass, const META_NS::ClassInfo& ClassInfo, class... Interfaces> 142 class BaseObjectFwd : public ConcreteBaseFwd<FinalClass, ClassInfo, META_NS::Internal::BaseObject, Interfaces...> { 143 using Impl = META_NS::Internal::BaseObject; 144 145 public: GetObjectRegistry() const146 virtual IObjectRegistry& GetObjectRegistry() const 147 { 148 return BaseObject::GetObjectRegistry(); 149 } 150 151 protected: 152 BaseObjectFwd() = default; 153 ~BaseObjectFwd() override = default; 154 }; 155 156 } // namespace Internal 157 158 class BaseObject : public Internal::BaseObjectFwd<BaseObject, META_NS::ClassId::BaseObject> { 159 using Super = Internal::BaseObjectFwd<BaseObject, META_NS::ClassId::BaseObject>; 160 161 public: 162 using Super::Super; 163 }; 164 165 META_END_NAMESPACE() 166 167 #endif 168