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 META_EXT_OBJECT_FACTORY_H 17 #define META_EXT_OBJECT_FACTORY_H 18 19 #include <meta/interface/interface_helpers.h> 20 #include <meta/interface/intf_object_factory.h> 21 #include <meta/interface/intf_object_registry.h> 22 23 #if defined(GetClassInfo) 24 #undef GetClassInfo 25 #endif 26 META_BEGIN_NAMESPACE()27META_BEGIN_NAMESPACE() 28 29 /** 30 * @brief A base class which can be used to implement custom object factories. 31 */ 32 class ObjectFactory : public IObjectFactory { 33 // Ignore refcounts as DefaultObjectFactory is always created as static object 34 // for the class for which it implements a factory. 35 void Ref() override {} 36 void Unref() override {} 37 38 public: 39 ObjectFactory(const META_NS::ClassInfo& info) : info_(info) {} 40 CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) override 41 { 42 if (uid == CORE_NS::IInterface::UID) { 43 return this; 44 } 45 if (uid == IClassInfo::UID) { 46 return this; 47 } 48 if (uid == IObjectFactory::UID) { 49 return this; 50 } 51 return nullptr; 52 } 53 const CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) const override 54 { 55 return const_cast<ObjectFactory*>(this)->GetInterface(uid); 56 } 57 const META_NS::ClassInfo& GetClassInfo() const override 58 { 59 return info_; 60 } 61 62 private: 63 META_NS::ClassInfo info_; 64 }; 65 66 template<typename FinalClass> 67 class PlainObjectFactory : public ObjectFactory { 68 public: PlainObjectFactory(const META_NS::ClassInfo & info)69 PlainObjectFactory(const META_NS::ClassInfo& info) : ObjectFactory(info) {} CreateInstance()70 IObject::Ptr CreateInstance() const override 71 { 72 return IObject::Ptr { new FinalClass }; 73 } ConstructionType()74 ClassConstructionType ConstructionType() const override 75 { 76 return ClassConstructionType::SIMPLE; 77 } 78 }; 79 80 /** 81 * @brief The DefaultObjectFactory class implements the default object factory used 82 * by ObjectFwd and such. hash 83 */ 84 template<typename FinalClass> 85 class DefaultObjectFactory final : public PlainObjectFactory<FinalClass> { 86 public: DefaultObjectFactory(const META_NS::ClassInfo & info)87 DefaultObjectFactory(const META_NS::ClassInfo& info) : PlainObjectFactory<FinalClass>(info) {} GetClassStaticMetadata()88 const StaticObjectMetadata* GetClassStaticMetadata() const override 89 { 90 return FinalClass::StaticMetadata(); 91 } GetClassInterfaces()92 const BASE_NS::vector<BASE_NS::Uid>& GetClassInterfaces() const override 93 { 94 static auto interfaces = FinalClass::GetInterfacesVector(); 95 return interfaces; 96 } ConstructionType()97 ClassConstructionType ConstructionType() const override 98 { 99 return ClassConstructionType::BUILD; 100 } 101 }; 102 103 /** 104 * Helper macro for creating ObjectTypeInfo with machinery to register a class using the default object 105 * factory and creating instances of it. 106 */ 107 #define META_DEFINE_OBJECT_TYPE_INFO(FinalClass, ClassInfo) \ 108 friend class META_NS::DefaultObjectFactory<FinalClass>; \ 109 \ 110 public: \ 111 static META_NS::IObjectFactory::Ptr GetFactory() \ 112 { \ 113 static META_NS::DefaultObjectFactory<FinalClass> factory(ClassInfo); \ 114 return META_NS::IObjectFactory::Ptr { &factory }; \ 115 } \ 116 static constexpr const META_NS::ObjectTypeInfo OBJECT_INFO { { META_NS::ObjectTypeInfo::UID }, GetFactory }; \ 117 \ 118 private: 119 120 META_END_NAMESPACE() 121 122 #endif 123