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