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_INTERFACE_IOBJECT_REGISTRY_H
17 #define META_INTERFACE_IOBJECT_REGISTRY_H
18
19 #include <base/containers/vector.h>
20 #include <core/plugin/intf_plugin.h>
21 #include <core/plugin/intf_plugin_register.h>
22
23 #include <meta/base/ids.h>
24 #include <meta/base/meta_types.h>
25 #include <meta/base/namespace.h>
26 #include <meta/base/object_traits.h>
27 #include <meta/interface/intf_meta_object_lib.h>
28 #include <meta/interface/object_type_info.h>
29 #include <meta/interface/property/intf_property.h>
30 #include <meta/interface/serialization/intf_global_serialization_data.h>
31
32 META_BEGIN_NAMESPACE()
33 /**
34 * @brief Information for an object category.
35 */
36 struct ObjectCategoryItem {
37 ObjectCategoryBits category;
38 BASE_NS::string_view name;
39 };
40
41 class IInterpolator;
42 class IObjectRegistry;
43 class ICallContext;
44 class IObjectContext;
45 class IObjectFactory;
46 class IObject;
47 class IClassRegistry;
48 class IPropertyRegister;
49 class IMetadata;
50 class IEngineData;
51
52 META_REGISTER_INTERFACE(IObjectRegistry, "1b2081c8-031f-4962-bb97-de2209573e96")
53 META_REGISTER_INTERFACE(IObjectRegistryExporter, "79e4e8ec-7da5-4757-a819-a1817e4bdd4f")
54
55 /**
56 * @brief The IObjectRegistryExporter defines an interface for exporting an
57 * object registry instance to string. Used for debugging purposes.
58 */
59 class IObjectRegistryExporter : public CORE_NS::IInterface {
60 META_INTERFACE(CORE_NS::IInterface, IObjectRegistryExporter)
61 public:
62 /**
63 * @brief Return a string representation of all objects alive within an object
64 * registry instance.
65 * @param registry The object registry instance to export.
66 */
67 virtual BASE_NS::string ExportRegistry(const IObjectRegistry* registry) = 0;
68 };
69
70 /**
71 * @brief The IObjectRegistry interface can be used to create widgets of any type registered with the registry.
72 */
73 class IObjectRegistry : public CORE_NS::IInterface {
74 META_INTERFACE(CORE_NS::IInterface, IObjectRegistry)
75 public:
76 /**
77 * @brief The CreateInfo struct defines set of object creation parameters that the user can give
78 * as a parameter when creating an object. In most cases the create information does not
79 * need to be filled.
80 */
81 struct CreateInfo {
CreateInfoCreateInfo82 constexpr CreateInfo() noexcept : instanceId(), isGloballyAvailable(false) {};
CreateInfoCreateInfo83 constexpr CreateInfo(InstanceId id) noexcept : instanceId(id), isGloballyAvailable(false) {};
CreateInfoCreateInfo84 constexpr CreateInfo(InstanceId id, bool global) noexcept : instanceId(id), isGloballyAvailable(global) {};
85 /** Specify instance id of the object to create. This should usually be left empty. */
86 InstanceId instanceId;
87 /** If true, the instance should be considered as a global object, This will affect
88 serialization, as global objects are only serialized as a reference. The references
89 are assumed to be always available even when de-serializing the object in some
90 other toolkit instance. */
91 bool isGloballyAvailable;
92 };
93
94 /**
95 * @brief Registers a class to the registry. Only registered objects can be constructed through Create().
96 * @param classInfo Class info for the class. Class info is assumed to not change while the class is
97 * registered.
98 * @return True if class was successfully registered, false otherwise.
99 */
100 virtual bool RegisterObjectType(const IClassInfo::Ptr& classInfo) = 0;
101
102 /**
103 * @brief Unregisters a class from the registry. Must be called before the plugin that called RegisterObjectType
104 * is unloaded.
105 * @param classInfo The info for the class to unregister.
106 * @return True if class was successfully unregistered, false otherwise.
107 */
108 virtual bool UnregisterObjectType(const IClassInfo::Ptr& classInfo) = 0;
109 /**
110 * @brief A helper template for calling RegisterObjectType on a type which defines IObjectFactory::Ptr GetFactory().
111 */
112 template<class T>
RegisterObjectType()113 constexpr void RegisterObjectType()
114 {
115 RegisterObjectType(T::GetFactory());
116 }
117 /**
118 * @brief A helper template for calling UnregisterObjectType on a type which defines IObjectFactory::Ptr
119 * GetFactory().
120 */
121 template<class T>
UnregisterObjectType()122 constexpr void UnregisterObjectType()
123 {
124 UnregisterObjectType(T::GetFactory());
125 }
126 /**
127 * @brief Creates a new instance of an object of a given type.
128 * @param uid Uid of the Object to create.
129 * @return The Object of correct type or nullptr if creation was not successful.
130 */
Create(ObjectId id)131 BASE_NS::shared_ptr<IObject> Create(ObjectId id) const
132 {
133 return Create(BASE_NS::move(id), CreateInfo {});
134 }
135
136 virtual BASE_NS::shared_ptr<IObject> Create(
137 ObjectId id, const CreateInfo& createInfo, const BASE_NS::shared_ptr<IMetadata>& data) const = 0;
138
139 /**
140 * @brief Creates a new instance of an object of a given type and specific creation info
141 * @param uid Uid of the Object to create.
142 * @param createInfo Force some parameters for the created object.
143 * @return The Object of correct type or nullptr if creation was not successful.
144 */
145 virtual BASE_NS::shared_ptr<IObject> Create(ObjectId id, const CreateInfo& createInfo) const = 0;
146
147 /**
148 * @brief Creates a new instance of an object of a given type.
149 * If the singleton flag is true, Object registry will store a weak reference to
150 * the created object, so as long as any caller has a strong reference to the
151 * object any further calls to Create will return this same object.
152 * @param info ClassInfo of the Object to create.
153 * @return The Object of correct type or nullptr if creation was not successful.
154 */
Create(const META_NS::ClassInfo & info)155 BASE_NS::shared_ptr<IObject> Create(const META_NS::ClassInfo& info) const
156 {
157 return Create(info, CreateInfo {});
158 }
Create(const META_NS::ClassInfo & info,const BASE_NS::shared_ptr<IMetadata> & data)159 BASE_NS::shared_ptr<IObject> Create(
160 const META_NS::ClassInfo& info, const BASE_NS::shared_ptr<IMetadata>& data) const
161 {
162 return Create(info, CreateInfo {}, data);
163 }
164
165 /**
166 * @brief Creates a new instance of an object of a given type specific creation info.
167 * If the singleton flag is true, Object registry will store a weak reference to
168 * the created object, so as long as any caller has a strong reference to the
169 * object any further calls to Create will return this same object.
170 * @param info ClassInfo of the Object to create.
171 * @param createInfo Force some parameters for the created object.
172 * @return The Object of correct type or nullptr if creation was not successful.
173 */
174 virtual BASE_NS::shared_ptr<IObject> Create(const META_NS::ClassInfo& info, const CreateInfo& createInfo) const = 0;
175
176 /**
177 * @brief Returns all available object categories. Use GetAllTypes() for a list of
178 * objects that can be created with Create() for a specific category.
179 */
180 virtual BASE_NS::vector<ObjectCategoryItem> GetAllCategories() const = 0;
181 /**
182 * @brief Returns a type information list of all registered object types for given categories.
183 * @param category The category to list. If category is ObjectCategoryBits::ANY, all
184 * registered object type infos are returned, regardless of category.
185 */
GetAllTypes(ObjectCategoryBits category)186 BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(ObjectCategoryBits category) const
187 {
188 return GetAllTypes(category, true, true);
189 }
190
191 virtual BASE_NS::shared_ptr<const IObjectFactory> GetObjectFactory(const ObjectId& uid) const = 0;
192
193 /**
194 * @brief Returns a type information list of all registered object types for a given category.
195 * @param category The category to list. If category is ObjectCategoryBits::ANY, all
196 * registered object type infos are returned, regardless of category.
197 * @param strict If true, all of the given category bits much be set for a type to be returned
198 * in the list. If false, if any of the category bits are set for a type it will be added
199 * to the list.
200 */
201 virtual BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(
202 ObjectCategoryBits category, bool strict, bool excludeDeprecated) const = 0;
203 /**
204 * @brief Returns an object instance with the given instance id.
205 * @param uid Instance id of the object to return.
206 * @return Object or nullptr if uid is does not point to a valid object.
207 */
208 virtual BASE_NS::shared_ptr<IObject> GetObjectInstanceByInstanceId(InstanceId uid) const = 0;
209 /**
210 * @brief Returns a list of all objects which are currently alive.
211 * @note The list includes both regular and singleton objects.
212 */
213 virtual BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetAllObjectInstances() const = 0;
214 /**
215 * @brief Returns a list of all singleton objects which are currently alive.
216 */
217 virtual BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetAllSingletonObjectInstances() const = 0;
218
219 /**
220 * @brief Remove any references to dead instances
221 */
222 virtual void Purge() = 0;
223
224 /**
225 * @brief Tell object registry that object with given uid is not used any more and so references can be removed
226 */
227 virtual void DisposeObject(const InstanceId&) const = 0;
228
229 /**
230 * @brief Construct empty default call context structure
231 */
232 virtual BASE_NS::shared_ptr<ICallContext> ConstructDefaultCallContext() const = 0;
233
234 /**
235 * @brief Returns a list of all objects which are currently alive and belong to a all specified object
236 * categories.
237 * @param category The category to return.
238 */
GetObjectInstancesByCategory(ObjectCategoryBits category)239 BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetObjectInstancesByCategory(ObjectCategoryBits category) const
240 {
241 return GetObjectInstancesByCategory(category, true);
242 }
243 /**
244 * @brief Returns a list of all objects which are currently alive and belong to a specified object
245 * category.
246 * @param category The category to return.
247 * @param strict If true, all of the given category bits much be set for a type to be returned
248 * in the list. If false, if any of the category bits are set for a type it will be added
249 * to the list.
250 */
251 virtual BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetObjectInstancesByCategory(
252 ObjectCategoryBits category, bool strict) const = 0;
253 /**
254 * @brief Returns a string dump of all objects which are currently alive and created through
255 * this object registry instance.
256 * @param exporter The exporter to use.
257 */
258 virtual BASE_NS::string ExportToString(const IObjectRegistryExporter::Ptr& exporter) const = 0;
259 /**
260 * @brief Returns the default object context.
261 */
262 virtual BASE_NS::shared_ptr<IObjectContext> GetDefaultObjectContext() const = 0;
263
264 // Templated helper method, Creates object by UID and returns the requested Interface (if available)
265 template<typename Interface>
Create(ObjectId uid)266 typename Interface::Ptr Create(ObjectId uid) const
267 {
268 auto p = Create(uid);
269 if (p) {
270 return interface_pointer_cast<Interface>(p);
271 }
272 return nullptr;
273 }
274 template<typename Interface>
Create(ObjectId uid,BASE_NS::shared_ptr<IMetadata> data)275 typename Interface::Ptr Create(ObjectId uid, BASE_NS::shared_ptr<IMetadata> data) const
276 {
277 auto p = Create(META_NS::ClassInfo { uid }, data);
278 if (p) {
279 return interface_pointer_cast<Interface>(p);
280 }
281 return nullptr;
282 }
283 template<typename Interface>
Create(ObjectId uid,InstanceId instanceid)284 typename Interface::Ptr Create(ObjectId uid, InstanceId instanceid) const
285 {
286 auto p = Create(uid, instanceid);
287 if (p) {
288 return interface_pointer_cast<Interface>(p);
289 }
290 return nullptr;
291 }
292 template<typename Interface>
Create(ObjectId uid,const CreateInfo & createInfo)293 typename Interface::Ptr Create(ObjectId uid, const CreateInfo& createInfo) const
294 {
295 auto p = Create(uid, createInfo);
296 if (p) {
297 return interface_pointer_cast<Interface>(p);
298 }
299 return nullptr;
300 }
301
302 /**
303 * @brief Registers an interpolator class for a property type. Any previously registered interpolators
304 * for the given property type are unregistered.
305 * After registration, an interpolator for a property type can be created by calling one of the
306 * CreateInterpolator methods.
307 * @param propertyTypeUid Property type.
308 * @param interpolatorClassUid Interpolator class id.
309 */
310 virtual void RegisterInterpolator(TypeId propertyTypeUid, BASE_NS::Uid interpolatorClassUid) = 0;
311 /**
312 * @brief Unregisters an interpolator for a given property type.
313 * @param propertyTypeUid The property type to unregister.
314 */
315 virtual void UnregisterInterpolator(TypeId propertyTypeUid) = 0;
316 /**
317 * @brief Returns true if an interpolator has been registered for the given property type.
318 * @note This function checks if a type-specific interpolator has been registered, and ignores
319 * the default interpolator.
320 * @param propertyTypeUid Property type to check.
321 */
322 virtual bool HasInterpolator(TypeId propertyTypeUid) const = 0;
323 /**
324 * @brief Creates an interpolator object for given property type.
325 * @param propertyTypeUid The property type.
326 * @note If an interpolator for the given property type has not been registered, a default
327 * interpolator is returned. This default interpolator just steps the property value
328 * without interpolation.
329 * @return Interpolator object or the default interpolator
330 */
331 virtual BASE_NS::shared_ptr<IInterpolator> CreateInterpolator(TypeId propertyTypeUid) = 0;
332 /**
333 * @brief Creates an interpolator object for given property, based on its type.
334 * @param property The property to create interpolator for.
335 * @return Interpolator object or null if an interpolator could not be created.
336 */
CreateInterpolator(const IProperty::ConstPtr & property)337 BASE_NS::shared_ptr<IInterpolator> CreateInterpolator(const IProperty::ConstPtr& property)
338 {
339 if (!property) {
340 return {};
341 }
342 return CreateInterpolator(property->GetTypeId());
343 }
344 /**
345 * @brief See HasInterpolator(BASE_NS::Uid propertyTypeUid)
346 */
HasInterpolator(const IProperty::ConstPtr & property)347 bool HasInterpolator(const IProperty::ConstPtr& property) const
348 {
349 if (!property) {
350 return false;
351 }
352 return HasInterpolator(property->GetTypeId());
353 }
354 /**
355 * @brief Returns the class registry instance used by the object registry instance.
356 */
357 virtual IClassRegistry& GetClassRegistry() = 0;
358 virtual IPropertyRegister& GetPropertyRegister() = 0;
359 virtual IGlobalSerializationData& GetGlobalSerializationData() = 0;
360
361 virtual IEngineData& GetEngineData() = 0;
362
363 virtual IObject::Ptr DefaultResolveObject(const IObjectInstance::Ptr& base, const RefUri& uri) const = 0;
364 virtual BASE_NS::shared_ptr<IMetadata> ConstructObjectDataContainer() = 0;
365 };
366
367 /** Returns a reference to the IWidgetRegistry instance. InitializeToolkit or InitializeToolKitAndEngine must be called
368 * before calling this function. */
GetObjectRegistry()369 inline META_NS::IObjectRegistry& GetObjectRegistry()
370 {
371 return GetMetaObjectLib().GetObjectRegistry();
372 }
373
374 /**
375 * @brief Retuns the global class registry instance.
376 */
GetClassRegistry()377 inline META_NS::IClassRegistry& GetClassRegistry()
378 {
379 return GetObjectRegistry().GetClassRegistry();
380 }
381
382 /**
383 * @brief Returns the default context object from the global object registry.
384 */
GetDefaultObjectContext()385 inline BASE_NS::shared_ptr<IObjectContext> GetDefaultObjectContext()
386 {
387 return META_NS::GetObjectRegistry().GetDefaultObjectContext();
388 }
389
390 /**
391 * @brief Registers Type to the default object registry.
392 */
393 template<typename Type>
RegisterObjectType()394 constexpr void RegisterObjectType()
395 {
396 META_NS::GetObjectRegistry().RegisterObjectType<Type>();
397 }
398
399 /**
400 * @brief Unregisters Type from default object registry.
401 */
402 template<typename Type>
UnregisterObjectType()403 constexpr void UnregisterObjectType()
404 {
405 META_NS::GetObjectRegistry().UnregisterObjectType<Type>();
406 }
407
408 META_END_NAMESPACE()
409
410 #endif
411