18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#ifndef META_SRC_OBJECT_REGISTRY_H 178bf80f4bSopenharmony_ci#define META_SRC_OBJECT_REGISTRY_H 188bf80f4bSopenharmony_ci 198bf80f4bSopenharmony_ci#include <atomic> 208bf80f4bSopenharmony_ci#include <shared_mutex> 218bf80f4bSopenharmony_ci 228bf80f4bSopenharmony_ci#include <base/containers/unordered_map.h> 238bf80f4bSopenharmony_ci 248bf80f4bSopenharmony_ci#include <meta/base/interface_macros.h> 258bf80f4bSopenharmony_ci#include <meta/base/namespace.h> 268bf80f4bSopenharmony_ci#include <meta/interface/animation/intf_interpolator.h> 278bf80f4bSopenharmony_ci#include <meta/interface/engine/intf_engine_data.h> 288bf80f4bSopenharmony_ci#include <meta/interface/intf_call_context.h> 298bf80f4bSopenharmony_ci#include <meta/interface/intf_metadata.h> 308bf80f4bSopenharmony_ci#include <meta/interface/intf_object_context.h> 318bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h> 328bf80f4bSopenharmony_ci#include <meta/interface/intf_task_queue_registry.h> 338bf80f4bSopenharmony_ci 348bf80f4bSopenharmony_ci#include "class_registry.h" 358bf80f4bSopenharmony_ci 368bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 378bf80f4bSopenharmony_ci 388bf80f4bSopenharmony_ciclass IRandom { 398bf80f4bSopenharmony_cipublic: 408bf80f4bSopenharmony_ci virtual ~IRandom() = default; 418bf80f4bSopenharmony_ci virtual void Initialize(uint64_t seed) = 0; 428bf80f4bSopenharmony_ci virtual uint64_t GetRandom() = 0; 438bf80f4bSopenharmony_ci}; 448bf80f4bSopenharmony_ci 458bf80f4bSopenharmony_ciclass ObjectRegistry final : public IObjectRegistry, 468bf80f4bSopenharmony_ci public ITaskQueueRegistry, 478bf80f4bSopenharmony_ci public IPropertyRegister, 488bf80f4bSopenharmony_ci public IGlobalSerializationData, 498bf80f4bSopenharmony_ci public IEngineData { 508bf80f4bSopenharmony_cipublic: 518bf80f4bSopenharmony_ci META_NO_COPY_MOVE(ObjectRegistry) 528bf80f4bSopenharmony_ci 538bf80f4bSopenharmony_ci ObjectRegistry(); 548bf80f4bSopenharmony_ci ~ObjectRegistry() override; 558bf80f4bSopenharmony_ci 568bf80f4bSopenharmony_ci IClassRegistry& GetClassRegistry() override; 578bf80f4bSopenharmony_ci 588bf80f4bSopenharmony_ci bool RegisterObjectType(const IClassInfo::Ptr& classInfo) override; 598bf80f4bSopenharmony_ci bool UnregisterObjectType(const IClassInfo::Ptr& classInfo) override; 608bf80f4bSopenharmony_ci 618bf80f4bSopenharmony_ci IObject::Ptr Create(ObjectId uid, const CreateInfo& createInfo, const IMetadata::Ptr& data) const override; 628bf80f4bSopenharmony_ci IObject::Ptr Create(ObjectId uid, const CreateInfo& createInfo) const override; 638bf80f4bSopenharmony_ci IObject::Ptr Create(const ClassInfo& info, const CreateInfo& createInfo) const override; 648bf80f4bSopenharmony_ci 658bf80f4bSopenharmony_ci IObjectFactory::ConstPtr GetObjectFactory(const ObjectId& uid) const override; 668bf80f4bSopenharmony_ci BASE_NS::vector<ObjectCategoryItem> GetAllCategories() const override; 678bf80f4bSopenharmony_ci BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes( 688bf80f4bSopenharmony_ci ObjectCategoryBits category, bool strict, bool excludeDeprecated) const override; 698bf80f4bSopenharmony_ci 708bf80f4bSopenharmony_ci IObject::Ptr GetObjectInstanceByInstanceId(InstanceId uid) const override; 718bf80f4bSopenharmony_ci BASE_NS::vector<IObject::Ptr> GetAllObjectInstances() const override; 728bf80f4bSopenharmony_ci BASE_NS::vector<IObject::Ptr> GetAllSingletonObjectInstances() const override; 738bf80f4bSopenharmony_ci BASE_NS::vector<IObject::Ptr> GetObjectInstancesByCategory(ObjectCategoryBits category, bool strict) const override; 748bf80f4bSopenharmony_ci 758bf80f4bSopenharmony_ci BASE_NS::string ExportToString(const IObjectRegistryExporter::Ptr& exporter) const override; 768bf80f4bSopenharmony_ci IObjectContext::Ptr GetDefaultObjectContext() const override; 778bf80f4bSopenharmony_ci 788bf80f4bSopenharmony_ci void Purge() override; 798bf80f4bSopenharmony_ci void DisposeObject(const InstanceId&) const override; 808bf80f4bSopenharmony_ci IMetadata::Ptr ConstructMetadata() const override; 818bf80f4bSopenharmony_ci ICallContext::Ptr ConstructDefaultCallContext() const override; 828bf80f4bSopenharmony_ci 838bf80f4bSopenharmony_ci const IInterface* GetInterface(const BASE_NS::Uid& uid) const override; 848bf80f4bSopenharmony_ci IInterface* GetInterface(const BASE_NS::Uid& uid) override; 858bf80f4bSopenharmony_ci 868bf80f4bSopenharmony_ci IPropertyRegister& GetPropertyRegister() override; 878bf80f4bSopenharmony_ci IProperty::Ptr Create(const ObjectId& object, BASE_NS::string_view name) const override; 888bf80f4bSopenharmony_ci IBind::Ptr CreateBind() const override; 898bf80f4bSopenharmony_ci IAny& InvalidAny() const override; 908bf80f4bSopenharmony_ci IAny::Ptr ConstructAny(const ObjectId& id) const override; 918bf80f4bSopenharmony_ci bool IsAnyRegistered(const ObjectId& id) const override; 928bf80f4bSopenharmony_ci void RegisterAny(BASE_NS::shared_ptr<AnyBuilder> builder) override; 938bf80f4bSopenharmony_ci void UnregisterAny(const ObjectId& id) override; 948bf80f4bSopenharmony_ci 958bf80f4bSopenharmony_ci // Interpolators 968bf80f4bSopenharmony_ci void RegisterInterpolator(TypeId propertyTypeUid, BASE_NS::Uid interpolatorClassUid) override; 978bf80f4bSopenharmony_ci void UnregisterInterpolator(TypeId propertyTypeUid) override; 988bf80f4bSopenharmony_ci bool HasInterpolator(TypeId propertyTypeUid) const override; 998bf80f4bSopenharmony_ci IInterpolator::Ptr CreateInterpolator(TypeId propertyTypeUid) override; 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_ci IGlobalSerializationData& GetGlobalSerializationData() override; 1028bf80f4bSopenharmony_ci SerializationSettings GetDefaultSettings() const override; 1038bf80f4bSopenharmony_ci void SetDefaultSettings(const SerializationSettings& settings) override; 1048bf80f4bSopenharmony_ci void RegisterGlobalObject(const IObject::Ptr& object) override; 1058bf80f4bSopenharmony_ci void UnregisterGlobalObject(const IObject::Ptr& object) override; 1068bf80f4bSopenharmony_ci IObject::Ptr GetGlobalObject(const InstanceId& id) const override; 1078bf80f4bSopenharmony_ci void RegisterValueSerializer(const IValueSerializer::Ptr&) override; 1088bf80f4bSopenharmony_ci void UnregisterValueSerializer(const TypeId& id) override; 1098bf80f4bSopenharmony_ci IValueSerializer::Ptr GetValueSerializer(const TypeId& id) const override; 1108bf80f4bSopenharmony_ci 1118bf80f4bSopenharmony_ci IEngineInternalValueAccess::Ptr GetInternalValueAccess(const CORE_NS::PropertyTypeDecl& type) const override; 1128bf80f4bSopenharmony_ci void RegisterInternalValueAccess(const CORE_NS::PropertyTypeDecl& type, IEngineInternalValueAccess::Ptr) override; 1138bf80f4bSopenharmony_ci void UnregisterInternalValueAccess(const CORE_NS::PropertyTypeDecl& type) override; 1148bf80f4bSopenharmony_ci IEngineData& GetEngineData() override; 1158bf80f4bSopenharmony_ci 1168bf80f4bSopenharmony_ciprotected: 1178bf80f4bSopenharmony_ci void Ref() override; 1188bf80f4bSopenharmony_ci void Unref() override; 1198bf80f4bSopenharmony_ci 1208bf80f4bSopenharmony_ciprotected: // ITaskQueueRegistry 1218bf80f4bSopenharmony_ci ITaskQueue::Ptr GetTaskQueue(const BASE_NS::Uid& queueId) const override; 1228bf80f4bSopenharmony_ci bool RegisterTaskQueue(const ITaskQueue::Ptr& queue, const BASE_NS::Uid& queueId) override; 1238bf80f4bSopenharmony_ci bool UnregisterTaskQueue(const BASE_NS::Uid& queueId) override; 1248bf80f4bSopenharmony_ci bool HasTaskQueue(const BASE_NS::Uid& queueId) const override; 1258bf80f4bSopenharmony_ci bool UnregisterAllTaskQueues() override; 1268bf80f4bSopenharmony_ci ITaskQueue::Ptr GetCurrentTaskQueue() const override; 1278bf80f4bSopenharmony_ci ITaskQueue::WeakPtr SetCurrentTaskQueue(ITaskQueue::WeakPtr) override; 1288bf80f4bSopenharmony_ci 1298bf80f4bSopenharmony_ciprivate: 1308bf80f4bSopenharmony_ci struct CreateResult { 1318bf80f4bSopenharmony_ci bool successful; 1328bf80f4bSopenharmony_ci uint64_t category; 1338bf80f4bSopenharmony_ci bool singleton = false; 1348bf80f4bSopenharmony_ci }; 1358bf80f4bSopenharmony_ci 1368bf80f4bSopenharmony_ci CreateResult CreateInternal(BASE_NS::Uid uid, BASE_NS::vector<IObject::Ptr>& classes) const; 1378bf80f4bSopenharmony_ci bool ConstructObjectInternal(const IObject::Ptr& obj, BASE_NS::vector<IObject::Ptr>& classes) const; 1388bf80f4bSopenharmony_ci void SetObjectInstanceIds(const BASE_NS::vector<IObject::Ptr>& classes, InstanceId instid) const; 1398bf80f4bSopenharmony_ci bool BuildObject(const BASE_NS::vector<IObject::Ptr>& classes, const IMetadata::Ptr& data) const; 1408bf80f4bSopenharmony_ci bool PostCreate(const BASE_NS::Uid& uid, InstanceId instid, const CreateResult& t, const CreateInfo& createInfo, 1418bf80f4bSopenharmony_ci const BASE_NS::vector<IObject::Ptr>& classes, const IMetadata::Ptr& data) const; 1428bf80f4bSopenharmony_ci 1438bf80f4bSopenharmony_ci BASE_NS::string GetClassName(BASE_NS::Uid uid) const; 1448bf80f4bSopenharmony_ci IObject::Ptr FindSingleton(const BASE_NS::Uid uid) const; 1458bf80f4bSopenharmony_ci void CheckGC() const; 1468bf80f4bSopenharmony_ci void GC() const; 1478bf80f4bSopenharmony_ci void DoDisposal(const BASE_NS::vector<InstanceId>& uids) const; 1488bf80f4bSopenharmony_ci 1498bf80f4bSopenharmony_ci struct ObjectInstance { 1508bf80f4bSopenharmony_ci IObject::WeakPtr ptr; 1518bf80f4bSopenharmony_ci uint64_t category {}; 1528bf80f4bSopenharmony_ci }; 1538bf80f4bSopenharmony_ci 1548bf80f4bSopenharmony_ci mutable std::shared_mutex mutex_; 1558bf80f4bSopenharmony_ci mutable std::shared_mutex disposalMutex_; 1568bf80f4bSopenharmony_ci 1578bf80f4bSopenharmony_ci BASE_NS::unique_ptr<IRandom> random_; 1588bf80f4bSopenharmony_ci 1598bf80f4bSopenharmony_ci // mutable so GC can clean up null objects. (GC is called from const methods) 1608bf80f4bSopenharmony_ci mutable BASE_NS::unordered_map<InstanceId, IObject::WeakPtr> singletons_; 1618bf80f4bSopenharmony_ci mutable BASE_NS::unordered_map<InstanceId, ObjectInstance> instancesByUid_; 1628bf80f4bSopenharmony_ci mutable IObjectContext::Ptr defaultContext_; 1638bf80f4bSopenharmony_ci 1648bf80f4bSopenharmony_ci mutable std::atomic_flag disposalInProgress_ = ATOMIC_FLAG_INIT; 1658bf80f4bSopenharmony_ci mutable std::atomic<size_t> purgeCounter_ {}; 1668bf80f4bSopenharmony_ci mutable BASE_NS::vector<InstanceId> disposals_; 1678bf80f4bSopenharmony_ci mutable BASE_NS::vector<InstanceId> disposalsStorage_; 1688bf80f4bSopenharmony_ci 1698bf80f4bSopenharmony_ci BASE_NS::unordered_map<BASE_NS::Uid, ITaskQueue::Ptr> queues_; 1708bf80f4bSopenharmony_ci 1718bf80f4bSopenharmony_ci ClassRegistry classRegistry_; 1728bf80f4bSopenharmony_ci 1738bf80f4bSopenharmony_ci // Interpolator constructors. 1748bf80f4bSopenharmony_ci BASE_NS::unordered_map<TypeId, BASE_NS::Uid> interpolatorConstructors_; 1758bf80f4bSopenharmony_ci 1768bf80f4bSopenharmony_ci SerializationSettings defaultSettings_; 1778bf80f4bSopenharmony_ci mutable BASE_NS::unordered_map<InstanceId, IObject::WeakPtr> globalObjects_; 1788bf80f4bSopenharmony_ci BASE_NS::unordered_map<TypeId, IValueSerializer::Ptr> valueSerializers_; 1798bf80f4bSopenharmony_ci 1808bf80f4bSopenharmony_ci BASE_NS::unordered_map<ObjectId, BASE_NS::shared_ptr<AnyBuilder>> anyBuilders_; 1818bf80f4bSopenharmony_ci BASE_NS::unordered_map<CORE_NS::PropertyTypeDecl, IEngineInternalValueAccess::Ptr> engineInternalAccess_; 1828bf80f4bSopenharmony_ci}; 1838bf80f4bSopenharmony_ci 1848bf80f4bSopenharmony_ciMETA_END_NAMESPACE() 1858bf80f4bSopenharmony_ci 1868bf80f4bSopenharmony_ci#endif 187