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#include "meta_object.h" 168bf80f4bSopenharmony_ci 178bf80f4bSopenharmony_ci#include <base/util/uid_util.h> 188bf80f4bSopenharmony_ci#include <core/plugin/intf_class_factory.h> 198bf80f4bSopenharmony_ci 208bf80f4bSopenharmony_ci#include <meta/api/iteration.h> 218bf80f4bSopenharmony_ci#include <meta/interface/builtin_objects.h> 228bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h> 238bf80f4bSopenharmony_ci#include <meta/interface/intf_proxy_object.h> 248bf80f4bSopenharmony_ci#include <meta/interface/property/construct_property.h> 258bf80f4bSopenharmony_ci#include <meta/interface/property/intf_property_internal.h> 268bf80f4bSopenharmony_ci 278bf80f4bSopenharmony_ci#include "ref_uri_util.h" 288bf80f4bSopenharmony_ci 298bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 308bf80f4bSopenharmony_cinamespace Internal { 318bf80f4bSopenharmony_ci 328bf80f4bSopenharmony_ci// IObject 338bf80f4bSopenharmony_ciIObject::Ptr MetaObject::Resolve(const RefUri& uri) const 348bf80f4bSopenharmony_ci{ 358bf80f4bSopenharmony_ci if (uri == RefUri::ContextUri()) { 368bf80f4bSopenharmony_ci return interface_pointer_cast<IObjectInstance>(ObjectContext()->GetValue()); 378bf80f4bSopenharmony_ci } 388bf80f4bSopenharmony_ci return Super::Resolve(uri); 398bf80f4bSopenharmony_ci} 408bf80f4bSopenharmony_ci 418bf80f4bSopenharmony_ci// ILifecycle 428bf80f4bSopenharmony_cibool MetaObject::Build(const IMetadata::Ptr& data) 438bf80f4bSopenharmony_ci{ 448bf80f4bSopenharmony_ci bool ret = Super::Build(data); 458bf80f4bSopenharmony_ci if (ret) { 468bf80f4bSopenharmony_ci // update owners in case the top most object was not set when we added some of the properties 478bf80f4bSopenharmony_ci // notice that we are building the object, so no locking is needed. 488bf80f4bSopenharmony_ci auto me = GetSelf(); 498bf80f4bSopenharmony_ci META_NS::Iterate( 508bf80f4bSopenharmony_ci GetPropertyContainer(), 518bf80f4bSopenharmony_ci [&](const IObject::Ptr& p) { 528bf80f4bSopenharmony_ci if (auto pp = interface_cast<IPropertyInternal>(p)) { 538bf80f4bSopenharmony_ci pp->SetOwner(me); 548bf80f4bSopenharmony_ci } 558bf80f4bSopenharmony_ci return true; 568bf80f4bSopenharmony_ci }, 578bf80f4bSopenharmony_ci IterateStrategy { TraversalType::NO_HIERARCHY, LockType::NO_LOCK }); 588bf80f4bSopenharmony_ci } 598bf80f4bSopenharmony_ci return ret; 608bf80f4bSopenharmony_ci} 618bf80f4bSopenharmony_ci 628bf80f4bSopenharmony_civoid MetaObject::Destroy() 638bf80f4bSopenharmony_ci{ 648bf80f4bSopenharmony_ci if (auto c = GetMetadata()) { 658bf80f4bSopenharmony_ci c->GetPropertyContainer()->RemoveAll(); 668bf80f4bSopenharmony_ci } 678bf80f4bSopenharmony_ci Super::Destroy(); 688bf80f4bSopenharmony_ci} 698bf80f4bSopenharmony_ci 708bf80f4bSopenharmony_ci// IObjectContextProvider 718bf80f4bSopenharmony_ciIProperty::Ptr MetaObject::PropertyObjectContext() 728bf80f4bSopenharmony_ci{ 738bf80f4bSopenharmony_ci if (!objectContext_) { 748bf80f4bSopenharmony_ci // By default use the global object context 758bf80f4bSopenharmony_ci auto context = META_NS::GetDefaultObjectContext(); 768bf80f4bSopenharmony_ci CORE_ASSERT(context); 778bf80f4bSopenharmony_ci objectContext_ = ConstructProperty<IObjectContext::Ptr>(context->GetObjectRegistry(), "ObjectContext", context); 788bf80f4bSopenharmony_ci CORE_ASSERT(objectContext_); 798bf80f4bSopenharmony_ci if (auto internal = interface_cast<IPropertyInternal>(objectContext_.GetProperty())) { 808bf80f4bSopenharmony_ci internal->SetOwner(GetSelf()); 818bf80f4bSopenharmony_ci } 828bf80f4bSopenharmony_ci } 838bf80f4bSopenharmony_ci return objectContext_; 848bf80f4bSopenharmony_ci} 858bf80f4bSopenharmony_ci 868bf80f4bSopenharmony_ciIProperty::ConstPtr MetaObject::PropertyObjectContext() const 878bf80f4bSopenharmony_ci{ 888bf80f4bSopenharmony_ci if (!objectContext_) { 898bf80f4bSopenharmony_ci // By default use the global object context 908bf80f4bSopenharmony_ci auto context = META_NS::GetDefaultObjectContext(); 918bf80f4bSopenharmony_ci CORE_ASSERT(context); 928bf80f4bSopenharmony_ci objectContext_ = ConstructProperty<IObjectContext::Ptr>(context->GetObjectRegistry(), "ObjectContext", context); 938bf80f4bSopenharmony_ci CORE_ASSERT(objectContext_); 948bf80f4bSopenharmony_ci if (auto internal = interface_cast<IPropertyInternal>(objectContext_.GetProperty())) { 958bf80f4bSopenharmony_ci internal->SetOwner(GetSelf()); 968bf80f4bSopenharmony_ci } 978bf80f4bSopenharmony_ci } 988bf80f4bSopenharmony_ci return objectContext_; 998bf80f4bSopenharmony_ci} 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_civoid MetaObject::ResetObjectContext() 1028bf80f4bSopenharmony_ci{ 1038bf80f4bSopenharmony_ci if (objectContext_) { 1048bf80f4bSopenharmony_ci objectContext_->SetValue(META_NS::GetDefaultObjectContext()); 1058bf80f4bSopenharmony_ci } 1068bf80f4bSopenharmony_ci} 1078bf80f4bSopenharmony_ci 1088bf80f4bSopenharmony_ciIObjectRegistry& MetaObject::GetObjectRegistry() const 1098bf80f4bSopenharmony_ci{ 1108bf80f4bSopenharmony_ci if (objectContext_) { 1118bf80f4bSopenharmony_ci if (auto ctx = objectContext_->GetValue()) { 1128bf80f4bSopenharmony_ci return ctx->GetObjectRegistry(); 1138bf80f4bSopenharmony_ci } 1148bf80f4bSopenharmony_ci } 1158bf80f4bSopenharmony_ci // No context set 1168bf80f4bSopenharmony_ci return Super::GetObjectRegistry(); 1178bf80f4bSopenharmony_ci} 1188bf80f4bSopenharmony_ci 1198bf80f4bSopenharmony_ciIMetadata::Ptr MetaObject::CloneMetadata() const 1208bf80f4bSopenharmony_ci{ 1218bf80f4bSopenharmony_ci return meta_->CloneMetadata(); 1228bf80f4bSopenharmony_ci} 1238bf80f4bSopenharmony_ci 1248bf80f4bSopenharmony_ciIContainer::Ptr MetaObject::GetPropertyContainer() 1258bf80f4bSopenharmony_ci{ 1268bf80f4bSopenharmony_ci return meta_->GetPropertyContainer(); 1278bf80f4bSopenharmony_ci} 1288bf80f4bSopenharmony_ci 1298bf80f4bSopenharmony_ciIContainer::ConstPtr MetaObject::GetPropertyContainer() const 1308bf80f4bSopenharmony_ci{ 1318bf80f4bSopenharmony_ci return meta_->GetPropertyContainer(); 1328bf80f4bSopenharmony_ci} 1338bf80f4bSopenharmony_ci 1348bf80f4bSopenharmony_civoid MetaObject::AddFunction(const IFunction::Ptr& p) 1358bf80f4bSopenharmony_ci{ 1368bf80f4bSopenharmony_ci meta_->AddFunction(p); 1378bf80f4bSopenharmony_ci} 1388bf80f4bSopenharmony_civoid MetaObject::RemoveFunction(const IFunction::Ptr& p) 1398bf80f4bSopenharmony_ci{ 1408bf80f4bSopenharmony_ci meta_->RemoveFunction(p); 1418bf80f4bSopenharmony_ci} 1428bf80f4bSopenharmony_civoid MetaObject::AddProperty(const IProperty::Ptr& p) 1438bf80f4bSopenharmony_ci{ 1448bf80f4bSopenharmony_ci if (auto pp = interface_pointer_cast<IPropertyInternal>(p)) { 1458bf80f4bSopenharmony_ci pp->SetOwner(GetSelf()); 1468bf80f4bSopenharmony_ci } 1478bf80f4bSopenharmony_ci meta_->AddProperty(p); 1488bf80f4bSopenharmony_ci} 1498bf80f4bSopenharmony_civoid MetaObject::RemoveProperty(const IProperty::Ptr& p) 1508bf80f4bSopenharmony_ci{ 1518bf80f4bSopenharmony_ci meta_->RemoveProperty(p); 1528bf80f4bSopenharmony_ci} 1538bf80f4bSopenharmony_civoid MetaObject::AddEvent(const IEvent::Ptr& p) 1548bf80f4bSopenharmony_ci{ 1558bf80f4bSopenharmony_ci meta_->AddEvent(p); 1568bf80f4bSopenharmony_ci} 1578bf80f4bSopenharmony_civoid MetaObject::RemoveEvent(const IEvent::Ptr& p) 1588bf80f4bSopenharmony_ci{ 1598bf80f4bSopenharmony_ci meta_->RemoveEvent(p); 1608bf80f4bSopenharmony_ci} 1618bf80f4bSopenharmony_civoid MetaObject::SetProperties(const BASE_NS::vector<IProperty::Ptr>& vec) 1628bf80f4bSopenharmony_ci{ 1638bf80f4bSopenharmony_ci meta_->SetProperties(vec); 1648bf80f4bSopenharmony_ci} 1658bf80f4bSopenharmony_civoid MetaObject::Merge(const IMetadata::Ptr& data) 1668bf80f4bSopenharmony_ci{ 1678bf80f4bSopenharmony_ci meta_->Merge(data); 1688bf80f4bSopenharmony_ci} 1698bf80f4bSopenharmony_ciBASE_NS::vector<IProperty::Ptr> MetaObject::GetAllProperties() 1708bf80f4bSopenharmony_ci{ 1718bf80f4bSopenharmony_ci return meta_->GetAllProperties(); 1728bf80f4bSopenharmony_ci} 1738bf80f4bSopenharmony_ciBASE_NS::vector<IProperty::ConstPtr> MetaObject::GetAllProperties() const 1748bf80f4bSopenharmony_ci{ 1758bf80f4bSopenharmony_ci return static_cast<const IMetadata*>(meta_.get())->GetAllProperties(); 1768bf80f4bSopenharmony_ci} 1778bf80f4bSopenharmony_ciBASE_NS::vector<IFunction::Ptr> MetaObject::GetAllFunctions() 1788bf80f4bSopenharmony_ci{ 1798bf80f4bSopenharmony_ci return meta_->GetAllFunctions(); 1808bf80f4bSopenharmony_ci} 1818bf80f4bSopenharmony_ciBASE_NS::vector<IFunction::ConstPtr> MetaObject::GetAllFunctions() const 1828bf80f4bSopenharmony_ci{ 1838bf80f4bSopenharmony_ci return static_cast<const IMetadata*>(meta_.get())->GetAllFunctions(); 1848bf80f4bSopenharmony_ci} 1858bf80f4bSopenharmony_ciBASE_NS::vector<IEvent::Ptr> MetaObject::GetAllEvents() 1868bf80f4bSopenharmony_ci{ 1878bf80f4bSopenharmony_ci return meta_->GetAllEvents(); 1888bf80f4bSopenharmony_ci} 1898bf80f4bSopenharmony_ciBASE_NS::vector<IEvent::ConstPtr> MetaObject::GetAllEvents() const 1908bf80f4bSopenharmony_ci{ 1918bf80f4bSopenharmony_ci return static_cast<const IMetadata*>(meta_.get())->GetAllEvents(); 1928bf80f4bSopenharmony_ci} 1938bf80f4bSopenharmony_ciIProperty::Ptr MetaObject::GetPropertyByName(BASE_NS::string_view name) 1948bf80f4bSopenharmony_ci{ 1958bf80f4bSopenharmony_ci return meta_->GetPropertyByName(name); 1968bf80f4bSopenharmony_ci} 1978bf80f4bSopenharmony_ciIProperty::ConstPtr MetaObject::GetPropertyByName(BASE_NS::string_view name) const 1988bf80f4bSopenharmony_ci{ 1998bf80f4bSopenharmony_ci return meta_->GetPropertyByName(name); 2008bf80f4bSopenharmony_ci} 2018bf80f4bSopenharmony_ciIFunction::Ptr MetaObject::GetFunctionByName(BASE_NS::string_view name) 2028bf80f4bSopenharmony_ci{ 2038bf80f4bSopenharmony_ci return meta_->GetFunctionByName(name); 2048bf80f4bSopenharmony_ci} 2058bf80f4bSopenharmony_ciIFunction::ConstPtr MetaObject::GetFunctionByName(BASE_NS::string_view name) const 2068bf80f4bSopenharmony_ci{ 2078bf80f4bSopenharmony_ci return meta_->GetFunctionByName(name); 2088bf80f4bSopenharmony_ci} 2098bf80f4bSopenharmony_ciIEvent::ConstPtr MetaObject::GetEventByName(BASE_NS::string_view name) const 2108bf80f4bSopenharmony_ci{ 2118bf80f4bSopenharmony_ci return meta_->GetEventByName(name); 2128bf80f4bSopenharmony_ci} 2138bf80f4bSopenharmony_ciIEvent::Ptr MetaObject::GetEventByName(BASE_NS::string_view name) 2148bf80f4bSopenharmony_ci{ 2158bf80f4bSopenharmony_ci return meta_->GetEventByName(name); 2168bf80f4bSopenharmony_ci} 2178bf80f4bSopenharmony_ciIMetadata::Ptr MetaObject::GetMetadata() const 2188bf80f4bSopenharmony_ci{ 2198bf80f4bSopenharmony_ci return meta_; 2208bf80f4bSopenharmony_ci} 2218bf80f4bSopenharmony_civoid MetaObject::SetMetadata(const IMetadata::Ptr& meta) 2228bf80f4bSopenharmony_ci{ 2238bf80f4bSopenharmony_ci meta_ = meta; 2248bf80f4bSopenharmony_ci} 2258bf80f4bSopenharmony_ci 2268bf80f4bSopenharmony_ciconst StaticObjectMetadata& MetaObject::GetStaticMetadata() const 2278bf80f4bSopenharmony_ci{ 2288bf80f4bSopenharmony_ci return GetStaticObjectMetadata(); 2298bf80f4bSopenharmony_ci} 2308bf80f4bSopenharmony_ci 2318bf80f4bSopenharmony_ci} // namespace Internal 2328bf80f4bSopenharmony_ci 2338bf80f4bSopenharmony_ciMETA_END_NAMESPACE() 234