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_API_OBJECT_H
17 #define META_API_OBJECT_H
18
19 #include <meta/api/interface_object.h>
20 #include <meta/api/make_callback.h>
21 #include <meta/interface/builtin_objects.h>
22 #include <meta/interface/intf_attach.h>
23 #include <meta/interface/intf_container.h>
24 #include <meta/interface/intf_content.h>
25 #include <meta/interface/intf_metadata.h>
26 #include <meta/interface/intf_named.h>
27 #include <meta/interface/intf_object.h>
28 #include <meta/interface/intf_object_registry.h>
29 #include <meta/interface/intf_required_interfaces.h>
30 #include <meta/interface/property/construct_property.h>
31
META_BEGIN_NAMESPACE()32 META_BEGIN_NAMESPACE()
33
34 /**
35 * @brief Creates an instance of a class with given class id.
36 * @param clsi The class info of the class to instantiate.
37 * @return The created object.
38 */
39 inline IObject::Ptr CreateObjectInstance(const META_NS::ClassInfo& clsi)
40 {
41 return META_NS::GetObjectRegistry().Create(clsi);
42 }
43
44 /**
45 * @brief Wrapper class for for objects which implement IObject.
46 * @note Object can be initialized with Any object created through IObjectRegistry::Create().
47 */
48 class Object : public InterfaceObject<IObject> {
49 public:
META_INTERFACE_OBJECT(Object,InterfaceObject<IObject>,IObject)50 META_INTERFACE_OBJECT(Object, InterfaceObject<IObject>, IObject)
51 META_INTERFACE_OBJECT_INSTANTIATE(Object, ClassId::Object)
52
53 /// Returns the class id of the underlying object.
54 auto GetClassId() const
55 {
56 return META_INTERFACE_OBJECT_CALL_PTR(GetClassId());
57 }
58 /// Returns the class name of the underlying object.
GetClassName()59 auto GetClassName() const
60 {
61 return META_INTERFACE_OBJECT_CALL_PTR(GetClassName());
62 }
63 /// Returns the name of the object.
GetName()64 auto GetName() const
65 {
66 return META_INTERFACE_OBJECT_CALL_PTR(GetName());
67 }
68 /// @see IObject::Resolve
Resolve(const RefUri & uri)69 auto Resolve(const RefUri& uri) const
70 {
71 return Object(META_INTERFACE_OBJECT_CALL_PTR(Resolve(uri)));
72 }
73 };
74
75 class ResetableObject : public META_NS::Object {
76 public:
META_INTERFACE_OBJECT(ResetableObject,META_NS::Object,IResetableObject)77 META_INTERFACE_OBJECT(ResetableObject, META_NS::Object, IResetableObject)
78 void ResetObject()
79 {
80 META_INTERFACE_OBJECT_CALL_PTR(ResetObject());
81 }
82 };
83
84 /**
85 * @brief Wrapper class for objects which implement IMetadata.
86 */
87 class Metadata : public InterfaceObject<IMetadata> {
88 public:
META_INTERFACE_OBJECT(Metadata,InterfaceObject<IMetadata>,IMetadata)89 META_INTERFACE_OBJECT(Metadata, InterfaceObject<IMetadata>, IMetadata)
90
91 /// @see IMetadata::GetProperties
92 auto GetProperties() const
93 {
94 return META_INTERFACE_OBJECT_CALL_PTR(GetProperties());
95 }
96 /// @see IMetadata::GetFunctions
GetFunctions()97 auto GetFunctions() const
98 {
99 return META_INTERFACE_OBJECT_CALL_PTR(GetFunctions());
100 }
101 /// @see IMetadata::GetEvents
GetEvents()102 auto GetEvents() const
103 {
104 return META_INTERFACE_OBJECT_CALL_PTR(GetEvents());
105 }
106 /// @see IMetadata::GetAllMetadatas
GetAllMetadatas(MetadataType types)107 auto GetAllMetadatas(MetadataType types) const
108 {
109 return META_INTERFACE_OBJECT_CALL_PTR(GetAllMetadatas(types));
110 }
111 /// @see IMetadata::GetMetadata
GetMetadata(MetadataType type,BASE_NS::string_view name)112 auto GetMetadata(MetadataType type, BASE_NS::string_view name) const
113 {
114 return META_INTERFACE_OBJECT_CALL_PTR(GetMetadata(type, name));
115 }
116 /// @see IMetadata::GetProperty
GetProperty(BASE_NS::string_view name,MetadataQuery query)117 auto GetProperty(BASE_NS::string_view name, MetadataQuery query) const
118 {
119 return META_INTERFACE_OBJECT_CALL_PTR(GetProperty(name, query));
120 }
121 /// @see IMetadata::GetProperty
GetProperty(BASE_NS::string_view name)122 auto GetProperty(BASE_NS::string_view name) const
123 {
124 return META_INTERFACE_OBJECT_CALL_PTR(GetProperty(name));
125 }
126 /// @see IMetadata::GetEvent
GetEvent(BASE_NS::string_view name)127 auto GetEvent(BASE_NS::string_view name) const
128 {
129 return META_INTERFACE_OBJECT_CALL_PTR(GetEvent(name));
130 }
131 /// @see IMetadata::GetFunction
GetFunction(BASE_NS::string_view name)132 auto GetFunction(BASE_NS::string_view name) const
133 {
134 return META_INTERFACE_OBJECT_CALL_PTR(GetFunction(name));
135 }
136 /// @see IMetadata::AddProperty
AddProperty(const IProperty::Ptr & property)137 auto AddProperty(const IProperty::Ptr& property)
138 {
139 return META_INTERFACE_OBJECT_CALL_PTR(AddProperty(property));
140 }
141 /// @see IMetadata::RemoveProperty
RemoveProperty(const IProperty::Ptr & property)142 auto RemoveProperty(const IProperty::Ptr& property)
143 {
144 return META_INTERFACE_OBJECT_CALL_PTR(RemoveProperty(property));
145 }
146 template<class T>
147 auto AddProperty(BASE_NS::string_view name, const T& value = {}, ObjectFlagBitsValue flags = ObjectFlagBits::NONE)
148 {
149 if (!GetProperty(name)) {
150 return AddProperty(ConstructProperty<T>(name, value, flags));
151 }
152 return false;
153 }
154 template<class T>
GetProperty(BASE_NS::string_view name)155 auto GetProperty(BASE_NS::string_view name) const
156 {
157 return META_INTERFACE_OBJECT_CALL_PTR(template GetProperty<T>(name));
158 }
159 };
160
161 /**
162 * @brief Wrapper class for objects which implement IContainer.
163 */
164 class Container : public InterfaceObject<IContainer> {
165 public:
META_INTERFACE_OBJECT(Container,InterfaceObject<IContainer>,IContainer)166 META_INTERFACE_OBJECT(Container, InterfaceObject<IContainer>, IContainer)
167 META_INTERFACE_OBJECT_INSTANTIATE(Container, ClassId::ObjectContainer)
168 auto GetAll() const
169 {
170 return Internal::ArrayCast<Object>(META_INTERFACE_OBJECT_CALL_PTR(GetAll()));
171 }
GetAt(IContainer::SizeType index)172 auto GetAt(IContainer::SizeType index)
173 {
174 return Object(META_INTERFACE_OBJECT_CALL_PTR(GetAt(index)));
175 }
GetSize()176 auto GetSize() const
177 {
178 return META_INTERFACE_OBJECT_CALL_PTR(GetSize());
179 }
FindAll(const IContainer::FindOptions & options)180 auto FindAll(const IContainer::FindOptions& options) const
181 {
182 return Internal::ArrayCast<Object>(META_INTERFACE_OBJECT_CALL_PTR(FindAll(options)));
183 }
FindAny(const IContainer::FindOptions & options)184 auto FindAny(const IContainer::FindOptions& options) const
185 {
186 return Object(META_INTERFACE_OBJECT_CALL_PTR(FindAny(options)));
187 }
FindByName(BASE_NS::string_view name)188 auto FindByName(BASE_NS::string_view name) const
189 {
190 return Object(META_INTERFACE_OBJECT_CALL_PTR(FindByName(name)));
191 }
Add(const IObject::Ptr & object)192 auto Add(const IObject::Ptr& object)
193 {
194 return META_INTERFACE_OBJECT_CALL_PTR(Add(object));
195 }
Insert(IContainer::SizeType index,const IObject::Ptr & object)196 auto Insert(IContainer::SizeType index, const IObject::Ptr& object)
197 {
198 return META_INTERFACE_OBJECT_CALL_PTR(Insert(index, object));
199 }
Remove(IContainer::SizeType index)200 auto Remove(IContainer::SizeType index)
201 {
202 return META_INTERFACE_OBJECT_CALL_PTR(Remove(index));
203 }
Remove(const IObject::Ptr & child)204 auto Remove(const IObject::Ptr& child)
205 {
206 return META_INTERFACE_OBJECT_CALL_PTR(Add(child));
207 }
Move(IContainer::SizeType fromIndex,IContainer::SizeType toIndex)208 auto Move(IContainer::SizeType fromIndex, IContainer::SizeType toIndex)
209 {
210 return META_INTERFACE_OBJECT_CALL_PTR(Move(fromIndex, toIndex));
211 }
Move(const IObject::Ptr & child,IContainer::SizeType toIndex)212 auto Move(const IObject::Ptr& child, IContainer::SizeType toIndex)
213 {
214 return META_INTERFACE_OBJECT_CALL_PTR(Move(child, toIndex));
215 }
Replace(const IObject::Ptr & child,const IObject::Ptr & replaceWith)216 auto Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith)
217 {
218 return META_INTERFACE_OBJECT_CALL_PTR(Replace(child, replaceWith));
219 }
Replace(const IObject::Ptr & child,const IObject::Ptr & replaceWith,bool addAlways)220 auto Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways)
221 {
222 return META_INTERFACE_OBJECT_CALL_PTR(Replace(child, replaceWith, addAlways));
223 }
RemoveAll()224 auto RemoveAll()
225 {
226 META_INTERFACE_OBJECT_CALL_PTR(RemoveAll());
227 }
228 };
229
230 /**
231 * @brief Wrapper class for objects which implement INamed.
232 */
233 class Named : public Object {
234 public:
235 META_INTERFACE_OBJECT(Named, Object, INamed)
236 /// @see INamed::Name
237 META_INTERFACE_OBJECT_PROPERTY(BASE_NS::string, Name)
238 };
239
240 /**
241 * @brief Wrapper class for objects which implement IAttach.
242 */
243 class AttachmentContainer : public InterfaceObject<IAttach> {
244 public:
META_INTERFACE_OBJECT(AttachmentContainer,InterfaceObject<IAttach>,IAttach)245 META_INTERFACE_OBJECT(AttachmentContainer, InterfaceObject<IAttach>, IAttach)
246 template<class T, class U>
247 auto Attach(const T& object, const U& dataContext)
248 {
249 return META_INTERFACE_OBJECT_CALL_PTR(
250 Attach(interface_pointer_cast<IObject>(object), interface_pointer_cast<IObject>(dataContext)));
251 }
252 template<class T>
Attach(const T & object)253 auto Attach(const T& object)
254 {
255 return Attach(interface_pointer_cast<IObject>(object), META_NS::IObject::Ptr {});
256 }
257 template<class T>
Detach(const T & object)258 auto Detach(const T& object)
259 {
260 return META_INTERFACE_OBJECT_CALL_PTR(Detach(interface_pointer_cast<IObject>(object)));
261 }
HasAttachments()262 bool HasAttachments() const
263 {
264 return META_INTERFACE_OBJECT_CALL_PTR(HasAttachments());
265 }
266 auto GetAttachments(BASE_NS::array_view<const TypeId> uids, bool strict = false) const
267 {
268 return META_INTERFACE_OBJECT_CALL_PTR(
269 GetAttachments(BASE_NS::vector<TypeId>(uids.begin(), uids.end()), strict));
270 }
GetAttachments()271 auto GetAttachments() const
272 {
273 return META_INTERFACE_OBJECT_CALL_PTR(GetAttachments());
274 }
275 template<class T>
GetAttachments()276 BASE_NS::vector<typename T::Ptr> GetAttachments() const
277 {
278 return META_INTERFACE_OBJECT_CALL_PTR(template GetAttachments<T>());
279 }
280 };
281
282 /**
283 * @brief Wrapper class for objects which implement IContent.
284 */
285 class ContentObject : public Object {
286 public:
META_INTERFACE_OBJECT(ContentObject,Object,IContent)287 META_INTERFACE_OBJECT(ContentObject, Object, IContent)
288
289 META_INTERFACE_OBJECT_READONLY_PROPERTY(IObject::Ptr, Content)
290 META_INTERFACE_OBJECT_PROPERTY(bool, ContentSearchable)
291 auto SetContent(const IObject::Ptr& content) const
292 {
293 return META_INTERFACE_OBJECT_CALL_PTR(SetContent(content));
294 }
SerializeContent(bool serialize)295 auto& SerializeContent(bool serialize)
296 {
297 auto object = GetPtr<IObject>();
298 META_NS::SetObjectFlags(object, ObjectFlagBits::SERIALIZE_HIERARCHY, serialize);
299 return *this;
300 }
301 };
302
303 /**
304 * @brief Wrapper class for objects which implement IRequiredInterfaces.
305 */
306 class RequiredInterfaces : public InterfaceObject<IRequiredInterfaces> {
307 public:
META_INTERFACE_OBJECT(RequiredInterfaces,InterfaceObject<IRequiredInterfaces>,IRequiredInterfaces)308 META_INTERFACE_OBJECT(RequiredInterfaces, InterfaceObject<IRequiredInterfaces>, IRequiredInterfaces)
309
310 auto SetRequiredInterfaces(const BASE_NS::vector<TypeId>& interfaces)
311 {
312 return META_INTERFACE_OBJECT_CALL_PTR(SetRequiredInterfaces(interfaces));
313 }
GetRequiredInterfaces()314 auto GetRequiredInterfaces() const
315 {
316 return META_INTERFACE_OBJECT_CALL_PTR(GetRequiredInterfaces());
317 }
318 };
319
320 /**
321 * @brief Creates an instance of a class with given class id and converts it to a given interface type.
322 * @param clsi The class info of the class to instantiate.
323 * @return The created object converted to given interface type.
324 */
325 template<typename Interface>
CreateObjectInstance(const META_NS::ClassInfo & clsi)326 inline auto CreateObjectInstance(const META_NS::ClassInfo& clsi)
327 {
328 return interface_pointer_cast<Interface>(CreateObjectInstance(clsi));
329 }
330 template<typename Interface>
331 auto CreateObjectInstance() = delete;
332 /// Returns a default object which implements IObject
333 template<>
334 inline auto CreateObjectInstance<IObject>()
335 {
336 return Object(CreateNew);
337 }
338 /// Returns a default object which implements IMetadata
339 template<>
340 inline auto CreateObjectInstance<IMetadata>()
341 {
342 return Metadata(CreateObjectInstance(META_NS::ClassId::Object));
343 }
344 /// Returns a default object which implements IAttach
345 template<>
346 inline auto CreateObjectInstance<IAttach>()
347 {
348 return AttachmentContainer(CreateObjectInstance(META_NS::ClassId::Object));
349 }
350 /// Returns a default object which implements IContainer
351 template<>
352 inline auto CreateObjectInstance<IContainer>()
353 {
354 return Container(CreateNew);
355 }
356
357 META_END_NAMESPACE()
358
359 #endif // META_API_OBJECT_H
360