• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 call context structure
232      */
233     virtual BASE_NS::shared_ptr<ICallContext> ConstructDefaultCallContext() const = 0;
234 
235     /**
236      * @brief Returns a list of all objects which are currently alive and belong to a all specified object
237      *        categories.
238      * @param category The category to return.
239      */
GetObjectInstancesByCategory(ObjectCategoryBits category)240     BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetObjectInstancesByCategory(ObjectCategoryBits category) const
241     {
242         return GetObjectInstancesByCategory(category, true);
243     }
244     /**
245      * @brief Returns a list of all objects which are currently alive and belong to a specified object
246      *        category.
247      * @param category The category to return.
248      * @param strict If true, all of the given category bits much be set for a type to be returned
249      *        in the list. If false, if any of the category bits are set for a type it will be added
250      *        to the list.
251      */
252     virtual BASE_NS::vector<BASE_NS::shared_ptr<IObject>> GetObjectInstancesByCategory(
253         ObjectCategoryBits category, bool strict) const = 0;
254     /**
255      * @brief Returns a string dump of all objects which are currently alive and created through
256      *        this object registry instance.
257      * @param exporter The exporter to use.
258      */
259     virtual BASE_NS::string ExportToString(const IObjectRegistryExporter::Ptr& exporter) const = 0;
260     /**
261      * @brief Returns the default object context.
262      */
263     virtual BASE_NS::shared_ptr<IObjectContext> GetDefaultObjectContext() const = 0;
264 
265     // Templated helper method, Creates object by UID and returns the requested Interface (if available)
266     template<typename Interface>
Create(ObjectId uid)267     typename Interface::Ptr Create(ObjectId uid) const
268     {
269         auto p = Create(uid);
270         if (p) {
271             return interface_pointer_cast<Interface>(p);
272         }
273         return nullptr;
274     }
275     template<typename Interface>
Create(ObjectId uid,BASE_NS::shared_ptr<IMetadata> data)276     typename Interface::Ptr Create(ObjectId uid, BASE_NS::shared_ptr<IMetadata> data) const
277     {
278         auto p = Create(META_NS::ClassInfo { uid }, data);
279         if (p) {
280             return interface_pointer_cast<Interface>(p);
281         }
282         return nullptr;
283     }
284     template<typename Interface>
Create(ObjectId uid,InstanceId instanceid)285     typename Interface::Ptr Create(ObjectId uid, InstanceId instanceid) const
286     {
287         auto p = Create(uid, instanceid);
288         if (p) {
289             return interface_pointer_cast<Interface>(p);
290         }
291         return nullptr;
292     }
293     template<typename Interface>
Create(ObjectId uid,const CreateInfo & createInfo)294     typename Interface::Ptr Create(ObjectId uid, const CreateInfo& createInfo) const
295     {
296         auto p = Create(uid, createInfo);
297         if (p) {
298             return interface_pointer_cast<Interface>(p);
299         }
300         return nullptr;
301     }
302 
303     /**
304      * @brief Registers an interpolator class for a property type. Any previously registered interpolators
305      *        for the given property type are unregistered.
306      *        After registration, an interpolator for a property type can be created by calling one of the
307      *        CreateInterpolator methods.
308      * @param propertyTypeUid Property type.
309      * @param interpolatorClassUid Interpolator class id.
310      */
311     virtual void RegisterInterpolator(TypeId propertyTypeUid, BASE_NS::Uid interpolatorClassUid) = 0;
312     /**
313      * @brief Unregisters an interpolator for a given property type.
314      * @param propertyTypeUid The property type to unregister.
315      */
316     virtual void UnregisterInterpolator(TypeId propertyTypeUid) = 0;
317     /**
318      * @brief Returns true if an interpolator has been registered for the given property type.
319      * @note  This function checks if a type-specific interpolator has been registered, and ignores
320      *        the default interpolator.
321      * @param propertyTypeUid Property type to check.
322      */
323     virtual bool HasInterpolator(TypeId propertyTypeUid) const = 0;
324     /**
325      * @brief Creates an interpolator object for given property type.
326      * @param propertyTypeUid The property type.
327      * @note  If an interpolator for the given property type has not been registered, a default
328      *        interpolator is returned. This default interpolator just steps the property value
329      *        without interpolation.
330      * @return Interpolator object or the default interpolator
331      */
332     virtual BASE_NS::shared_ptr<IInterpolator> CreateInterpolator(TypeId propertyTypeUid) = 0;
333     /**
334      * @brief Creates an interpolator object for given property, based on its type.
335      * @param property The property to create interpolator for.
336      * @return Interpolator object or null if an interpolator could not be created.
337      */
CreateInterpolator(const IProperty::ConstPtr & property)338     BASE_NS::shared_ptr<IInterpolator> CreateInterpolator(const IProperty::ConstPtr& property)
339     {
340         if (!property) {
341             return {};
342         }
343         return CreateInterpolator(property->GetTypeId());
344     }
345     /**
346      * @brief See HasInterpolator(BASE_NS::Uid propertyTypeUid)
347      */
HasInterpolator(const IProperty::ConstPtr & property)348     bool HasInterpolator(const IProperty::ConstPtr& property) const
349     {
350         if (!property) {
351             return false;
352         }
353         return HasInterpolator(property->GetTypeId());
354     }
355     /**
356      * @brief Returns the class registry instance used by the object registry instance.
357      */
358     virtual IClassRegistry& GetClassRegistry() = 0;
359     virtual IPropertyRegister& GetPropertyRegister() = 0;
360     virtual IGlobalSerializationData& GetGlobalSerializationData() = 0;
361 
362     virtual IEngineData& GetEngineData() = 0;
363 
364     virtual IObject::Ptr DefaultResolveObject(const IObjectInstance::Ptr& base, const RefUri& uri) const = 0;
365     virtual BASE_NS::shared_ptr<IMetadata> ConstructObjectDataContainer() = 0;
366 };
367 
368 /** Returns a reference to the IWidgetRegistry instance. InitializeToolkit or InitializeToolKitAndEngine must be called
369  *  before calling this function. */
GetObjectRegistry()370 inline META_NS::IObjectRegistry& GetObjectRegistry()
371 {
372     return GetMetaObjectLib().GetObjectRegistry();
373 }
374 
375 /**
376  * @brief Retuns the global class registry instance.
377  */
GetClassRegistry()378 inline META_NS::IClassRegistry& GetClassRegistry()
379 {
380     return GetObjectRegistry().GetClassRegistry();
381 }
382 
383 /**
384  * @brief Returns the default context object from the global object registry.
385  */
GetDefaultObjectContext()386 inline BASE_NS::shared_ptr<IObjectContext> GetDefaultObjectContext()
387 {
388     return META_NS::GetObjectRegistry().GetDefaultObjectContext();
389 }
390 
391 /**
392  * @brief Registers Type to the default object registry.
393  */
394 template<typename Type>
RegisterObjectType()395 constexpr void RegisterObjectType()
396 {
397     META_NS::GetObjectRegistry().RegisterObjectType<Type>();
398 }
399 
400 /**
401  * @brief Unregisters Type from default object registry.
402  */
403 template<typename Type>
UnregisterObjectType()404 constexpr void UnregisterObjectType()
405 {
406     META_NS::GetObjectRegistry().UnregisterObjectType<Type>();
407 }
408 
409 META_END_NAMESPACE()
410 
411 #endif
412