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 * @brief A helper macro for classes deriving from BaseObjectFwd. Makes a forwarder for an interface
51 * property implemented by the super class.
52 */
53 #define META_EXT_BASE_READONLY_PROPERTY(Interface, Type, Name) \
54 META_FORWARD_READONLY_PROPERTY(Type, Name, META_IMPL_CALL_BASE(Interface, Name()))
55
56 /**
57 * @brief A helper macro for classes deriving from BaseObjectFwd. Makes a forwarder for an interface
58 * property defined by the super class.
59 */
60 #define META_EXT_BASE_PROPERTY(Interface, Type, Name) \
61 META_FORWARD_PROPERTY(Type, Name, META_IMPL_CALL_BASE(Interface, Name()))
62
63 /**
64 * @brief A helper macro for classes deriving from BaseObjectFwd. Calls a method through an interface defined
65 * by the super class.
66 */
67 #define META_EXT_CALL_BASE(Interface, Function) META_IMPL_CALL_BASE(Interface, Function)
68
69 /**
70 * @brief A helper class for implementing a class which implements the full set of object interfaces.
71 */
72 class ObjectFwd : public IntroduceInterfaces<BaseObjectFwd, IMetadata, IOwner, IAttach> {
META_OBJECT_NO_CLASSINFO(ObjectFwd,IntroduceInterfaces,ClassId::Object)73 META_OBJECT_NO_CLASSINFO(ObjectFwd, IntroduceInterfaces, ClassId::Object)
74
75 protected:
76 bool AddProperty(const IProperty::Ptr& p) override
77 {
78 return META_EXT_CALL_BASE(IMetadata, AddProperty(p));
79 }
RemoveProperty(const IProperty::Ptr & p)80 bool RemoveProperty(const IProperty::Ptr& p) override
81 {
82 return META_EXT_CALL_BASE(IMetadata, RemoveProperty(p));
83 }
AddFunction(const IFunction::Ptr & p)84 bool AddFunction(const IFunction::Ptr& p) override
85 {
86 return META_EXT_CALL_BASE(IMetadata, AddFunction(p));
87 }
RemoveFunction(const IFunction::Ptr & p)88 bool RemoveFunction(const IFunction::Ptr& p) override
89 {
90 return META_EXT_CALL_BASE(IMetadata, RemoveFunction(p));
91 }
AddEvent(const IEvent::Ptr & p)92 bool AddEvent(const IEvent::Ptr& p) override
93 {
94 return META_EXT_CALL_BASE(IMetadata, AddEvent(p));
95 }
RemoveEvent(const IEvent::Ptr & p)96 bool RemoveEvent(const IEvent::Ptr& p) override
97 {
98 return META_EXT_CALL_BASE(IMetadata, RemoveEvent(p));
99 }
GetProperties()100 BASE_NS::vector<META_NS::IProperty::Ptr> GetProperties() override
101 {
102 return META_EXT_CALL_BASE(IMetadata, GetProperties());
103 }
GetProperties()104 BASE_NS::vector<META_NS::IProperty::ConstPtr> GetProperties() const override
105 {
106 return META_EXT_CALL_BASE(IMetadata, GetProperties());
107 }
GetFunctions()108 BASE_NS::vector<IFunction::Ptr> GetFunctions() override
109 {
110 return META_EXT_CALL_BASE(IMetadata, GetFunctions());
111 }
GetFunctions()112 BASE_NS::vector<IFunction::ConstPtr> GetFunctions() const override
113 {
114 return META_EXT_CALL_BASE(IMetadata, GetFunctions());
115 }
GetEvents()116 BASE_NS::vector<IEvent::Ptr> GetEvents() override
117 {
118 return META_EXT_CALL_BASE(IMetadata, GetEvents());
119 }
GetEvents()120 BASE_NS::vector<IEvent::ConstPtr> GetEvents() const override
121 {
122 return META_EXT_CALL_BASE(IMetadata, GetEvents());
123 }
GetAllMetadatas(MetadataType types)124 BASE_NS::vector<MetadataInfo> GetAllMetadatas(MetadataType types) const override
125 {
126 return META_EXT_CALL_BASE(IMetadata, GetAllMetadatas(types));
127 }
128 using IMetadata::GetProperty;
GetProperty(BASE_NS::string_view name,MetadataQuery q)129 IProperty::Ptr GetProperty(BASE_NS::string_view name, MetadataQuery q) override
130 {
131 return META_EXT_CALL_BASE(IMetadata, GetProperty(name, q));
132 }
GetProperty(BASE_NS::string_view name,MetadataQuery q)133 IProperty::ConstPtr GetProperty(BASE_NS::string_view name, MetadataQuery q) const override
134 {
135 return META_EXT_CALL_BASE(IMetadata, GetProperty(name, q));
136 }
137 using IMetadata::GetFunction;
GetFunction(BASE_NS::string_view name,MetadataQuery q)138 IFunction::ConstPtr GetFunction(BASE_NS::string_view name, MetadataQuery q) const override
139 {
140 return META_EXT_CALL_BASE(IMetadata, GetFunction(name, q));
141 }
GetFunction(BASE_NS::string_view name,MetadataQuery q)142 IFunction::Ptr GetFunction(BASE_NS::string_view name, MetadataQuery q) override
143 {
144 return META_EXT_CALL_BASE(IMetadata, GetFunction(name, q));
145 }
146 using IMetadata::GetEvent;
GetEvent(BASE_NS::string_view name,MetadataQuery q)147 IEvent::ConstPtr GetEvent(BASE_NS::string_view name, MetadataQuery q) const override
148 {
149 return META_EXT_CALL_BASE(IMetadata, GetEvent(name, q));
150 }
GetEvent(BASE_NS::string_view name,MetadataQuery q)151 IEvent::Ptr GetEvent(BASE_NS::string_view name, MetadataQuery q) override
152 {
153 return META_EXT_CALL_BASE(IMetadata, GetEvent(name, q));
154 }
155
156 protected: // IAttach
Attach(const IObject::Ptr & attachment,const IObject::Ptr & dataContext)157 bool Attach(const IObject::Ptr& attachment, const IObject::Ptr& dataContext) override
158 {
159 return META_EXT_CALL_BASE(IAttach, Attach(attachment, dataContext));
160 }
161
Detach(const IObject::Ptr & attachment)162 bool Detach(const IObject::Ptr& attachment) override
163 {
164 return META_EXT_CALL_BASE(IAttach, Detach(attachment));
165 }
GetAttachments(const BASE_NS::vector<TypeId> & uids,bool strict)166 BASE_NS::vector<IObject::Ptr> GetAttachments(const BASE_NS::vector<TypeId>& uids, bool strict) const override
167 {
168 return META_EXT_CALL_BASE(IAttach, GetAttachments(uids, strict));
169 }
HasAttachments()170 bool HasAttachments() const override
171 {
172 return META_EXT_CALL_BASE(IAttach, HasAttachments());
173 }
GetAttachmentContainer(bool initializeAlways)174 IContainer::Ptr GetAttachmentContainer(bool initializeAlways) const override
175 {
176 return META_EXT_CALL_BASE(IAttach, GetAttachmentContainer(initializeAlways));
177 }
178
179 protected:
180 ObjectFwd() = default;
181 ~ObjectFwd() override = default;
182 META_NO_COPY_MOVE(ObjectFwd)
183 };
184
185 META_END_NAMESPACE()
186
187 #endif
188