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_FWD_H
17 #define META_EXT_OBJECT_FWD_H
18
19 #include <base/util/uid_util.h>
20
21 #include <meta/ext/base_object_fwd.h>
22 #include <meta/ext/metadata_helpers.h>
23 #include <meta/interface/intf_attachment.h>
24 #include <meta/interface/intf_container_query.h>
25 #include <meta/interface/intf_metadata.h>
26 #include <meta/interface/property/intf_property.h>
27 #include <meta/interface/static_object_metadata.h>
28
29 #include "object_factory.h"
30
META_BEGIN_NAMESPACE()31 META_BEGIN_NAMESPACE()
32
33 template<typename T>
34 inline T DefaultResult()
35 {
36 return T {};
37 }
38 template<>
39 inline void DefaultResult<void>()
40 {}
41 #define META_IMPL_CALL_BASE(Interface, Function) \
42 [&] { \
43 auto b = Super::template GetBaseAs<Interface>(); \
44 if (b) { \
45 return b->Function; \
46 } \
47 return META_NS::DefaultResult<decltype(b->Function)>(); \
48 }()
49
50 /**
51 * @brief A helper macro for classes deriving from BaseObjectFwd. Makes a forwarder for an interface
52 * property implemented by the super class.
53 */
54 #define META_EXT_BASE_READONLY_PROPERTY(Interface, Type, Name) \
55 META_FORWARD_READONLY_PROPERTY(Type, Name, META_IMPL_CALL_BASE(Interface, Name()))
56
57 /**
58 * @brief A helper macro for classes deriving from BaseObjectFwd. Makes a forwarder for an interface
59 * property defined by the super class.
60 */
61 #define META_EXT_BASE_PROPERTY(Interface, Type, Name) \
62 META_FORWARD_PROPERTY(Type, Name, META_IMPL_CALL_BASE(Interface, Name()))
63
64 /**
65 * @brief A helper macro for classes deriving from BaseObjectFwd. Calls a method through an interface defined
66 * by the super class.
67 */
68 #define META_EXT_CALL_BASE(Interface, Function) META_IMPL_CALL_BASE(Interface, Function)
69
70 /**
71 * @brief A helper class for implementing a class using "aggregate inheritance" which implements the full set of object
72 * interfaces.
73 */
74 class ObjectFwd : public IntroduceInterfaces<BaseObjectFwd, IMetadata, IOwner, IAttach> {
META_OBJECT_NO_CLASSINFO(ObjectFwd,IntroduceInterfaces,ClassId::Object)75 META_OBJECT_NO_CLASSINFO(ObjectFwd, IntroduceInterfaces, ClassId::Object)
76
77 protected:
78 bool AddProperty(const IProperty::Ptr& p) override
79 {
80 return META_EXT_CALL_BASE(IMetadata, AddProperty(p));
81 }
RemoveProperty(const IProperty::Ptr & p)82 bool RemoveProperty(const IProperty::Ptr& p) override
83 {
84 return META_EXT_CALL_BASE(IMetadata, RemoveProperty(p));
85 }
AddFunction(const IFunction::Ptr & p)86 bool AddFunction(const IFunction::Ptr& p) override
87 {
88 return META_EXT_CALL_BASE(IMetadata, AddFunction(p));
89 }
RemoveFunction(const IFunction::Ptr & p)90 bool RemoveFunction(const IFunction::Ptr& p) override
91 {
92 return META_EXT_CALL_BASE(IMetadata, RemoveFunction(p));
93 }
AddEvent(const IEvent::Ptr & p)94 bool AddEvent(const IEvent::Ptr& p) override
95 {
96 return META_EXT_CALL_BASE(IMetadata, AddEvent(p));
97 }
RemoveEvent(const IEvent::Ptr & p)98 bool RemoveEvent(const IEvent::Ptr& p) override
99 {
100 return META_EXT_CALL_BASE(IMetadata, RemoveEvent(p));
101 }
GetProperties()102 BASE_NS::vector<META_NS::IProperty::Ptr> GetProperties() override
103 {
104 return META_EXT_CALL_BASE(IMetadata, GetProperties());
105 }
GetProperties()106 BASE_NS::vector<META_NS::IProperty::ConstPtr> GetProperties() const override
107 {
108 return META_EXT_CALL_BASE(IMetadata, GetProperties());
109 }
GetFunctions()110 BASE_NS::vector<IFunction::Ptr> GetFunctions() override
111 {
112 return META_EXT_CALL_BASE(IMetadata, GetFunctions());
113 }
GetFunctions()114 BASE_NS::vector<IFunction::ConstPtr> GetFunctions() const override
115 {
116 return META_EXT_CALL_BASE(IMetadata, GetFunctions());
117 }
GetEvents()118 BASE_NS::vector<IEvent::Ptr> GetEvents() override
119 {
120 return META_EXT_CALL_BASE(IMetadata, GetEvents());
121 }
GetEvents()122 BASE_NS::vector<IEvent::ConstPtr> GetEvents() const override
123 {
124 return META_EXT_CALL_BASE(IMetadata, GetEvents());
125 }
GetAllMetadatas(MetadataType types)126 BASE_NS::vector<MetadataInfo> GetAllMetadatas(MetadataType types) const override
127 {
128 return META_EXT_CALL_BASE(IMetadata, GetAllMetadatas(types));
129 }
GetMetadata(MetadataType type,BASE_NS::string_view name)130 MetadataInfo GetMetadata(MetadataType type, BASE_NS::string_view name) const override
131 {
132 return META_EXT_CALL_BASE(IMetadata, GetMetadata(type, name));
133 }
134 using IMetadata::GetProperty;
GetProperty(BASE_NS::string_view name,MetadataQuery q)135 IProperty::Ptr GetProperty(BASE_NS::string_view name, MetadataQuery q) override
136 {
137 return META_EXT_CALL_BASE(IMetadata, GetProperty(name, q));
138 }
GetProperty(BASE_NS::string_view name,MetadataQuery q)139 IProperty::ConstPtr GetProperty(BASE_NS::string_view name, MetadataQuery q) const override
140 {
141 return META_EXT_CALL_BASE(IMetadata, GetProperty(name, q));
142 }
143 using IMetadata::GetFunction;
GetFunction(BASE_NS::string_view name,MetadataQuery q)144 IFunction::ConstPtr GetFunction(BASE_NS::string_view name, MetadataQuery q) const override
145 {
146 return META_EXT_CALL_BASE(IMetadata, GetFunction(name, q));
147 }
GetFunction(BASE_NS::string_view name,MetadataQuery q)148 IFunction::Ptr GetFunction(BASE_NS::string_view name, MetadataQuery q) override
149 {
150 return META_EXT_CALL_BASE(IMetadata, GetFunction(name, q));
151 }
152 using IMetadata::GetEvent;
GetEvent(BASE_NS::string_view name,MetadataQuery q)153 IEvent::ConstPtr GetEvent(BASE_NS::string_view name, MetadataQuery q) const override
154 {
155 return META_EXT_CALL_BASE(IMetadata, GetEvent(name, q));
156 }
GetEvent(BASE_NS::string_view name,MetadataQuery q)157 IEvent::Ptr GetEvent(BASE_NS::string_view name, MetadataQuery q) override
158 {
159 return META_EXT_CALL_BASE(IMetadata, GetEvent(name, q));
160 }
161
162 protected: // IAttach
Attach(const IObject::Ptr & attachment,const IObject::Ptr & dataContext)163 bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override
164 {
165 return META_EXT_CALL_BASE(IAttach, Attach(attachment, dataContext));
166 }
167
Detach(const IObject::Ptr & attachment)168 bool Detach(const IObject::Ptr& attachment) override
169 {
170 return META_EXT_CALL_BASE(IAttach, Detach(attachment));
171 }
GetAttachments(const BASE_NS::vector<TypeId> & uids,bool strict)172 BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) const override
173 {
174 return META_EXT_CALL_BASE(IAttach, GetAttachments(uids, strict));
175 }
HasAttachments()176 bool HasAttachments() const override
177 {
178 return META_EXT_CALL_BASE(IAttach, HasAttachments());
179 }
GetAttachmentContainer(bool initializeAlways)180 IContainer::Ptr GetAttachmentContainer(bool initializeAlways) const override
181 {
182 return META_EXT_CALL_BASE(IAttach, GetAttachmentContainer(initializeAlways));
183 }
184
185 protected:
186 ObjectFwd() = default;
187 ~ObjectFwd() override = default;
188 META_NO_COPY_MOVE(ObjectFwd)
189 };
190
191 META_END_NAMESPACE()
192
193 #endif
194