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